-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCausesChallengeAutomaton.java
75 lines (51 loc) · 2.02 KB
/
CausesChallengeAutomaton.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
import java.util.Collection;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.List;
import java.io.File;
import com.infiauto.datastr.auto.DictionaryAutomaton;
import com.infiauto.datastr.auto.LevenshteinAutomaton;
class CausesChallengeAutomaton {
public static void main(String[] args) throws Exception {
Scanner s = new Scanner(new File(args[0]));
ArrayList<String> wordList = new ArrayList<String>();
wordList.ensureCapacity(270000);
while (s.hasNext()) {
wordList.add(s.next());
}
s.close();
WordNetwork wordNetwork = new WordNetwork(wordList);
System.out.println(
wordNetwork.discoverNetwork("causes").size()
);
}
}
class WordNetwork {
private DictionaryAutomaton dictionary;
private LevenshteinAutomaton automaton;
static public final int FRIENDSHIP = 1;
public WordNetwork(List<String> words) throws Exception {
dictionary = new DictionaryAutomaton(words);
automaton = new LevenshteinAutomaton(FRIENDSHIP);
}
public ArrayList<String> discoverNetwork(String word) throws Exception {
ArrayList<String> network = new ArrayList<String>();
ArrayList<String> buffer = new ArrayList<String>();
network.ensureCapacity(80000);
buffer.ensureCapacity(40000);
buffer.add(word);
String friend;
while (!buffer.isEmpty()) {
friend = buffer.get(0);
buffer.remove(0);
if (network.contains(friend)) continue;
buffer.addAll(discoverFriends(friend));
network.add(friend);
System.out.println(network.size());
}
return network;
}
public Collection<String> discoverFriends(String word) throws Exception {
return automaton.recognize(word, dictionary);
}
}