-
Notifications
You must be signed in to change notification settings - Fork 0
/
UseDictionnary.java
67 lines (52 loc) · 1.85 KB
/
UseDictionnary.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
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.io.File;
class UseDictionnary {
private static String cleanWord(String word){
word = word.toLowerCase();
word = word.replaceAll("[^a-z]","");
return word;
}
private static void loadLine(Trie dictionnary, String line) {
//use a second Scanner to parse the content of each line
Scanner scanner = new Scanner(line);
scanner.useDelimiter(" ");
while (scanner.hasNext()){
String word = cleanWord(scanner.next());
if (word.length() > 0){
dictionnary.insertWord(word);
}
}
}
public static void main(String[] args){
String dictionnaryPath = args[0];
Trie dictionnary = new Trie();
try {
Scanner scanner = new Scanner(new File(dictionnaryPath));
while (scanner.hasNextLine()){
loadLine(dictionnary, scanner.nextLine());
}
}
catch (FileNotFoundException e) {
return;
}
System.out.println("Total number of words in text: "+ dictionnary.getTotalLength());
System.out.println("Count of different words loaded: "+ dictionnary.getLength());
while (true) {
System.out.println("Type Q anytime you want to quit");
System.out.println("Type a word: ");
Scanner sc = new Scanner(System.in);
String word = sc.next();
if (word.equals("Q")){
System.out.println("OK, bye!");
return;
}
if (dictionnary.has(word)){
System.out.println("Yup, " + word + " is in the dictionnary !");
}
else {
System.out.println("Nope, " + word + " is not in the dictionnary !");
}
}
}
}