这是我在大学项目中所做的代码的一部分,所以基本上我正在使用两种字符串匹配算法并在主类中使用它进行简单的抄袭检测,因为我在循环中犯了一些错误,因此我的输出是重复 12 次并一次又一次地检查我的代码,但无法真正弄清楚我哪里出错了我真的需要有人帮助我解决这个问题我必须在本月底之前提交这个我附上我的输出照片输出
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
public class MClass {
public static void main(String[] args) throws IOException {
String ptrnLine, textLine,inpLine,sFilePath,srcLine;
int srcLineIndex=1, inpLineIndex=1;
KMP kmpComponent;
RabinKarp rkComponent;
int inputLen,srcLen,patterntextLength;
double kmpSimRatio = 0;
int rkNumberOfMatches;
int fullTextLength=0, fullPatternLength=0;
boolean rkPlagarismStatus = false;
final File folder = new File("D:\\Project");
File fileKmp = new File("kmp.txt");
File fileRK = new File("rk.txt");
int coun = 0;
fileKmp.delete();
fileRK.delete();
FileWriter outKmpFile = new FileWriter("kmp.txt", true);
FileWriter outRkFile = new FileWriter("rk.txt", true);
for (final File fileEntry : folder.listFiles()) {
sFilePath = fileEntry.getPath();
srcLineIndex=1;
File sourceFile = new File("source.txt");
File inputFile = new File( "input.txt");
@SuppressWarnings("resource")
BufferedReader sReader = new BufferedReader( new FileReader(sourceFile));
while((srcLine = sReader.readLine())!=null)
{
BufferedReader reader = new BufferedReader( new FileReader(inputFile));
inpLineIndex=1;
fullTextLength = fullTextLength+srcLine.length();
while((inpLine = reader.readLine())!=null)
{
inputLen = inpLine.length();
srcLen = srcLine.length();
if(inputLen>0 && srcLen>0)
{
if(srcLen>inputLen)
{
textLine = srcLine;
ptrnLine = inpLine;
}
else
{
textLine = inpLine;
ptrnLine = srcLine;
}
patterntextLength = ptrnLine.length();
if(coun<1)
{
fullPatternLength = fullPatternLength+ ptrnLine.length();
}
// KMP Algorithm
kmpComponent = new KMP();
if(patterntextLength!=0)
{ kmpSimRatio= (kmpComponent.searchSubString(textLine, ptrnLine)/(double)(patterntextLength));
}
System.out.println("KMP Algorithm Result");
System.out.println("Similarity ratio = "+kmpSimRatio*100.000+" Line Number of the input file= "+inpLineIndex+
" Line Number of the source file = "+srcLineIndex);
System.out.println("------------------------------------------------------------------------------------------------------------------------------------------");
PrintWriter outPKmpFile = new PrintWriter(outKmpFile);
if(kmpSimRatio>0.60)
{ outPKmpFile.append("Line "+inpLineIndex + " of the input file has plagarised " +kmpSimRatio*100.000+
"% from line "+srcLineIndex +" of the source file \n");
}
//Rabin Karp Algorithm
rkComponent = new RabinKarp();
if(patterntextLength!=0)
{
rkNumberOfMatches = rkComponent.search(ptrnLine,textLine);
if(rkNumberOfMatches>0)
{
rkPlagarismStatus = true;
}
else
{
rkPlagarismStatus =false;
}
if(rkPlagarismStatus)
{ System.out.println("Rabin Karp Algorithm Result");
System.out.println(" Line Number of the input file= "+inpLineIndex+ " is plagarised from" +
" Line Number of the source file = "+srcLineIndex+" Number of times string matched was "+rkNumberOfMatches);
System.out.println("------------------------------------------------------------------------------------------------------------------------------------------");
PrintWriter outPRkFile = new PrintWriter(outRkFile);
outPRkFile.append("Line "+inpLineIndex + " of the input file has plagarised from line "+srcLineIndex +" of the source file "+fileEntry.getName()+
" "+rkNumberOfMatches+" time string matching found\n");
}
}
inpLineIndex++;
}
}
coun++;
srcLineIndex++;
}
}
outKmpFile.close();
outRkFile.close();
}
}