Coders Packet

Plagiarism Checker With Java

By Apoorwa

The given tool is used to compare two text files and output the percentage similarity between them to check for plagiarism.

In the digital age, the ease of accessing vast repositories of information has revolutionized education, research, and creative endeavours. However, this accessibility has also led to a rise in academic dishonesty, most notably plagiarism, where individuals present others' work as their own. Plagiarism not only undermines the integrity of academic institutions but also hampers the growth of original ideas and knowledge.

To combat this issue effectively, we present a pioneering project that leverages the power of Data Structures and Algorithms (DSA) to detect plagiarism in text-based documents. Our goal is to create a robust and efficient plagiarism detection system that can accurately identify similarities between two text files and reveal potential instances of content misuse.

By employing techniques like string matching, hashing, and similarity metrics, we aim to create a scalable and reliable solution that can cater to the demands of both small-scale academic assignments.

The code takes input of two text files and stores one file in hashmap to check the occurrence of each word by comparing every sentence between the text files. It displays the sentences which have more than 70 percent of words similar and calculates the percentage similarity accordingly. The given code is written in java using basic java modules for creating a graphical user interface like swing and awt along with some input outout modules. This packet can be used by simply running the given code in any java runtime environment platform like intellij, netbeans or any online IDE's.

 

Following is the code for comparing two text files and calculating percentage similarity :

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.*;
import java.util.*;

public class TextFileSimilarityGUI extends JFrame {
    private JTextField file1TextField;
    private JTextField file2TextField;
    private JButton browseFile1Button;
    private JButton browseFile2Button;
    private JButton calculateButton;
    private JTextArea resultTextArea;

    public TextFileSimilarityGUI() {
        setTitle("Text File Similarity Calculator");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(500, 300);
        setLocationRelativeTo(null);
        setLayout(new BorderLayout());

        JPanel inputPanel = new JPanel();
        inputPanel.setLayout(new FlowLayout());
        file1TextField = new JTextField(20);
        file2TextField = new JTextField(20);
        browseFile1Button = new JButton("Browse File 1");
        browseFile2Button = new JButton("Browse File 2");
        calculateButton = new JButton("Calculate Similarity");

        inputPanel.add(new JLabel("File 1:"));
        inputPanel.add(file1TextField);
        inputPanel.add(browseFile1Button);
        inputPanel.add(new JLabel("File 2:"));
        inputPanel.add(file2TextField);
        inputPanel.add(browseFile2Button);
        inputPanel.add(calculateButton);

        resultTextArea = new JTextArea(10, 40);
        resultTextArea.setEditable(false);

        add(inputPanel, BorderLayout.NORTH);
        add(new JScrollPane(resultTextArea), BorderLayout.CENTER);

        browseFile1Button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser fileChooser = new JFileChooser();
                int returnValue = fileChooser.showOpenDialog(null);
                if (returnValue == JFileChooser.APPROVE_OPTION) {
                    File selectedFile = fileChooser.getSelectedFile();
                    file1TextField.setText(selectedFile.getAbsolutePath());
                }
            }
        });

        browseFile2Button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser fileChooser = new JFileChooser();
                int returnValue = fileChooser.showOpenDialog(null);
                if (returnValue == JFileChooser.APPROVE_OPTION) {
                    File selectedFile = fileChooser.getSelectedFile();
                    file2TextField.setText(selectedFile.getAbsolutePath());
                }
            }
        });

        calculateButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String file1Path = file1TextField.getText();
                String file2Path = file2TextField.getText();

                try {
                    String similarityResult = calculateSimilarity(file1Path, file2Path);
                    resultTextArea.setText(similarityResult);
                } catch (IOException ex) {
                    resultTextArea.setText("Error reading files: " + ex.getMessage());
                }
            }
        });
    }
    static float compareStr(String s1, String s2)
    {
        int i=0;
        int l;
        float avg;
        int count=0;
        if(s1.length()<s2.length()) {

            StringTokenizer tokenizer1 = new StringTokenizer(s1);
            StringTokenizer tokenizer2 = new StringTokenizer(s2);

            Map<String, Integer> map = new HashMap<>();

            while (tokenizer2.hasMoreTokens()) {
                String word = tokenizer2.nextToken();
                map.put(word,i);
                i++;

            }
            l=i;
            while (tokenizer1.hasMoreTokens()){
                String word = tokenizer1.nextToken();

                if(map.get(word)!=null)
                    count++;

            }
            avg=((float)count/(float)l)*100;

        }
        else {

            StringTokenizer tokenizer1 = new StringTokenizer(s1);
            StringTokenizer tokenizer2 = new StringTokenizer(s2);
            Map<String, Integer> map = new HashMap<>();

            while (tokenizer1.hasMoreTokens()) {
                String word = tokenizer1.nextToken();
                map.put(word,i);
                i++;
            }
            l=i;
            while (tokenizer2.hasMoreTokens()){
                String word = tokenizer2.nextToken();
                if(map.get(word)!=null)
                    count++;
            }
            avg=((float)count/(float)l)*100;
        }
        return avg;
    }
    private String calculateSimilarity(String file1Path, String file2Path) throws IOException {
        float per = 0;
        String s="";
        try {
            File file = new File(file1Path);
            Scanner scanner1 = new Scanner(file);
            scanner1.useDelimiter("\\.");
            File file2 = new File(file2Path);
            Scanner scanner2 = new Scanner(file2);
            scanner2.useDelimiter("\\.");
            Map<Integer,String> hashMap1 = new HashMap<>();
            Map< Integer,String> hashMap2 = new HashMap<>();
            // Read the content line by line
            int i=0;
            while (scanner1.hasNext()) {

                String line1 = scanner1.next();
                hashMap1.put(i,line1);
                i++;
            }
            i=0;
            while (scanner2.hasNext()) {

                String line1 = scanner2.next();
                hashMap2.put(i,line1);
                i++;
            }
            int c=0;
            float ans=0;
            int j=0;
            i=0;
            Iterator<Map.Entry< Integer,String>> iterator1 = hashMap1.entrySet().iterator();
            System.out.println("The sentences similar in both the files are : ");
            System.out.println();
            while (iterator1.hasNext()) {

                String s1= String.valueOf(hashMap1.get(i));
                j=0;
                Iterator<Map.Entry< Integer,String>> iterator2 = hashMap2.entrySet().iterator();
                while(iterator2.hasNext())
                {

                    String s2= String.valueOf(hashMap2.get(j));
                    float same=compareStr(s1,s2);
                    ans=ans+same;
                    if(same>=70.0) {
                        System.out.println(s1);
                        c++;
                    }
                    j++;
                    Map.Entry<Integer, String> entry = iterator2.next();
                }
                i++;
                Map.Entry<Integer, String> entry = iterator1.next();
            }

            per=((float)c/(float)(i))*100;
            s=Float.toString(per);
            scanner1.close();
            scanner2.close();
        } catch (FileNotFoundException e) {
            System.out.println("File not found ");
        }
        return s;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new TextFileSimilarityGUI().setVisible(true);
            }
        });
    }
}

For Example :

If we input two files files,

File 1:

File 1

File 2:

File 2

The output on the Graphical User Interface will be:

GUI

The code also displays the similar texts from the two files:

Output

Download Complete Code

Comments

No comments yet

Download Packet

Reviews Report

Submitted by Apoorwa (Apoorwa)

Download packets of source code on Coders Packet