-
Notifications
You must be signed in to change notification settings - Fork 19
/
LinDekNeighbors.java
449 lines (428 loc) · 14 KB
/
LinDekNeighbors.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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
/*******************************************************************************
* Copyright (c) 2011 Dipanjan Das
* Language Technologies Institute,
* Carnegie Mellon University,
* All Rights Reserved.
*
* LinDekNeighbors.java is part of SEMAFOR 2.0.
*
* SEMAFOR 2.0 is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* SEMAFOR 2.0 is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with SEMAFOR 2.0. If not, see <http://www.gnu.org/licenses/>.
******************************************************************************/
package edu.cmu.cs.lti.ark.fn.identification;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.StringTokenizer;
import edu.cmu.cs.lti.ark.fn.data.prep.ParsePreparation;
import edu.cmu.cs.lti.ark.fn.wordnet.WordNetRelations;
import edu.cmu.cs.lti.ark.util.ds.Pair;
import gnu.trove.THashSet;
import gnu.trove.TObjectIntHashMap;
public class LinDekNeighbors {
public static final int MAX_NEIGHBORS = 20;
public static void main(String[] args) {
Pair<TObjectIntHashMap<String>, TObjectIntHashMap<String>> p =
readAdjectivesAndAdverbs();
TObjectIntHashMap<String> adjectives = p.getFirst();
TObjectIntHashMap<String> adverbs = p.getSecond();
System.out.println("Number of adjectives:" + adjectives.size());
System.out.println("Number of adverbs:" + adverbs.size());
try {
//createNeighborsForAdverbsAndAdjectives(p);
//createNeighborsForNouns();
createNeighborsForVerbs();
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
}
public static void createNeighborsForVerbs()
throws IOException {
String stopfile = "lrdata/stopwords.txt";
String wnConfigFile = "file_properties.xml";
WordNetRelations wnr = new WordNetRelations(stopfile, wnConfigFile);
System.out.println(wnr.getLemmaForWord("worse", "J"));
if (true)
System.exit(-1);
String lindekdirectory = "/home/dipanjan/work/fall2010/SSL/FNData";
String outFile = "/home/dipanjan/work/fall2010/SSL/FNData/lindekneighbors.dat";
BufferedWriter bWriter = new BufferedWriter(new FileWriter(outFile, true));
String line;
BufferedReader bReader = new BufferedReader(new FileReader(lindekdirectory + "/simV.lsp"));
line = bReader.readLine();
ArrayList<String> lines = new ArrayList<String>();
while (line != null) {
line = line.trim();
lines.clear();
while (!line.equals("))")) {
lines.add(line);
line = bReader.readLine();
}
String firstLine = lines.get(0);
firstLine = firstLine.substring(1);
int ind = firstLine.indexOf("(desc");
firstLine = firstLine.substring(0, ind).trim();
String pos = null;
// multiword case
boolean isAdjective = false;
boolean isAdverb = false;
int knn = 0;
String outline = "";
for (int i = 1; i < lines.size() ; i ++) {
StringTokenizer st = new StringTokenizer(lines.get(i).trim(), " \t", true);
ArrayList<String> toks = new ArrayList<String>();
while (st.hasMoreTokens()) {
toks.add(st.nextToken());
}
double value = new Double(toks.get(toks.size()-1));
String unit = "";
for (int j = 0; j < toks.size()-2; j++) {
unit += toks.get(j);
}
if (unit.startsWith("\"")) {
if (!unit.endsWith("\"")) {
System.out.println("Problem with unit:" + unit);
System.exit(-1);
}
unit = unit.substring(1, unit.length()-1).toLowerCase();
} else {
String lc = unit.toLowerCase();
lc = wnr.getLemmaForWord(lc, "V");
unit = lc;
}
outline += unit + ".v\t" + value +"\t";
knn++;
if (knn > MAX_NEIGHBORS) {
break;
}
}
outline = outline.trim();
if (firstLine.startsWith("\"")) {
if (!firstLine.endsWith("\"")) {
System.out.println("Problem with unit:" + firstLine);
System.exit(-1);
}
firstLine = firstLine.substring(1, firstLine.length()-1).toLowerCase();
} else {
String lc = firstLine.toLowerCase();
firstLine = wnr.getLemmaForWord(lc, "V");
}
bWriter.write(firstLine + ".v\t" + outline + "\n");
line = bReader.readLine();
}
bReader.close();
bWriter.close();
}
public static void createNeighborsForNouns()
throws IOException {
String stopfile = "lrdata/stopwords.txt";
String wnConfigFile = "file_properties.xml";
WordNetRelations wnr = new WordNetRelations(stopfile, wnConfigFile);
String lindekdirectory = "/home/dipanjan/work/fall2010/SSL/FNData";
String outFile = "/home/dipanjan/work/fall2010/SSL/FNData/lindekneighbors.dat";
BufferedWriter bWriter = new BufferedWriter(new FileWriter(outFile, true));
String line;
BufferedReader bReader = new BufferedReader(new FileReader(lindekdirectory + "/simN.lsp"));
line = bReader.readLine();
ArrayList<String> lines = new ArrayList<String>();
while (line != null) {
line = line.trim();
lines.clear();
while (!line.equals("))")) {
lines.add(line);
line = bReader.readLine();
}
String firstLine = lines.get(0);
firstLine = firstLine.substring(1);
int ind = firstLine.indexOf("(desc");
firstLine = firstLine.substring(0, ind).trim();
String pos = null;
// multiword case
boolean isAdjective = false;
boolean isAdverb = false;
int knn = 0;
String outline = "";
for (int i = 1; i < lines.size() ; i ++) {
StringTokenizer st = new StringTokenizer(lines.get(i).trim(), " \t", true);
ArrayList<String> toks = new ArrayList<String>();
while (st.hasMoreTokens()) {
toks.add(st.nextToken());
}
double value = new Double(toks.get(toks.size()-1));
String unit = "";
for (int j = 0; j < toks.size()-2; j++) {
unit += toks.get(j);
}
if (unit.startsWith("\"")) {
if (!unit.endsWith("\"")) {
System.out.println("Problem with unit:" + unit);
System.exit(-1);
}
unit = unit.substring(1, unit.length()-1).toLowerCase();
} else {
String lc = unit.toLowerCase();
lc = wnr.getLemmaForWord(lc, "N");
unit = lc;
}
outline += unit + ".n\t" + value +"\t";
knn++;
if (knn > MAX_NEIGHBORS) {
break;
}
}
outline = outline.trim();
if (firstLine.startsWith("\"")) {
if (!firstLine.endsWith("\"")) {
System.out.println("Problem with unit:" + firstLine);
System.exit(-1);
}
firstLine = firstLine.substring(1, firstLine.length()-1).toLowerCase();
} else {
String lc = firstLine.toLowerCase();
firstLine = wnr.getLemmaForWord(lc, "N");
}
bWriter.write(firstLine + ".n\t" + outline + "\n");
line = bReader.readLine();
}
bReader.close();
bWriter.close();
}
public static void createNeighborsForAdverbsAndAdjectives(Pair<TObjectIntHashMap<String>,
TObjectIntHashMap<String>> p)
throws IOException {
String lindekdirectory = "/home/dipanjan/work/fall2010/SSL/FNData";
String outFile = "/home/dipanjan/work/fall2010/SSL/FNData/lindekneighbors.dat";
BufferedWriter bWriter = new BufferedWriter(new FileWriter(outFile));
String line;
ArrayList<String> mAdjectives = ParsePreparation.readSentencesFromFile(lindekdirectory + "/mult.word.j");
THashSet<String> adjSet = new THashSet<String>();
for (int i = 0; i < mAdjectives.size(); i++) {
String w = mAdjectives.get(i).toLowerCase();
adjSet.add(w);
}
ArrayList<String> mAdverbs = ParsePreparation.readSentencesFromFile(lindekdirectory + "/mult.word.r");
THashSet<String> advSet = new THashSet<String>();
for (int i = 0; i < mAdverbs.size(); i++) {
String w = mAdverbs.get(i).toLowerCase();
advSet.add(w);
}
TObjectIntHashMap<String> dAdjectives = p.getFirst();
TObjectIntHashMap<String> dAdverbs = p.getSecond();
// adverbs and adjectives;
BufferedReader bReader = new BufferedReader(new FileReader(lindekdirectory + "/simA.lsp"));
line = bReader.readLine();
ArrayList<String> lines = new ArrayList<String>();
while (line != null) {
line = line.trim();
lines.clear();
while (!line.equals("))")) {
lines.add(line);
line = bReader.readLine();
}
String firstLine = lines.get(0);
firstLine = firstLine.substring(1);
int ind = firstLine.indexOf("(desc");
firstLine = firstLine.substring(0, ind).trim();
// multiword case
boolean isAdjective = false;
boolean isAdverb = false;
int knn = 0;
String outline = "";
for (int i = 1; i < lines.size() ; i ++) {
StringTokenizer st = new StringTokenizer(lines.get(i).trim(), " \t", true);
ArrayList<String> toks = new ArrayList<String>();
while (st.hasMoreTokens()) {
toks.add(st.nextToken());
}
double value = new Double(toks.get(toks.size()-1));
String unit = "";
for (int j = 0; j < toks.size()-2; j++) {
unit += toks.get(j);
}
if (unit.startsWith("\"")) {
if (!unit.endsWith("\"")) {
System.out.println("Problem with unit:" + unit);
System.exit(-1);
}
unit = unit.substring(1, unit.length()-1).toLowerCase();
isAdjective = isMultiWordAdjective(adjSet, unit);
isAdverb = isMultiWordAdverb(advSet, unit);
} else {
String lc = unit.toLowerCase();
int wpos = findWordPOS(dAdjectives,
dAdverbs,
lc);
if (wpos == 1) {
isAdjective = true;
} else if (wpos == 2) {
isAdverb = true;
} else {
isAdjective = true;
isAdverb = true;
}
unit = lc;
}
if (isAdjective)
outline += unit + ".a\t" + value +"\t";
if (isAdverb)
outline += unit + ".adv\t" + value +"\t";
knn++;
if (knn > MAX_NEIGHBORS) {
break;
}
}
outline = outline.trim();
if (firstLine.startsWith("\"")) {
if (!firstLine.endsWith("\"")) {
System.out.println("Problem with unit:" + firstLine);
System.exit(-1);
}
firstLine = firstLine.substring(1, firstLine.length()-1).toLowerCase();
isAdjective = isMultiWordAdjective(adjSet, firstLine);
isAdverb = isMultiWordAdverb(advSet, firstLine);
} else {
String lc = firstLine.toLowerCase();
int wpos = findWordPOS(dAdjectives,
dAdverbs,
lc);
if (wpos == 1) {
isAdjective = true;
} else if (wpos == 2) {
isAdverb = true;
} else {
isAdjective = true;
isAdverb = true;
}
firstLine = lc;
}
if (isAdjective) {
bWriter.write(firstLine + ".a\t" + outline + "\n");
}
if (isAdverb) {
bWriter.write(firstLine + ".adv\t" + outline + "\n");
}
line = bReader.readLine();
}
bReader.close();
bWriter.close();
}
public static boolean isMultiWordAdjective(THashSet<String> mAdjectives,
String word) {
return mAdjectives.contains(word);
}
public static boolean isMultiWordAdverb(THashSet<String> mAdverbs,
String word) {
return mAdverbs.contains(word);
}
// 1 : adjective
// 2: adverb
// 3: both
public static int findWordPOS(TObjectIntHashMap<String> dAdjectives,
TObjectIntHashMap<String> dAdverbs,
String word) {
int adjCount = dAdjectives.get(word);
int advCount = dAdverbs.get(word);
if (adjCount == 0 && advCount == 0) {
if (word.endsWith("ly"))
return 2;
else {
return 3;
}
}
int total = adjCount + advCount;
double adjProb = (double)adjCount / (double) total;
double advProb = (double)advCount / (double) total;
if (Math.abs(adjProb - advProb) < 0.2) {
return 3;
} else {
if (adjProb > advProb) {
return 1;
} else {
return 2;
}
}
}
public static Pair<TObjectIntHashMap<String>, TObjectIntHashMap<String>>
readAdjectivesAndAdverbs() {
String adjFile = "/home/dipanjan/work/fall2010/SSL/FNData/gw.a";
String advFile = "/home/dipanjan/work/fall2010/SSL/FNData/gw.adv";
ArrayList<String> adjectives =
ParsePreparation.readSentencesFromFile(adjFile);
ArrayList<String> adverbs =
ParsePreparation.readSentencesFromFile(advFile);
TObjectIntHashMap<String> adjMap =
new TObjectIntHashMap<String>();
TObjectIntHashMap<String> advMap =
new TObjectIntHashMap<String>();
for (String string: adjectives) {
String[] toks = string.trim().split("\t");
adjMap.put(toks[0], new Integer(toks[1]));
}
for (String string: adverbs) {
String[] toks = string.trim().split("\t");
try {
advMap.put(toks[0], new Integer(toks[1]));
} catch (Exception e) {
System.out.println(string + "\n\n");
e.printStackTrace();
System.exit(-1);
}
}
return new Pair<TObjectIntHashMap<String>, TObjectIntHashMap<String>>(adjMap, advMap);
}
public static Pair<TObjectIntHashMap<String>, TObjectIntHashMap<String>>
scanAdjectivesAndAdverbs() {
String largeFile =
"/home/dipanjan/work/fall2010/SSL/FNData/AP_1m.all.lemma.tags";
TObjectIntHashMap<String> adjectives = new TObjectIntHashMap<String>();
TObjectIntHashMap<String> adverbs = new TObjectIntHashMap<String>();
try {
BufferedReader bReader = new BufferedReader(new FileReader(largeFile));
String line = null;
int count = 0;
while ((line = bReader.readLine()) != null) {
line = line.trim();
String[] toks = line.split("\t");
int numTokens = new Integer(toks[0]);
for (int i = 0; i < numTokens; i++) {
String pos = toks[1 + numTokens + i];
if (pos.startsWith("J")) {
int c = adjectives.get(toks[1 + i].toLowerCase());
adjectives.put(toks[1 + i].toLowerCase(), c+1);
} else if (pos.startsWith("RB")) {
int c = adverbs.get(toks[1 + i].toLowerCase());
adverbs.put(toks[1 + i].toLowerCase(), c+1);
}
}
count++;
if (count % 1000 == 0) {
System.out.print(". ");
}
if (count % 10000 == 0) {
System.out.println(count);
}
// if (count > 1000)
// break;
}
bReader.close();
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
return new Pair<TObjectIntHashMap<String>, TObjectIntHashMap<String>>(adjectives, adverbs);
}
}