-
Notifications
You must be signed in to change notification settings - Fork 0
/
SentencePair.java
340 lines (274 loc) · 10.2 KB
/
SentencePair.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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/*
* Builds and stores an unlabeled sentence pair (input for our system).
*
* Copyright (C) 2013 Lisa Vitolo <[email protected]>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the Creative Commons
* Attribution-NonCommercial-ShareAlike 3.0 license.
* You should have received a copy of the license with this product.
* Otherwise, visit http://creativecommons.org/licenses/by-nc-sa/3.0/
*/
import java.util.List;
import java.util.LinkedList;
import java.util.ListIterator;
import edu.stanford.nlp.ling.CoreAnnotations;
import edu.stanford.nlp.ling.CoreLabel;
import edu.stanford.nlp.pipeline.Annotation;
import edu.stanford.nlp.pipeline.StanfordCoreNLP;
import edu.stanford.nlp.util.CoreMap;
import java.util.Iterator;
public class SentencePair
{
public List<POSTaggedToken> s1;
public List<POSTaggedToken> s2;
private StanfordCoreNLP nlp;
/*
* This builds the sentences from partial information. For the tweet question we already have tokens,
* POS tags and lemmas, while we use the Stanford CoreNLP parser for the question extracted from
* Q&A databases.
*
* NOTE: lemmatization must be repeated after preprocessing the sentences, because preprocessing may
* add, remove or alter tokens in the sentence.
*/
public SentencePair(List<String> s1Tokens, List<String> s1Tags, List<String> s1Lemmas, String s2Text, StanfordCoreNLP nlp)
{
this.nlp = nlp;
this.s1 = new LinkedList<>();
this.s2 = new LinkedList<>();
for (int i = 0; i < s1Tokens.size(); i++) {
POSTaggedToken tt = new POSTaggedToken( s1Tokens.get(i), s1Tags.get(i), s1Lemmas.get(i) );
this.s1.add(tt);
}
createSentence(s2Text, this.s2);
preprocess();
lemmatize(this.s2);
preprocess();
}
/*
* This builds the sentences from raw text. It is used during training, because no additional
* information is available from the training files.
*/
public SentencePair(String text1, String text2, StanfordCoreNLP nlp)
{
this.s1 = new LinkedList<>();
this.s2 = new LinkedList<>();
this.nlp = nlp;
createSentence(text1, this.s1);
createSentence(text2, this.s2);
preprocess();
lemmatize(this.s1);
lemmatize(this.s2);
}
@Override public String toString()
{
String p = "";
for (POSTaggedToken tt : s1) {
p += tt + ", ";
}
p += "\n";
for (POSTaggedToken tt : s2) {
p += tt + ", ";
}
p += "\n";
return p;
}
private void createSentence(String text, List<POSTaggedToken> sentence)
{
Annotation d = new Annotation(text);
nlp.annotate(d);
for (CoreMap ss : d.get(CoreAnnotations.SentencesAnnotation.class)) {
for (CoreLabel token : ss.get(CoreAnnotations.TokensAnnotation.class)) {
sentence.add( new POSTaggedToken(token.toString(), translateTag(token.tag())) );
}
}
}
private void lemmatize(List<POSTaggedToken> sentence)
{
String text = "";
/* Convert the sentence back to a single string */
for (POSTaggedToken tt : sentence) {
text += tt.token + " ";
}
Annotation d = new Annotation(text);
nlp.annotate(d);
for (CoreMap ss : d.get(CoreAnnotations.SentencesAnnotation.class)) {
Iterator<CoreLabel> itToken = ss.get(CoreAnnotations.TokensAnnotation.class).iterator();
ListIterator<POSTaggedToken> itSentence = sentence.listIterator();
while (itToken.hasNext() && itSentence.hasNext()) {
CoreLabel token = itToken.next();
POSTaggedToken tt = itSentence.next();
tt.lemma = token.lemma(); /* add a lemma to the POSTaggedToken */
itSentence.set(tt);
}
}
}
/*
* The Stanford Core NLP uses the Penn Treebank tagset, while for Palmisano the tagset proposed
* by Gimpel et al. is used. Here is the transformation; of course there is a loss of precision due
* to Penn Treebank having a greater number of tags. *
*/
private String translateTag(String tag)
{
if (tag.equals("NN") || tag.equals("NNS")) {
return "N";
}
if (tag.startsWith("V") || tag.equals("MD")) {
return "V";
}
if (tag.equals("NNP") || tag.equals("NNPS")) {
return "^";
}
if (tag.equals("RB") || tag.equals("RBR") || tag.equals("RBS") || tag.equals("WRB")) {
return "R";
}
if (tag.equals("PDT") || tag.equals("X")) {
return "X";
}
if (tag.equals("IN") || tag.equals("TO")) {
return "P";
}
if (tag.equals("CC")) {
return "&";
}
if (tag.equals("CD")) {
return "$";
}
if (tag.startsWith("J")) {
return "A";
}
if (tag.equals("PRP") || tag.equals("WP")) {
return "O";
}
if (tag.equals("RP")) {
return "T";
}
if (tag.equals("UH")) {
return "!";
}
if (tag.equals("DT") || tag.equals("PRP$") || tag.equals("WDT") || tag.equals("WP$")) {
return "D";
}
/* Punctuaction tags are all merged together */
if (tag.equals("$") || tag.equals(",") || tag.equals("(") || tag.equals(")") || tag.equals(".") || tag.equals(":")) {
return ",";
}
/* Generic tag for foreign words or other unrecognizable tokens */
return "G";
}
/*
* Read the report for a complete list of the preprocessing steps applied here.
*/
private void preprocess()
{
preprocessSentence(this.s1);
preprocessSentence(this.s2);
mergeCompounds(this.s1, this.s2);
mergeCompounds(this.s2, this.s1);
}
private void preprocessSentence(List<POSTaggedToken> sentence)
{
for (ListIterator<POSTaggedToken> it = sentence.listIterator(); it.hasNext();) {
POSTaggedToken tt = it.next();
/*
* All the preprocessing steps that are concerned with one token at a time are
* applied here, and they return the modified token, or an empty one if they decided
* to delete it.
*/
tt = stripAngularBrackets(tt);
tt = removeHyphensSlashes(tt);
tt = removeStopWords(tt);
/* One of the steps wants to delete the token */
if (tt.token.isEmpty()) {
it.remove();
} else { /* we assume the token has been changed somehow */
it.set(tt);
}
}
expandVerbAbbreviations(sentence);
}
private POSTaggedToken stripAngularBrackets(POSTaggedToken tt)
{
String token = tt.token;
while (token.startsWith("<") || token.startsWith(">")) {
token = token.substring(1);
}
while (token.endsWith("<") || token.endsWith(">")) {
token = token.substring(0, token.length() - 2);
}
tt.token = token;
return tt;
}
private POSTaggedToken removeHyphensSlashes(POSTaggedToken tt)
{
String s = tt.token;
s = s.replaceAll("-", "");
s = s.replaceAll("/", "");
tt.token = s;
return tt;
}
private POSTaggedToken removeStopWords(POSTaggedToken tt)
{
String[] stopWords = Constants.getStopWords();
for (int i = 0; i < stopWords.length; i++) {
if (stopWords[i].equals( tt.token.toLowerCase() )) {
tt.token = "";
return tt;
}
}
return tt;
}
private void expandVerbAbbreviations(List<POSTaggedToken> sentence)
{
for (ListIterator<POSTaggedToken> it = sentence.listIterator(); it.hasNext();) {
POSTaggedToken tt = it.next();
String string = tt.token;
if (string.endsWith("n't")) {
int index = string.lastIndexOf("n");
string = string.substring(0, index);
if (!string.isEmpty()) {
tt.token = string;
it.set(tt);
}
it.add( new POSTaggedToken("not", "R") );
} else if (string.endsWith("'m")) {
int index = string.lastIndexOf("'");
string = string.substring(0, index);
if (!string.isEmpty()) {
tt.token = string;
it.set(tt);
}
it.add( new POSTaggedToken("am", "V") );
}
}
}
private void mergeCompounds(List<POSTaggedToken> sentence1, List<POSTaggedToken> sentence2)
{
for (ListIterator<POSTaggedToken> it = sentence1.listIterator(); it.hasNext();) {
POSTaggedToken tt1 = it.next();
if (it.hasNext()) {
POSTaggedToken tt2 = it.next();
String compound = tt1.token + tt2.token;
String tag = containsTokenWithTag(sentence2, compound);
if (!tag.isEmpty()) {
it.remove();
tt1.token = compound;
tt1.tag = tag;
it.previous();
it.set(tt1);
} else {
it.previous();
}
}
}
}
private String containsTokenWithTag(List<POSTaggedToken> list, String s)
{
for (POSTaggedToken tt : list) {
if (tt.token.equals(s)) {
return tt.tag;
}
}
return "";
}
}