|
package SentenceSplitting; |
|
|
|
import java.io.BufferedReader; |
|
import java.io.BufferedWriter; |
|
import java.io.File; |
|
import java.io.FileReader; |
|
import java.io.FileWriter; |
|
import java.io.IOException; |
|
import java.util.ArrayList; |
|
import java.util.List; |
|
|
|
import opennlp.tools.sentdetect.SentenceDetectorME; |
|
import opennlp.tools.sentdetect.SentenceModel; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public class EvaluateModelSS { |
|
|
|
public static void main(String[] args) throws IOException { |
|
|
|
String testDir = args[0]; |
|
String gsDir = args[1]; |
|
String out = args[2]; |
|
String modelFile = args[3]; |
|
|
|
|
|
SentenceModel model = new SentenceModel(new File(modelFile)); |
|
SentenceDetectorME sentenceDetectorME = new SentenceDetectorME(model); |
|
|
|
|
|
|
|
double totalFiles = 0; |
|
double totalCorrect = 0; |
|
double totalCorrectSentencesIncluded = 0; |
|
double totalWrong = 0; |
|
double more = 0; |
|
double less = 0; |
|
|
|
|
|
double totalSentencesGS = 0; |
|
double totalSentencesTest = 0; |
|
double totalSentencesCorrect = 0; |
|
double totalSentencesWrong = 0; |
|
|
|
|
|
|
|
|
|
File testFiles[] = new File(testDir).listFiles(); |
|
for (int i = 0; i < testFiles.length; i++) |
|
{ |
|
String name = testFiles[i].getName(); |
|
|
|
|
|
List<String> fullText = new ArrayList<String>(); |
|
BufferedReader reader = new BufferedReader(new FileReader(testDir + File.separator + name)); |
|
String line = ""; |
|
while ((line = reader.readLine()) != null) |
|
{ |
|
fullText.add(line); |
|
} |
|
reader.close(); |
|
|
|
|
|
String gsName = name; |
|
List<String> gsSentenceList = new ArrayList<String>(); |
|
BufferedReader reader2 = new BufferedReader(new FileReader(gsDir + File.separator + gsName)); |
|
String line2 = ""; |
|
while ((line2 = reader2.readLine()) != null) |
|
{ |
|
gsSentenceList.add(line2); |
|
} |
|
reader2.close(); |
|
String[] gsSentences = new String[gsSentenceList.size()]; |
|
gsSentences = gsSentenceList.toArray(gsSentences); |
|
|
|
|
|
totalSentencesGS = totalSentencesGS + gsSentenceList.size(); |
|
|
|
|
|
int testSentences = 0; |
|
List<String> testSentenceList = new ArrayList<String>(); |
|
BufferedWriter writer = new BufferedWriter(new FileWriter(out + File.separator + gsName)); |
|
for (int j = 0; j < fullText.size(); j++) |
|
{ |
|
String origLine = fullText.get(j); |
|
|
|
String[] sentences = sentenceDetectorME.sentDetect(origLine); |
|
for (int k = 0; k < sentences.length; k++) |
|
{ |
|
writer.write(sentences[k] + "\n"); |
|
testSentenceList.add(sentences[k]); |
|
testSentences++; |
|
} |
|
} |
|
writer.close(); |
|
|
|
|
|
totalSentencesTest = totalSentencesTest + testSentenceList.size(); |
|
|
|
|
|
totalFiles++; |
|
if (testSentences == gsSentences.length) |
|
{ |
|
|
|
totalCorrect++; |
|
|
|
if (gsSentenceList.equals(testSentenceList)) |
|
{ |
|
|
|
totalCorrectSentencesIncluded++; |
|
totalSentencesCorrect = totalSentencesCorrect + testSentenceList.size(); |
|
} |
|
else |
|
{ |
|
|
|
totalWrong++; |
|
|
|
|
|
|
|
|
|
} |
|
} |
|
else |
|
{ |
|
|
|
totalWrong++; |
|
System.out.println("Document " + name + " splitting does not match: " + testSentences + " vs " + gsSentences.length); |
|
|
|
|
|
if (testSentences > gsSentences.length) |
|
{ |
|
more++; |
|
} |
|
else if (testSentences < gsSentences.length) |
|
{ |
|
less++; |
|
} |
|
|
|
|
|
|
|
|
|
|
|
int k = 0; |
|
for (int j = 0; j < testSentenceList.size(); j++) |
|
{ |
|
String testSentence = testSentenceList.get(j); |
|
String gsSentence = gsSentenceList.get(k); |
|
if (testSentence.equals(gsSentence)) |
|
{ |
|
|
|
totalSentencesCorrect++; |
|
k++; |
|
} |
|
else |
|
{ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
totalSentencesWrong++; |
|
System.out.println("\t" + testSentence); |
|
|
|
|
|
if (k+1 == gsSentenceList.size() || j+1 == testSentenceList.size()) |
|
{ |
|
break; |
|
} |
|
|
|
if (testSentence.contains(gsSentence)) |
|
{ |
|
|
|
|
|
|
|
int before = k; |
|
k++; |
|
String nextTest = testSentenceList.get(j+1); |
|
String nextGS = gsSentenceList.get(k); |
|
|
|
boolean end = false; |
|
|
|
while (!nextTest.equals(nextGS)) |
|
{ |
|
k++; |
|
try |
|
{ |
|
nextGS = gsSentenceList.get(k); |
|
} |
|
catch (Exception e) |
|
{ |
|
|
|
end = true; |
|
break; |
|
} |
|
} |
|
if (end) |
|
{ |
|
k = before; |
|
} |
|
} |
|
else if (gsSentence.contains(testSentence)) |
|
{ |
|
|
|
|
|
|
|
int before = j; |
|
j++; |
|
String nextTest = testSentenceList.get(j); |
|
String nextGS = gsSentenceList.get(k+1); |
|
|
|
boolean end = false; |
|
|
|
while (!nextTest.equals(nextGS)) |
|
{ |
|
j++; |
|
try |
|
{ |
|
nextTest = testSentenceList.get(j); |
|
} |
|
catch (Exception e) |
|
{ |
|
|
|
end = true; |
|
break; |
|
} |
|
} |
|
if (!end) |
|
{ |
|
j--; |
|
} |
|
else |
|
{ |
|
j = before; |
|
} |
|
k++; |
|
} |
|
else |
|
{ |
|
|
|
|
|
|
|
|
|
|
|
if (testSentenceList.size() > gsSentenceList.size()) |
|
{ |
|
j++; |
|
} |
|
else if (testSentenceList.size() < gsSentenceList.size()) |
|
{ |
|
k++; |
|
j--; |
|
} |
|
else |
|
{ |
|
k++; |
|
} |
|
} |
|
} |
|
} |
|
} |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
System.out.println("Total correct with same number of sentences: " + totalCorrect); |
|
double percent = totalCorrect * 100 / totalFiles; |
|
System.out.println("Results by number of sentences: " + percent + "%"); |
|
System.out.println("Total correct with same sentences: " + totalCorrectSentencesIncluded); |
|
double percentSameSentences = totalCorrectSentencesIncluded * 100 / totalFiles; |
|
System.out.println("Results by same sentences: " + percentSameSentences + "%"); |
|
System.out.println("Total wrong: " + totalWrong); |
|
System.out.println("Total files: " + totalFiles); |
|
System.out.println("More sentences generated in test: " + more); |
|
System.out.println("More sentences found in gold standard: " + less); |
|
|
|
|
|
|
|
System.out.println("Sentences in Gold Standard: " + totalSentencesGS); |
|
System.out.println("Sentences in Test: " + totalSentencesTest); |
|
System.out.println("Sentences correct: " + totalSentencesCorrect); |
|
System.out.println("Sentences wrong: " + totalSentencesWrong); |
|
double precision = totalSentencesCorrect * 100 / totalSentencesTest; |
|
double recall = totalSentencesCorrect * 100 / totalSentencesGS; |
|
double F1 = (2 * precision * recall) / (precision + recall); |
|
System.out.println("Precision: " + precision); |
|
System.out.println("Recall: " + recall); |
|
System.out.println("F1: " + F1); |
|
} |
|
|
|
} |
|
|