-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathPredictivePrototype.java
More file actions
120 lines (104 loc) · 3.36 KB
/
PredictivePrototype.java
File metadata and controls
120 lines (104 loc) · 3.36 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
115
116
117
118
119
120
package predictive;
import java.io.FileReader;
import java.io.IOException;
import java.util.Scanner;
import java.util.Set;
import java.util.TreeSet;
/**
* class PredictivePrototype contains two method
* wordToSignature and signatureToWords
* @author ShukriAli
*@version 5/2/2018
*/
public class PredictivePrototype {
/**
* wordToSignature method converts a word into a signature
* based on the T9 system, where it checks each letter and
* assigns it to a corresponding number if it is an alphabetic
* character. This method used StringBuffer instead of String
* because the String object is immutable. When you concatenated
* Strings then in the bytecode it uses StringBuffer as it has
* an append method and converts back to a String. By using a
* StringBuffer you can skip that whole part making it
* more efficient.
* @param word is the String that is being converted
* @return retuns a String of the numbers corresponding
* to the word
*/
public static String wordToSignature(String word) {
StringBuffer str = new StringBuffer();
word = word.toLowerCase();
int x;
for (int i = 0; i < word.length(); i++) {
x = word.charAt(i)-97;
if(x<0 || x > 25){
str.append(" ");
}else
// these numbers round up to a number higher than the number allocated in the t9
if (x == 18 ||x == 21 || x == 24 || x == 25) {
str.append(x/3+1);
}
else
str.append(x/3+2);
}
return str.toString();
}
/**
* signatureToWords method creates a set with all
* possible matches of a signature from a
* file.
* @param signature is String of the signature numbers
* @return returns a set of words matching the signature
*/
public static Set<String> signatureToWords(String signature){
Set<String> setOfWords = new TreeSet<>();
Scanner scr = null;
String line;
try {
// wrap a scanner around FileReader
scr = new Scanner(new FileReader("/Users/ShukriAli/Downloads/words"));
// use the hasNext() method of the Scanner to read one line at a time.
// if there is no more lines left it will be false and stop the loop
//toLowerCase() ensures that the words from the dictionary are lowercase
//otherwise method wordToSignature might not work
while ((scr.hasNext())){
//uses wordToSignature method to convert the word in the dictionary into a signuture and
//the compares if the signutures are the same and if that word is not already
// in the list
//and ignores words with non-alphabetic characters
line = scr.next().toLowerCase();
if (isValidWord(line)){
if (wordToSignature(line).equals(signature)){
// if the word fillfulls the previous conditions the the word is added to the set
setOfWords.add(line);
}
}
}
}
catch (IOException e) {
System.out.println("Words file has not been found.");
} finally {
scr.close();
}
return setOfWords;
}
/**
* This method checks whether the words in the
* dictionary are non-alphabetic characters
* @param word is the words in dictionary to be checked
* if it is a valid word
* @return returns true if the word exists of only
* alphabetic characters else returns false
*/
public static boolean isValidWord(String word) {
word = word.toLowerCase();
int x;
for (int i = 0; i < word.length(); i++) {
x = word.charAt(i);
if(x<97 || x > 122){
return false;
}
}
return true;
}
}