-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathAnnotationTaskGenerator.java
163 lines (127 loc) · 5.93 KB
/
AnnotationTaskGenerator.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
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
import java.io.*;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Class to create the annotation task for verifying the emotion of top bigrams and a sample sentence.
*
* Created by sebastian on 23/06/15.
*/
public class AnnotationTaskGenerator {
/**
* The directory of the files from which the bigrams should be retrieved.
*/
private static String pmiDir = "/home/sebastian/git/sentiment_analysis/out/scores/pmi/";
/**
* The collocations file containing sentences.
*/
private static String collocationsFilePath = "/home/sebastian/git/sentiment_analysis/out/results_final/collocations.txt";
/**
* The path to the file containing the annotation task.
*/
private static String annotationFilePath = "/home/sebastian/git/sentiment_analysis/out/annotation.txt";
/**
* Number of top ngrams that should be retrieved.
*/
private static int topN = 20;
/**
* Main method to create the annotation task.
* @param args the input arguments
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// the bigrams that should be annotated
Map<String, String> bigramEmotionMap = getBigramsForAnnotation(pmiDir, topN);
File file = new File(annotationFilePath);
file.delete();
String[] collocations = readCollocationsFile(collocationsFilePath);
for (Map.Entry<String, String> entry : bigramEmotionMap.entrySet()) {
String emotion = entry.getValue();
String bigram = entry.getKey().split("\t")[0];
List<String> matchedSentences = matchAgainstCollocations(collocations, emotion, bigram);
String randomCollocation = getRandomMember(matchedSentences);
PrintWriter writer = new PrintWriter(new BufferedWriter(new FileWriter(annotationFilePath, true)));
System.out.printf("%s\t\t\t%s\t\t\n", bigram, randomCollocation);
writer.printf("%s\t\t\t%s\t\t\n", bigram, randomCollocation);
writer.close();
}
}
/**
* Retrieves a random member from a list of string.
* @param list the list of strings
* @return a random member of said list
*/
private static String getRandomMember(List<String> list) {
int randomIdx = (int)(Math.random() * list.size());
return list.get(randomIdx);
}
/**
* Matches a bigram and an emotion against the string array of collocations using a predefined pattern and returns
* the matched sentences.
* @param collocations an array of collocations
* @param emotion the emotion that should be matched
* @param bigram the bigram that should be matched
* @return the matched sentences
*/
private static List<String> matchAgainstCollocations(String[] collocations, String emotion, String bigram) {
List<String> matchedSentences = new ArrayList<String>();
Pattern pattern = Pattern.compile("emotion" + ".*[^a-z]" + emotion + ".*cause.*[^a-z]" + Extensions.join(bigram.replace("$", "\\$").replace("NUM", "NUMBER").toLowerCase().split(" "), "([^a-z].*[^a-z]| )") + "[^a-z].*cause bow");
for (int i = 1; i < collocations.length; i++) {
Matcher m = pattern.matcher(collocations[i]);
if (m.find()) {
matchedSentences.add(collocations[i - 1].split("\t")[1]);
}
}
return matchedSentences;
}
/**
* Read in a collocations file and store it in a String array. Note: The standard collocations file that is 4,641,270
* lines long is assumed.
* @param filePath the file path to the collocations file
* @return a string array containing the collocations file
* @throws IOException
*/
public static String[] readCollocationsFile(String filePath) throws IOException {
String[] collocations = new String[4641271];
BufferedReader reader = new BufferedReader(new FileReader(filePath));
String line = reader.readLine();
int i = 0;
while (line != null && !line.equals("")) {
collocations[i++] = line.toLowerCase();
line = reader.readLine();
}
return collocations;
}
/**
* Return the top n bigrams of NP cause and predicate direct object files in a directory as a map of bigrams + their
* ngram source and their emotions.
* @param dir the directory containing the files
* @param topN the top n bigrams
* @return a map with key: bigram tab ngram type (np_cause|s_cause_pred_dobj); value: emotion
* @throws IOException
*/
public static Map<String, String> getBigramsForAnnotation(String dir, int topN) throws IOException {
Map<String, String> bigramEmotionMap = new HashMap<String, String>();
List<String> fileNames = Utils.getFileNames(dir, "(np_cause|pred_dobj)", "bigram", "\\.txt");
Collections.sort(fileNames);
for (String fileName : fileNames) {
String ngramSource = fileName.contains(Enums.NgramSource.np_cause.toString()) ? Enums.NgramSource.np_cause.toString() : Enums.NgramSource.s_cause_pred_dobj.toString();
int count = 0;
BufferedReader reader = new BufferedReader(new FileReader(Utils.combine(dir, fileName)));
String line = reader.readLine();
while (line != null && !line.equals("") && count < topN && count++ < topN + 1) {
// add ngram source to bigram to be used in evaluation
String bigram = line.split("\t")[0] + "\t" + ngramSource;
String emotion = fileName.split("_")[0];
// don't get bigrams for named entities
if (bigram.contains("/")) {
line = reader.readLine();
continue;
}
bigramEmotionMap.put(bigram, emotion);
line = reader.readLine();
}
}
return bigramEmotionMap;
}
}