-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWordNet.java
183 lines (154 loc) · 5.66 KB
/
WordNet.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
//package wordNet;
import java.util.ArrayList;
//import edu.princeton.cs.algs4.Digraph;
//import edu.princeton.cs.algs4.DirectedCycle;
//import edu.princeton.cs.algs4.Queue;
//import edu.princeton.cs.introcs.In;
//import edu.princeton.cs.introcs.StdOut;
public class WordNet {
private Digraph wordGraph;
private int V;
private ArrayList<Integer> id;
private ArrayList<String> noun;
// constructor takes the name of the two input files
public WordNet(String synsets, String hypernyms)
{
//read hypernyms.txt
this.V = 0;
ArrayList<Integer> s = new ArrayList<Integer>();
ArrayList<Integer> d = new ArrayList<Integer>();
In h = new In(hypernyms);
while (h.hasNextLine()) {
// 1,0,2
String line = h.readLine();
String[] line1 = line.split(",");
for (int i=1;i<line1.length;i++) {
s.add(Integer.parseInt(line1[0]));
d.add(Integer.parseInt(line1[i]));
if (Integer.parseInt(line1[0]) > this.V) this.V = Integer.parseInt(line1[0]);
if (Integer.parseInt(line1[1]) > this.V) this.V = Integer.parseInt(line1[1]);
}
}
wordGraph = new Digraph(this.V+1); //construct V points directed graph
for (int j = 0;j<s.size();j++) {
wordGraph.addEdge(s.get(j), d.get(j));
}
//check for multi root
int rootNum = 0;
for (int i=0;i<wordGraph.V();i++) {
if (! wordGraph.adj(1).iterator().hasNext()) rootNum++;
}
if (rootNum >=2) throw new java.lang.IllegalArgumentException();
//check for cycle
DirectedCycle dc = new DirectedCycle(wordGraph);
if (dc.hasCycle()) throw new java.lang.IllegalArgumentException();
//read synsets.txt;
this.id = new ArrayList<Integer>();
this.noun = new ArrayList<String>();
In syn = new In(synsets);
while (syn.hasNextLine()) {
String line_syn = syn.readLine();
String[] line_syn1 = line_syn.split(",");
String[] nouns = line_syn1[1].split(" ");
for (int k=0;k<nouns.length;k++) {
this.id.add(Integer.parseInt(line_syn1[0]));
this.noun.add(nouns[k]);
//System.out.println(Integer.parseInt(line_syn1[0])+"->"+nouns[k]);
}
}
//System.out.println(this.id.size());
//System.out.println(this.noun.size());
}
// returns all WordNet nouns
public Iterable<String> nouns()
{
Queue<String> q = new Queue<String>();
for (int i = 0;i<this.noun.size();i++)
{
q.enqueue(this.noun.get(i));
}
return q;
}
// is the word a WordNet noun?
public boolean isNoun(String word)
{
boolean flag = false;
for (int i = 0;i<this.noun.size();i++)
{
if (this.noun.get(i).equals(word))
flag = true;
}
return flag;
}
// distance between nounA and nounB (defined below)
public int distance(String nounA, String nounB)
{
SAP sap = new SAP(this.wordGraph);
if (!isNoun(nounA) | !isNoun(nounB)) {
throw new IllegalArgumentException();
}
return sap.length(getID(nounA), getID(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 (!isNoun(nounA) | !isNoun(nounB)) {
throw new IllegalArgumentException();
}
SAP sap = new SAP(this.wordGraph);
//Queue<String> nameQueue = new Queue<String>();
//String name = null;
StringBuilder name = new StringBuilder();
int id = sap.ancestor(getID(nounA), getID(nounB));
//System.out.println("id= "+id);
for (int i=0;i<this.id.size();i++) {
if (this.id.get(i) == id) {
//nameQueue.enqueue(this.noun.get(i));
name.append(this.noun.get(i));
name.append(" ");
}
}
//for (String one : nameQueue) {
// name = one+" "+name;
// }
//StringBuilder s = new StringBuilder();
//s.append("xyz");
//s.append("abc");
//System.out.println(s);
return name.toString();
}
private Iterable<Integer> getID (String in)
{
boolean flag=false;
Queue<Integer> qId = new Queue<Integer>();
for (int i = 0;i<this.noun.size();i++)
if (this.noun.get(i).equals(in)) {
flag = true;
qId.enqueue(this.id.get(i));
}
if (flag) return qId;
else return null;
}
// do unit testing of this class
public static void main(String[] args)
{
// WordNet wd = new WordNet("synsets.txt", "hypernyms.txt");
// WordNet wd = new WordNet("synsets3.txt", "hypernyms3InvalidCycle.txt");
// WordNet wd = new WordNet("synsets3.txt", "hypernyms3InvalidTwoRoots.txt");
WordNet wd = new WordNet("synsets15.txt", "hypernyms15Tree.txt");
StdOut.println(wd.distance("a","invalid"));
//StdOut.println(wd.isNoun("worm"));
//StdOut.println(wd.getID("worm"));
//StdOut.println(wd.distance("white_marlin", "mileage"));
//StdOut.println(wd.distance("catafalque", "hardcover"));
//StdOut.println(wd.sap("catafalque", "hardcover"));
//StdOut.println(wd.distance("American_water_spaniel", "histology"));
//StdOut.println(wd.distance("Brown_Swiss", "barrel_roll"));
//StdOut.println(wd.distance("phosphoprotein", "electric_doublet"));
//StdOut.println(wd.sap("phosphoprotein", "electric_doublet"));
//
//WordNet wd = new WordNet("synsets15.txt", "hypernyms15Path.txt");
//StdOut.println(wd.distance("a", "o"));
}
}