-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThesaurus.java
More file actions
114 lines (107 loc) · 3.55 KB
/
Thesaurus.java
File metadata and controls
114 lines (107 loc) · 3.55 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
import org.json.*;
import java.io.StringWriter;
import java.io.PrintWriter;
import java.util.LinkedList;
import java.util.List;
import java.util.Iterator;
import java.util.HashSet;
import java.util.Set;
import java.util.Map;
import java.util.HashMap;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Thesaurus{
public static class NoSuchWordException extends Exception{
public NoSuchWordException(){
super();
}
}
public static class NoPathExistsException extends Exception{
public NoPathExistsException(){
super();
}
}
//Map<String, Wordnode> t;
Trie<Wordnode> t;
String path = "";
public Thesaurus(String path) {
System.out.println("loading json from" + path);
this.path = path;
//this.t = new HashMap<String, Wordnode>();
this.t = new Trie<Wordnode>();
StringBuffer txt = new StringBuffer();
BufferedReader reader = null;
try{
reader = new BufferedReader(new FileReader(path));
String line;
while((line = reader.readLine()) != null){
txt.append(line);
}
JSONObject j = new JSONObject(txt.toString());
Iterator<?> it = j.keys();
while(it.hasNext()){
String k = (String) it.next();
if(!t.containsKey(k)){
t.put(k, new Wordnode(k));
}
Wordnode cur = t.get(k);
JSONArray syns = j.getJSONArray(k);
for(int i = 0; i<syns.length(); i++){
String syn = syns.getString(i);
if(!syn.equals(cur.word)){
if(!t.containsKey(syn)){
t.put(syn, new Wordnode(syn));
}
Wordnode synnode = t.get(syn);
if(!cur.neighbors.contains(synnode))
cur.neighbors.add(synnode);
//only if bidirectional
//if(!synnode.neighbors.contains(cur))
//synnode.neighbors.add(cur);
}
}
}
}
catch (Exception e){
e.printStackTrace();
}
finally{
try{
if (reader != null) reader.close();
}
catch (IOException ex){
ex.printStackTrace();
}
}
}
public List<Wordnode> getSynonyms(String word){
Wordnode node = t.get(word);
return node.neighbors;
}
public List<String> synpath(String start, String end, JSONArray avoids)
throws NoSuchWordException, NoPathExistsException, JSONException{
Wordnode origin = t.get(start);
Wordnode dest = t.get(end);
JSONObject retjson = new JSONObject();
List<String> result = null;
if(origin == null || dest == null){
throw new NoSuchWordException();
}
else{
if(avoids.length() == 0)
result = origin.shortestPath(dest, 0);
else{
Set<Wordnode> avoidsSet = new HashSet<>();
for(int i = 0; i<avoids.length(); i++){
avoidsSet.add(t.get(avoids.getString(i)));
}
result = origin.shortestPath(dest, 0, avoidsSet);
}
if(result == null){
throw new NoPathExistsException();
}
}
return result;
}
}