-
Notifications
You must be signed in to change notification settings - Fork 0
/
Graph.java
150 lines (138 loc) · 4.48 KB
/
Graph.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
package ngordnet.main;
import java.io.*;
import java.util.*;
public class Graph {
private List<List<String>> nodes; // synsets
private HashMap<Integer, List<Integer>> wordnet; // graph
public Graph(String hyponyms, String synsets) {
addNodes(synsets);
addEdge(hyponyms);
}
/*
add nodes to the graph, using adjacent list. the index of list is the identity of nodes.
*/
private void addNodes(String synsets) {
int lineNum = getFileLineNum(synsets);
nodes = new ArrayList<>(lineNum);
for (int i = 0; i < lineNum; i++) {
nodes.add(new ArrayList<>());
}
try {
BufferedReader reader = new BufferedReader(new FileReader(synsets));
for (int i = 0; i < lineNum; i++) {
String line = reader.readLine();
String[] words = line.split(",")[1].split(" ");
for (String word : words) {
nodes.get(i).add(word);
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/*
add edges among nodes
*/
private void addEdge(String hyponyms) {
this.wordnet = new HashMap<>();
try {
BufferedReader reader = new BufferedReader(new FileReader(hyponyms));
String line;
while ((line = reader.readLine()) != null) {
String[] item = line.split(",");
int parent = Integer.parseInt(item[0]);
List<Integer> children = new ArrayList<>();
for (int i = 1; i < item.length; i++) {
children.add(Integer.valueOf(item[i]));
}
if (wordnet.containsKey(parent)) {
wordnet.get(parent).addAll(children);
} else {
wordnet.put(parent, children);
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/*
find all synsets that contain the given word
*/
public List<Integer> findSynsets(String word) {
List<Integer> parents = new ArrayList<>();
for (int i = 0; i < nodes.size(); i++) {
if (nodes.get(i).contains(word)) {
parents.add(i);
}
}
return parents;
}
/*
find hyponyms with the given word
*/
public Set<String> findHyponymsOfWord(String word) {
List<Integer> wordSynsets = findSynsets(word);
Set<Integer> synsets = new HashSet<>();
for (int synset : wordSynsets) {
synsets.addAll(findChildren(synset));
}
Set<String> words = new TreeSet<>();
for (int synset : synsets) {
words.addAll(this.nodes.get(synset));
}
return words;
}
/*
find hyponyms with the given words
*/
public Set<String> findHyponyms(List<String> list) {
if (list.isEmpty()) {
return null;
}
int index = 0;
String word = list.get(0);
index++;
Set<String> commonHyponyms = findHyponymsOfWord(word);
while (index < list.size()) {
String otherWord = list.get(index);
index++;
Set<String> otherHyponyms = findHyponymsOfWord(otherWord);
commonHyponyms.retainAll(otherHyponyms);
}
return commonHyponyms;
}
/*
find all the children of the given node recursively, including the node itself
*/
public Set<Integer> findChildren(Integer node) {
Set<Integer> children = new HashSet<>();
findChildrenHelper(node, children);
children.add(node);
return children;
}
/*
help function of findChildren
*/
public void findChildrenHelper(Integer node, Set<Integer> children) {
if (!wordnet.containsKey(node)) {
children.add(node);
}
if (wordnet.containsKey(node)) {
for (Integer child : wordnet.get(node)) {
findChildrenHelper(child, children);
children.add(child);
}
}
}
/*
return the number of lines of the given file
*/
public static int getFileLineNum(String filePath) {
try (LineNumberReader lineNumberReader = new LineNumberReader(new FileReader(filePath))) {
lineNumberReader.skip(Long.MAX_VALUE);
return lineNumberReader.getLineNumber();
} catch (IOException e) {
return -1;
}
}
}