-
Notifications
You must be signed in to change notification settings - Fork 0
/
HyponymsHandler.java
100 lines (94 loc) · 3.29 KB
/
HyponymsHandler.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
package ngordnet.main;
import ngordnet.browser.NgordnetQuery;
import ngordnet.browser.NgordnetQueryHandler;
import ngordnet.ngrams.NGramMap;
import ngordnet.ngrams.TimeSeries;
import java.util.*;
public class HyponymsHandler extends NgordnetQueryHandler {
private WordNet wm;
private NGramMap map;
public HyponymsHandler(WordNet wn, NGramMap ngm) {
this.wm = wn;
this.map = ngm;
}
@Override
public String handle(NgordnetQuery q) {
List<String> words = q.words();
int startYear = q.startYear();
int endYear = q.endYear();
int k = q.k();
Set<String> wordset = wm.findHyponyms(words);
if (words.isEmpty()) {
return " ";
}
if (k == 0) {
return wordset.toString();
} else {
Map<String, Double> count = new HashMap<>();
for (String word : wordset) {
TimeSeries ts = map.countHistory(word, startYear, endYear);
if (ts.size() != 0) {
double num = 0;
for (double val : ts.values()) {
num += val;
}
count.put(word, num);
}
}
if (count.size() <= k) {
List<String> keys = new ArrayList<>();
List<String> keys2 = new ArrayList<>();
List<String> ret = new ArrayList<>();
for (String key : count.keySet()) {
keys.add(key);
keys2.add(key);
}
for (String key2 : keys2) {
String min = keys.get(0);
for (String key : keys) {
if (key.compareTo(min) < 0) {
min = key;
}
}
ret.add(min);
keys.remove(min);
}
return ret.toString();
} else {
Set<String> kWords = new HashSet<>();
for (int i = 0; i < k; i++) {
double max = 0;
String maxWord = "";
for (String word : wordset) {
if (count.containsKey(word)) {
if (count.get(word) > max) {
max = count.get(word);
maxWord = word;
}
}
}
kWords.add(maxWord);
count.remove(maxWord);
}
List<String> keys = new ArrayList<>();
List<String> keys2 = new ArrayList<>();
List<String> ret = new ArrayList<>();
for (String key : kWords) {
keys.add(key);
keys2.add(key);
}
for (String key2 : keys2) {
String min = keys.get(0);
for (String key : keys) {
if (key.compareTo(min) < 0) {
min = key;
}
}
ret.add(min);
keys.remove(min);
}
return ret.toString();
}
}
}
}