File size: 11,242 Bytes
393badf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
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;

/*
 * This class evaluates the performance of the sentence splitting model created with the class CreateModelSS.
 * Input:
 * - Test set folder (one file per document).
 * - Gold standard folder, one file per document and one sentence per line.
 * - Folder to print the results of the sentence splitting process with the created model, one sentence per line.
 * - Model file.
 * Output:
 * - Files with sentences splitted.
 * - Evaluation of the sentence splitter, showing the number of documents that match with the gold standard, with exactly the same output,
 * and the number of sentences per document that match with the gold sentences.
 */

public class EvaluateModelSS {

	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		String testDir = args[0];
		String gsDir = args[1];
		String out = args[2];
		String modelFile = args[3];
		
		// Load the model
		SentenceModel model = new SentenceModel(new File(modelFile));
		SentenceDetectorME sentenceDetectorME = new SentenceDetectorME(model);
		
		// Variables for statistics.
		// Document level evaluation.
		double totalFiles = 0;
		double totalCorrect = 0;
		double totalCorrectSentencesIncluded = 0;
		double totalWrong = 0;
		double more = 0;
		double less = 0;
		
		// Sentence-level evaluation, used to get Precision, Recall and F-Measure.
		double totalSentencesGS = 0;
		double totalSentencesTest = 0;
		double totalSentencesCorrect = 0;
		double totalSentencesWrong = 0;
		
		/*
		 * Load each test file, split the sentences, and evaluate the output against the gold standard.
		 */
		File testFiles[] = new File(testDir).listFiles();
		for (int i = 0; i < testFiles.length; i++)
		{
			String name = testFiles[i].getName();
			
			// Load the test file.
			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();
			
		    // Load the Gold Standard file.
		    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);	
		    
		 // Total sentences in the Gold Standard.
		    totalSentencesGS = totalSentencesGS + gsSentenceList.size();
	         
		    // Split sentences of the testing set and write the results in a new file.
		    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);
		    	// Split the sentences of the line and save all sentences in an array.
		    	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();
		    
		    // Total sentences in the testing set.
		    totalSentencesTest = totalSentencesTest + testSentenceList.size();
		    
		    // Check sentences' lists
		    totalFiles++;
		    if (testSentences == gsSentences.length)
		    {
		    	// Test and Gold Standard have the same length. High probability of guess.
		    	totalCorrect++;
		    	
		    	if (gsSentenceList.equals(testSentenceList))
		    	{
		    		// Test and Gold Standard are equal, full guess.
		    		totalCorrectSentencesIncluded++;
		    		totalSentencesCorrect = totalSentencesCorrect + testSentenceList.size();
		    	}
		    	else
		    	{
		    		// Test and Gold Standard are not equal.
		    		totalWrong++;
		    		
		    		// TODO
		    		// Check the number of sentences that match with the gold Standard.
		    		// (We didn't reach this situation when creating and evaluating the model).
		    	}
		    }
		    else
		    {
		    	// Test and Gold Standard do not have the same length.
		    	totalWrong++;
		    	System.out.println("Document " + name + " splitting does not match: " + testSentences + " vs " + gsSentences.length);
		    	
		    	// Compare the number of sentences in the Gold Standard and test set.
		    	if (testSentences > gsSentences.length)
			    {
		    		more++;
			    }
		    	else if (testSentences < gsSentences.length) 
		    	{
		    		less++;
		    	}
		    	
		    	// Check the number of sentences that match with the gold Standard.
		    	// Variables "j" and "k" indicate the positions of the current sentences we are analyzing.
		    	// "j" for the test set position.
		    	// "k" for the gold standard position.
		    	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))
		    		{
		    			// We found the same sentence in test and GS
		    			totalSentencesCorrect++;
		    			k++;
		    		}
		    		else
		    		{
		    			// Sentences don't match in test and GS
		    			/*
		    			 * It could happen that the sentences in the test set were not correctly splitted,
		    			 * or they splitted when there was no need for it. 
		    			 * 
		    			 * In this algorithm, we consider that the correct Gold sentence could be inside the test sentence if the 
		    			 * test sentence is longer. Or the test sentence could be inside the Gold sentence if the Gold one is longer.
		    			 * 
		    			 * The following algorithm checks the next sentences after the failed ones, and update the positions after detecting the
		    			 * correct ones.  
		    			 */
		    			
		    			totalSentencesWrong++;
		    			System.out.println("\t" + testSentence);
		    			
		    			// In case we reached the end of the document, and the last sentences don't match, end the loop.
		    			if (k+1 == gsSentenceList.size() || j+1 == testSentenceList.size())
		    			{
		    				break;
		    			}

		    			if (testSentence.contains(gsSentence))
		    			{
		    				/*
		    				 * The sentence in the testing set is longer than the gold sentence, the gold sentence is part of the test sentence.
		    				 */
		    				int before = k;
		    				k++;
		    				String nextTest = testSentenceList.get(j+1);	// Get the next test sentence.
		    				String nextGS = gsSentenceList.get(k);			// Get the next gold sentence.
		    				
		    				boolean end = false;
		    				// Check the next sentences in the gold standard and stop when we meet the sentence that matches the one with the test.
		    				while (!nextTest.equals(nextGS))		    				
		    				{
		    					k++;		    					
		    					try
		    					{
		    						nextGS = gsSentenceList.get(k);
		    					}
		    					catch (Exception e)
		    					{
		    						// We didn't reach the sentence we were looking for, continue normally without updating the positions.
		    						end = true;
		    						break;
		    					}
		    				}
		    				if (end)
		    				{
		    					k = before;
		    				}
		    			}
		    			else if (gsSentence.contains(testSentence))
		    			{
		    				/*
		    				 * The sentence in the gold standard is longer than the test sentence, the test sentence is part of the gold sentence.
		    				 */
		    				int before = j;
		    				j++;
		    				String nextTest = testSentenceList.get(j);	// Get the next test sentence.
		    				String nextGS = gsSentenceList.get(k+1);	// Get the next gold sentence.
		    				
		    				boolean end = false;
		    				// Check the next sentences in the test set and stop when we meet the sentence that matches the one with the gold standard.
		    				while (!nextTest.equals(nextGS))
		    				{
		    					j++;
		    					try
		    					{
		    						nextTest = testSentenceList.get(j);
		    					}
		    					catch (Exception e)
		    					{
		    						// We didn't reach the sentence we were looking for, continue normally without updating the positions.
		    						end = true;
		    						break;
		    					}
		    				}
		    				if (!end)
		    				{
		    					j--;
		    				}
		    				else
		    				{
		    					j = before;
		    				}
		    				k++;
		    			}
		    			else
		    			{ 		
		    				/*
		    				 * Gold and test sentences do not match and are not part of each other.
		    				 * Depending on the number of sentences of test and gold, update the positions of current sentences for 
		    				 * the set with more sentences.
		    				 */
		    				if (testSentenceList.size() > gsSentenceList.size())
		    				{
		    					j++;
		    				}
		    				else if (testSentenceList.size() < gsSentenceList.size()) 
		    				{
		    					k++;
		    					j--;
		    				}
		    				else
		    				{
		    					k++;
		    				}
		    			}
		    		}
		    	}
		    }
		}
		
		
		/*
		 * Display the evaluation results for document level and sentence level.
		 */
		
		// The following lines show the number of documents that have equal sentence splitting results.
		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);
		
		// The following lines show the number of sentences that have been correctly splitted,
		// together with the Precision, Recall and F-Measure.
		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);
	}

}