-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLCS.java
More file actions
130 lines (118 loc) · 3.31 KB
/
LCS.java
File metadata and controls
130 lines (118 loc) · 3.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import java.util.*;
import java.io.*;
interface longestcs
{
public String Char(String s);
public int strmatch(String s1,String s2);
}
class ValInp implements longestcs
{
public String Char(String str)
{
String st="";
for (int i=0;i<str.length();i++ ) //Converts the string into lowercase
{
if(95<=str.charAt(i) && str.charAt(i)<=122 || str.charAt(i)==32)
{
st=st+str.charAt(i);
}
}
return st;
}
public int strmatch(String str1,String str2)
{
int lcs=0;
for (int i=0;i<str1.length();i++ )
{
for (int j=0;j<str2.length() ;j++ )
{
String st="";
int y=0;
while(i+y<str1.length() && j+y<str2.length() && str1.charAt(i+y)==str2.charAt(j+y))
{
st=st+str1.charAt(i);
y++;
}
if(st.length()>lcs)
{
lcs=st.length(); //Calculates the length of Longest common Substring
}
}
}
return lcs;
}
}
class LCS
{
public static String Fileread(String str)throws FileNotFoundException
{
int count=0;
File file = new File(str);
String str1="";
try
{
Scanner s= new Scanner(file);
while(s.hasNextLine())
{
str1+=s.nextLine();
str1=str1.replace("\n"," "); //replaces the nextLine with spaces
count=count+1;
}
s.close(); //Closes the file
}
catch(Exception e)
{
e.printStackTrace(); //prints stack trace for the object
}
return str1;
}
public static void main(String[] args) throws FileNotFoundException
{
File folder=new File(args[0]);
int fcount=0;
File[] lf=folder.listFiles();
File[] f_name=new File[lf.length];
for (int i=0;i<lf.length ;++i ){
File file=lf[i];
if(file.getName().endsWith(".txt"))
{ //Files that ends with text are found
f_name[fcount]=file;
fcount++; //Filecount is found
}
}
if(fcount==0){
System.out.println("Empty Directory");
}
for (int i=0;i<fcount;++i )
{
System.out.print(" "+f_name[i].getName()); //Column printing of file names
}
System.out.println("\n");
for (int i=0;i<fcount;++i)
{
System.out.print(f_name[i].getName()); //Row printing of file names
for (int j=0;j<fcount;++j)
{
if(i==j)
{
System.out.printf("\t\t"+"%.2f",100.00);
}
else
{
ValInp l=new ValInp();
String str3=Fileread(f_name[i].getName());
String str4=Fileread(f_name[j].getName());
String str1=l.Char(str3);
String str2=l.Char(str4);
int tlength = str1.length()+str2.length();
int lstring = l.strmatch(str1,str2); // Maximum length of the common substring is calculated
if(lstring*2==tlength)
System.out.printf("\t\t"+"%.2f",100.00);
else
System.out.printf("\t\t"+"%.2f",(float)(lstring*200)/tlength); //plagiarism percentage is calculated
}
}
System.out.println();
}
}
}