-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPoemCondense.java
55 lines (46 loc) · 1.39 KB
/
PoemCondense.java
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
import java.io.*;
import java.util.*;
import java.util.regex.*;
public class PoemCondense {
public double condenseScore = 0;
//constructor
public PoemCondense(String format, int poemInd) {
BufferedReader reader = new BufferedReader(new StringReader(format));
// System.out.println("Entered Condense");
try {
String line = reader.readLine();
// System.out.println("1taken line: "+line);
int lineNum = 0;
while(line != null) {
if(line.compareTo("")==0) {
while(line.compareTo("")==0) {
line = reader.readLine();
}
}
condenseScore += GetCondenseScores(line);
line = reader.readLine();
lineNum ++;
}
condenseScore /= (double)lineNum;
} catch (IOException e) {
System.out.println("Format cannot be read");
e.printStackTrace();
}
System.out.println(condenseScore);
}
//Get the condensation score of one line
public double GetCondenseScores(String line) {
String[] sections = line.split("\\|");
int sectionSize = sections.length;
Pattern pattern = Pattern.compile("(\\([^\\+|\\-|\\(]+\\))");
int countMeaningNum = 0;
for (int i =0; i<sections.length; i++) {
Matcher matcherTag = pattern.matcher(sections[i]);
if(matcherTag.find()) { //as long as we find one!
// System.out.println(matcherTag.group(1));
countMeaningNum++;
}
}
return ((double)countMeaningNum/(double)sectionSize);
}
}