aboutsummaryrefslogtreecommitdiff
path: root/CS2452/DictionarySearch.java
blob: 5d86a91ae003edd17e6c6227f4dd2b9f021d2d17 (plain)
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
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class DictionarySearch
{
	public static String[] dict;

	public static void main(String[] args)
	{
		BufferedReader ir = new BufferedReader(new InputStreamReader(System.in));
		BufferedReader fr = null;
		try
		{
			fr = new BufferedReader(new FileReader("/home/tje/workspace/Recursion/dictionary.wwf"));
		} catch (FileNotFoundException e1)
		{
			e1.printStackTrace();
		}

		String str = null;
		dict = new String[0];
		System.out.println("Dictionary loading...");

		do
		{
			try
			{
				str = fr.readLine();
			} catch (IOException e)
			{
				e.printStackTrace();
			}
			
			String[] tmp = new String[dict.length + 1];
			int i;
			for (i = 0; i < dict.length; i++ )
			{
				tmp[i] = dict[i];
			}
			tmp[i] = str;
			dict = tmp;

		} while (str != null);
		System.out.println("Dictionary loaded.");
		
		while (true)
		{
			System.out.print("What word would you like to look up? ");
			try
			{
				System.out.println("The closest word is " + check(ir.readLine().toLowerCase(), 0, dict.length));
			} catch (IOException e)
			{
				e.printStackTrace();
			}
			
			

		}
	}

	public static String check(String word, int a, int b)
	{
		if (a == b -1)
		{
			return dict[a];
		}
		int mid = (a+b)/2;
		if(word.compareTo(dict[mid]) > 0)
		{
			return check(word, mid, b);
		}
		
		if (word.compareTo(dict[mid]) < 0)
		{
			return check(word, a, mid);
		}
		
		if (word.compareTo(dict[mid]) == 0)
		{
			return dict[mid];
		}
		
		
		return word;

	}
}