1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Scanner;
- import java.util.Date;
- import java.io.File;
-
- class Main {
- public static void main(String[] args) throws Exception {
- BinTree<String> tree = new BinTree<>();
-
- // Insert dictionary into tree
- Scanner dictFile = new Scanner(new File("dictionary.txt"));
- while (dictFile.hasNextLine())
- tree.insert(dictFile.nextLine());
-
- // Remove and add busybody
- tree.remove("busybody");
- tree.insert("busybody");
-
- // User interface
- Scanner in = new Scanner(System.in);
- System.out.print("> ");
- while (in.hasNextLine()) {
- String str = in.nextLine().toLowerCase();
-
- // Quit on q
- if (str.equals("q")) {
- TreeStats.printStats(tree);
- return;
- }
-
- // Word exists
- if (tree.search(str) != null) {
- System.out.println("Word found.");
-
- // Word doesn't exist
- } else {
- long start = System.nanoTime();
-
- List<BinTree<String>.Node> similar = tree.findSimilar(str);
-
- // No matches found
- if (similar.size() == 0) {
- System.out.println("Neither the word nor similar words was found.");
-
- // Matches found, list them
- } else {
- System.out.println("Word not found. Similar words:");
- for (BinTree<String>.Node n: similar) {
- System.out.println("\t"+n.elem);
- }
- }
-
- long end = System.nanoTime();
- System.out.println("");
- System.out.println(similar.size()+" lookups gave a positive answer.");
- System.out.printf("Time spent looking for similar words: %.2f ms\n",
- ((end - start) / 1000000f));
- }
-
- // Pretty prompt
- System.out.print("> ");
- }
- }
- }
|