Skip to content

Instantly share code, notes, and snippets.

@yaswanthrajyadiki
Created September 2, 2015 07:22
Show Gist options
  • Save yaswanthrajyadiki/168d41929dd3687f082d to your computer and use it in GitHub Desktop.
Save yaswanthrajyadiki/168d41929dd3687f082d to your computer and use it in GitHub Desktop.
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.StringTokenizer;
class Plagiarism {
String filename1;
String filename2;
String content1;
String content2;
Plagarism(String filename1, String filename2) {
this.filename1 = filename1;
this.filename2 = filename2;
content1 = this.extractContent(filename1);
content2 = this.extractContent(filename2);
}
public String extractContent(String filename) {
StringBuffer content = new StringBuffer();
try {
BufferedReader br = new BufferedReader(new FileReader(filename));
String line;
while ((line = br.readLine()) != null) {
content.append(line);
}
br.close();
} catch (Exception e) {
System.out.println(e);
}
return content.toString();
}
public int calcPlagarismPercent() {
int percent;
int count1 = 0;
int commonWordCount = 0;
StringTokenizer st = new StringTokenizer(content1,
".><:&();{}, \t\n\"");
while (st.hasMoreTokens()) {
String word = st.nextToken();
count1++;
if (content2.contains(word)) {
commonWordCount++;
}
}
percent = (commonWordCount * 100)/count1;
return percent;
}
}
class PlagiarismDemo {
public static void main(String[] args) {
String filename1 = "Document1.txt";
String filename2 = "Document2.txt";
Plagarism p = new Plagarism(filename1, filename2);
System.out.println(p.calcPlagarismPercent() + "%");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment