-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGS.java
76 lines (70 loc) · 2.24 KB
/
GS.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
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
public class GS {
private static class singletonHolder {
private static GS instance = null;
}
private Set<String> words;
private HashMap<String, Set<String>> pairs ;
private HashMap<String, Boolean> values;
public static GS getInstance() {
if(singletonHolder.instance==null)
singletonHolder.instance = new GS();
return singletonHolder.instance;
}
private GS() {
words= new HashSet<>();
pairs= new HashMap<>();
values= new HashMap<>();
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(this.getClass().getResourceAsStream("/word-relatedness.txt")));
String line = reader.readLine();
while (line != null) {
String[] l = line.split("\\s+");
words.add(l[0]);
words.add(l[1]);
pairs.putIfAbsent(l[0], new HashSet<String>());
pairs.get(l[0]).add(l[1]);
pairs.putIfAbsent(l[1], new HashSet<String>());
pairs.get(l[1]).add(l[0]);
boolean val = false;
if (l[2].equals("True")){
val = true;
}
values.putIfAbsent(l[0]+" "+l[1], val);
line = reader.readLine();
}
} catch (IOException e) {
e.printStackTrace();
}
}
protected boolean ContainInWords(String w){
return words.contains(w);
}
protected Set<String> getSetFromPairs(String w){
return pairs.getOrDefault(w, null);
}
protected String getValue(String w1, String w2){
Boolean val;
if(values.containsKey(w1+" "+w2)) {
val = values.get(w1 + " " + w2);
if (val)
return "yes";
else
return "no";
}
else if (values.containsKey(w2+" "+w1)) {
val = values.get(w2 + " " + w1);
if (val)
return "yes";
else
return "no";
}
else
return null;
}
}