forked from gzc/MOOC-Course
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordNet.java
More file actions
130 lines (113 loc) · 3.39 KB
/
Copy pathWordNet.java
File metadata and controls
130 lines (113 loc) · 3.39 KB
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
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class WordNet {
private final Map<String, List<Integer>> words;
private final Map<Integer, String> synset_index;
private final Digraph graph;
private final SAP sap;
private int V = 0;
// constructor takes the name of the two input files
public WordNet(String synsets, String hypernyms){
words = new HashMap<String, List<Integer>>();
synset_index = new HashMap<Integer, String>();
In in1 = new In(synsets);
In in2 = new In(hypernyms);
loaddata(in1);
graph = new Digraph(V);
buildgraph(in2);
sap = new SAP(graph);
DirectedCycle dc = new DirectedCycle(graph);
if(dc.hasCycle())
throw new java.lang.IllegalArgumentException();
int de = 0;
for(int i = 0;i < V;i++)
{
de++;
for(int j : graph.adj(i)) {
de--;
break;
}
}
if(de > 1)
throw new java.lang.IllegalArgumentException();
}
private void loaddata(In in) {
while(in.exists()){
String line = in.readLine();
if(line == null)
break;
String[] fields = line.split("\\,");
int id = Integer.parseInt(fields[0]);
String[] synset = fields[1].split(" ");
for(int i = 0;i < synset.length;i++) {
if(words.get(synset[i]) == null) {
List<Integer> list = new ArrayList<Integer>();
words.put(synset[i], list);
words.get(synset[i]).add(id);
} else {
words.get(synset[i]).add(id);
}
}
V++;
synset_index.put(id, fields[1]);
}
//StdOut.println(words.get("worm").size());
//StdOut.println(words.get("white_marlin").size());
}
private void buildgraph(In in) {
while(in.exists()){
String line = in.readLine();
if(line == null)
break;
String[] fields = line.split("\\,");
int root = Integer.parseInt(fields[0]);
for(int n = 1; n < fields.length; n++) {
int id = Integer.parseInt(fields[n]);
graph.addEdge(root, id);
}
}
}
// returns all WordNet nouns
public Iterable<String> nouns() {
return words.keySet();
}
// is the word a WordNet noun?
public boolean isNoun(String word) {
if(word == null)
throw new java.lang.NullPointerException();
if(words.containsKey(word))
return true;
return false;
}
// distance between nounA and nounB (defined below)
public int distance(String nounA, String nounB) {
if(nounA == null || nounB == null)
throw new java.lang.NullPointerException();
if (!isNoun(nounA) || !isNoun(nounB))
throw new java.lang.IllegalArgumentException();
return sap.length(words.get(nounA), words.get(nounB));
}
// a synset (second field of synsets.txt) that is the common ancestor of nounA and nounB
// in a shortest ancestral path (defined below)
public String sap(String nounA, String nounB) {
if(nounA == null || nounB == null)
throw new java.lang.NullPointerException();
if (!isNoun(nounA) || !isNoun(nounB))
throw new java.lang.IllegalArgumentException();
int ancestor = sap.ancestor(words.get(nounA), words.get(nounB));
return synset_index.get(ancestor);
}
// do unit testing of this class
public static void main(String[] args) {
String filename1 = "synsets3.txt";
String filename2 = "hypernyms3InvalidTwoRoots.txt";
WordNet wordnet = new WordNet(filename1, filename2);
/*String nounA = "b";
String nounB = "c";
int distance = wordnet.distance(nounA, nounB);
String ancestor = wordnet.sap(nounA, nounB);
StdOut.println(distance + ancestor);*/
}
}