Advertisement
Guest User

Untitled

a guest
Jul 16th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.89 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. //package project;
  7. import java.nio.file.Paths;
  8. import java.io.FileReader;
  9. import java.io.BufferedReader;
  10. import java.io.FileNotFoundException;
  11. import java.io.IOException;
  12. import java.util.*;
  13. import java.util.stream.Collectors;
  14. /**
  15. *
  16. * @author Aharon's PC
  17. */
  18. public class Project2 {
  19. public static void main (String[] args) throws FileNotFoundException { test(); }
  20.  
  21. public static void test() throws FileNotFoundException{
  22. StringBuilder txtFilePath = new StringBuilder (Paths.get("").toAbsolutePath().toString());
  23. txtFilePath.append("/COSC602_P2_EnglishWordList.txt");
  24. BufferedReader txtFile = new BufferedReader( new FileReader(txtFilePath.toString()));
  25. List<String> wordsList = new ArrayList<>();
  26. Map<Integer, Set<Character>> digitsMap = new HashMap<>();
  27. try {
  28. String line;
  29. while ( (line = txtFile.readLine()) != null) {
  30. wordsList.add(line);
  31. }
  32.  
  33. } catch (IOException ioe) {
  34. wordsList.clear();
  35. }
  36.  
  37. digitsMap.put(2, new HashSet<> (Arrays.asList('A', 'B', 'C')) );
  38. digitsMap.put(3, new HashSet<> (Arrays.asList('D', 'E', 'F')) );
  39. digitsMap.put(4, new HashSet<> (Arrays.asList('G', 'H', 'I')) );
  40. digitsMap.put(5, new HashSet<> (Arrays.asList('J', 'K', 'L')) );
  41. digitsMap.put(6, new HashSet<> (Arrays.asList('M', 'N', 'O')) );
  42. digitsMap.put(7, new HashSet<> (Arrays.asList('P', 'Q', 'R','S')) );
  43. digitsMap.put(8, new HashSet<> (Arrays.asList('T', 'U', 'V')) );
  44. digitsMap.put(9, new HashSet<> (Arrays.asList('W', 'X', 'Y','Z')) );
  45.  
  46. //This is the variable representing my string followed by the string's statement.
  47. String p= "Please enter a seven digit phone number without a zero or a one to recieve it's corresponding word";
  48. System.out.println(p);
  49. //This is supposed to be the scanner which uploads the word list file from my computer into this program.
  50. //Scanner theFile=new Scanner(new File("cosc602p2englishwordlist.txt"));
  51. //This scanner takes in the user's request for which numbers should be converted into words.
  52. Scanner scanner=new Scanner(System.in);
  53. String input = scanner.next();
  54. Integer numbers = Integer.valueOf(input); //2225663;
  55.  
  56. //Here I am running the "error" method.
  57. while (errorCheck(input)) {
  58. System.out.println("ERROR: Invalid request. Reread the instructions please and try again.");
  59. System.out.println(p);
  60. input = scanner.next();
  61. }
  62. numbers = Integer.valueOf(input);
  63. System.out.println("Processing request...");
  64.  
  65. wordsList = wordsList
  66. .stream()
  67. .filter( e -> e.length()==7)
  68. .map ( w -> w.toUpperCase() )
  69. .collect(Collectors.toList());
  70.  
  71. //check for matches number by number
  72. // Set<String> matches = checkMatch(numbers, digitsMap, wordsList);
  73. // matches.forEach(System.out::println);
  74.  
  75. // key-value pairs
  76. // for example,
  77. // 2225663 -> [ABALONE]
  78. Map<Integer, List<String>> dictionary = new HashMap<>();
  79. wordsList.forEach(w ->
  80. {
  81. Integer key = wordToNum(w);
  82. if (dictionary.containsKey(key))
  83. dictionary.get(key).add(w);
  84. else {
  85. List<String> words = new ArrayList<>();
  86. words.add(w);
  87. dictionary.put(key, words);
  88. }
  89. }
  90. );
  91.  
  92. //checking to see if our dictionary contains the sequence of numbers the client entered
  93. if(!dictionary.containsKey(numbers))
  94. dictionary.get(numbers).forEach(System.out::println);
  95. System.out.println("Word not found.");
  96.  
  97. }
  98.  
  99. public static Integer wordToNum (String word) {
  100. StringBuilder digits = new StringBuilder(0);
  101. for (char c : word.toCharArray()) {
  102. switch (c) {
  103. case 'A':
  104. case 'B':
  105. case 'C':
  106. digits.append('2');
  107. break;
  108.  
  109. case 'D':
  110. case 'E':
  111. case 'F':
  112. digits.append('3');
  113. break;
  114.  
  115. case 'G':
  116. case 'H':
  117. case 'I':
  118. digits.append('4');
  119. break;
  120.  
  121. case 'J':
  122. case 'K':
  123. case 'L':
  124. digits.append('5');
  125. break;
  126.  
  127. case 'M':
  128. case 'N':
  129. case 'O':
  130. digits.append('6');
  131. break;
  132.  
  133. case 'P':
  134. case 'Q':
  135. case 'R':
  136. case 'S':
  137. digits.append('7');
  138. break;
  139.  
  140. case 'T':
  141. case 'U':
  142. case 'V':
  143. digits.append('8');
  144. break;
  145.  
  146. case 'W':
  147. case 'X':
  148. case 'Y':
  149. case 'Z':
  150. digits.append('9');
  151. break;
  152. }
  153. }
  154. return Integer.valueOf(digits.toString());
  155. }
  156.  
  157. @Deprecated
  158. public static Set<String> checkMatch (String digits, Map<Integer, Set<Character>> digitsMap, List<String> wordsList) {
  159. Set<String> remainder = new HashSet<>(wordsList);
  160.  
  161. Set<Character> firstDigitSet = digitsMap.get(Integer.valueOf(digits.substring(0,1)));
  162. Set<Character> secondDigitSet = digitsMap.get(Integer.valueOf(digits.substring(1,2)));
  163. Set<Character> thirdDigitSet = digitsMap.get(Integer.valueOf(digits.substring(2,3)));
  164. Set<Character> fourthDigitSet = digitsMap.get(Integer.valueOf(digits.substring(3,4)));
  165. Set<Character> fifthDigitSet = digitsMap.get(Integer.valueOf(digits.substring(4,5)));
  166. Set<Character> sixthDigitSet = digitsMap.get(Integer.valueOf(digits.substring(5,6)));
  167. Set<Character> seventhDigitSet = digitsMap.get(Integer.valueOf(digits.substring(6,7)));
  168.  
  169. //filter out potential first letter matches
  170. for (String word: wordsList) {
  171. if (! firstDigitSet.contains( word.substring(0, 1).toUpperCase().charAt(0))) {
  172. remainder.remove(word);
  173. }
  174. }
  175.  
  176. for (char firstDigit : firstDigitSet) {
  177.  
  178. Set<String> secondDigit1 = new HashSet<>(remainder),
  179. secondDigit2 = new HashSet<>(remainder),
  180. secondDigit3 = new HashSet<>(remainder);
  181. Character[] secondDigitArray = secondDigitSet.toArray(new Character[0]);
  182.  
  183. for (int i=0; i<secondDigitArray.length; i++ ) {
  184. String secondDigitString = new StringBuilder().append(firstDigit)
  185. .append(secondDigitArray[i]).toString(); //AA or AB or AC given 2
  186. if (i==0)
  187. for (String word : secondDigit1) { //check for begins with AA
  188. if (!word.substring(0, 2).toUpperCase().contentEquals(secondDigitString) ) {
  189. secondDigit1.remove(word);
  190. }
  191. }
  192. else if (i ==1)
  193. for (String word : secondDigit2) { //check for begins with AB
  194. if (!word.substring(0, 2).toUpperCase().contentEquals(secondDigitString) ) {
  195. secondDigit2.remove(word);
  196. }
  197. }
  198. else if (i ==2)
  199. for (String word : secondDigit3) { //check for begins with AC
  200. if (!word.substring(0, 2).toUpperCase().contentEquals(secondDigitString) ) {
  201. secondDigit3.remove(word);
  202. }
  203. }
  204. }
  205. Set<String> union = new HashSet<>(secondDigit1); //words that begin with any combination of A-C, two letter combination
  206. union.addAll(secondDigit2);
  207. union.addAll(secondDigit3);
  208. System.out.println(union);
  209.  
  210.  
  211. // for (char thirdDigit : thirdDigitSet) {
  212. // String thirdDigitString = new StringBuilder(firstDigit).append(secondDigit)
  213. // .append(thirdDigit).toString();
  214. // for (String word : remainder) {
  215. // if (!word.substring(0, 2).toLowerCase().contentEquals(thirdDigitString) ) {
  216. // remainder.remove(word);
  217. // }
  218. // }
  219. // System.out.println("ok");
  220. // }
  221. // }
  222. }
  223.  
  224.  
  225.  
  226. return remainder;
  227. }
  228.  
  229. /*This is the "error" method.
  230. It is meant to produce an error message if the user
  231. enters a number that is either greater or less than 7
  232. and contains either a number "1" or a number "0".
  233. If there is an error message which is reported, after it reports
  234. the message it starts the original request again.
  235. */
  236. public static boolean errorCheck (String input) {
  237. if(input.contains("0")||input.contains("1")||input.length()<7||input.length()>7)
  238. return true;
  239. else return false; //no errors
  240. }
  241.  
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement