Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- //package project;
- import java.nio.file.Paths;
- import java.io.FileReader;
- import java.io.BufferedReader;
- import java.io.FileNotFoundException;
- import java.io.IOException;
- import java.util.*;
- import java.util.stream.Collectors;
- /**
- *
- * @author Aharon's PC
- */
- public class Project2 {
- public static void main (String[] args) throws FileNotFoundException { test(); }
- public static void test() throws FileNotFoundException{
- StringBuilder txtFilePath = new StringBuilder (Paths.get("").toAbsolutePath().toString());
- txtFilePath.append("/COSC602_P2_EnglishWordList.txt");
- BufferedReader txtFile = new BufferedReader( new FileReader(txtFilePath.toString()));
- List<String> wordsList = new ArrayList<>();
- Map<Integer, Set<Character>> digitsMap = new HashMap<>();
- try {
- String line;
- while ( (line = txtFile.readLine()) != null) {
- wordsList.add(line);
- }
- } catch (IOException ioe) {
- wordsList.clear();
- }
- digitsMap.put(2, new HashSet<> (Arrays.asList('A', 'B', 'C')) );
- digitsMap.put(3, new HashSet<> (Arrays.asList('D', 'E', 'F')) );
- digitsMap.put(4, new HashSet<> (Arrays.asList('G', 'H', 'I')) );
- digitsMap.put(5, new HashSet<> (Arrays.asList('J', 'K', 'L')) );
- digitsMap.put(6, new HashSet<> (Arrays.asList('M', 'N', 'O')) );
- digitsMap.put(7, new HashSet<> (Arrays.asList('P', 'Q', 'R','S')) );
- digitsMap.put(8, new HashSet<> (Arrays.asList('T', 'U', 'V')) );
- digitsMap.put(9, new HashSet<> (Arrays.asList('W', 'X', 'Y','Z')) );
- //This is the variable representing my string followed by the string's statement.
- String p= "Please enter a seven digit phone number without a zero or a one to recieve it's corresponding word";
- System.out.println(p);
- //This is supposed to be the scanner which uploads the word list file from my computer into this program.
- //Scanner theFile=new Scanner(new File("cosc602p2englishwordlist.txt"));
- //This scanner takes in the user's request for which numbers should be converted into words.
- Scanner scanner=new Scanner(System.in);
- String input = scanner.next();
- Integer numbers = Integer.valueOf(input); //2225663;
- //Here I am running the "error" method.
- while (errorCheck(input)) {
- System.out.println("ERROR: Invalid request. Reread the instructions please and try again.");
- System.out.println(p);
- input = scanner.next();
- }
- numbers = Integer.valueOf(input);
- System.out.println("Processing request...");
- wordsList = wordsList
- .stream()
- .filter( e -> e.length()==7)
- .map ( w -> w.toUpperCase() )
- .collect(Collectors.toList());
- //check for matches number by number
- // Set<String> matches = checkMatch(numbers, digitsMap, wordsList);
- // matches.forEach(System.out::println);
- // key-value pairs
- // for example,
- // 2225663 -> [ABALONE]
- Map<Integer, List<String>> dictionary = new HashMap<>();
- wordsList.forEach(w ->
- {
- Integer key = wordToNum(w);
- if (dictionary.containsKey(key))
- dictionary.get(key).add(w);
- else {
- List<String> words = new ArrayList<>();
- words.add(w);
- dictionary.put(key, words);
- }
- }
- );
- //checking to see if our dictionary contains the sequence of numbers the client entered
- if(!dictionary.containsKey(numbers))
- dictionary.get(numbers).forEach(System.out::println);
- System.out.println("Word not found.");
- }
- public static Integer wordToNum (String word) {
- StringBuilder digits = new StringBuilder(0);
- for (char c : word.toCharArray()) {
- switch (c) {
- case 'A':
- case 'B':
- case 'C':
- digits.append('2');
- break;
- case 'D':
- case 'E':
- case 'F':
- digits.append('3');
- break;
- case 'G':
- case 'H':
- case 'I':
- digits.append('4');
- break;
- case 'J':
- case 'K':
- case 'L':
- digits.append('5');
- break;
- case 'M':
- case 'N':
- case 'O':
- digits.append('6');
- break;
- case 'P':
- case 'Q':
- case 'R':
- case 'S':
- digits.append('7');
- break;
- case 'T':
- case 'U':
- case 'V':
- digits.append('8');
- break;
- case 'W':
- case 'X':
- case 'Y':
- case 'Z':
- digits.append('9');
- break;
- }
- }
- return Integer.valueOf(digits.toString());
- }
- @Deprecated
- public static Set<String> checkMatch (String digits, Map<Integer, Set<Character>> digitsMap, List<String> wordsList) {
- Set<String> remainder = new HashSet<>(wordsList);
- Set<Character> firstDigitSet = digitsMap.get(Integer.valueOf(digits.substring(0,1)));
- Set<Character> secondDigitSet = digitsMap.get(Integer.valueOf(digits.substring(1,2)));
- Set<Character> thirdDigitSet = digitsMap.get(Integer.valueOf(digits.substring(2,3)));
- Set<Character> fourthDigitSet = digitsMap.get(Integer.valueOf(digits.substring(3,4)));
- Set<Character> fifthDigitSet = digitsMap.get(Integer.valueOf(digits.substring(4,5)));
- Set<Character> sixthDigitSet = digitsMap.get(Integer.valueOf(digits.substring(5,6)));
- Set<Character> seventhDigitSet = digitsMap.get(Integer.valueOf(digits.substring(6,7)));
- //filter out potential first letter matches
- for (String word: wordsList) {
- if (! firstDigitSet.contains( word.substring(0, 1).toUpperCase().charAt(0))) {
- remainder.remove(word);
- }
- }
- for (char firstDigit : firstDigitSet) {
- Set<String> secondDigit1 = new HashSet<>(remainder),
- secondDigit2 = new HashSet<>(remainder),
- secondDigit3 = new HashSet<>(remainder);
- Character[] secondDigitArray = secondDigitSet.toArray(new Character[0]);
- for (int i=0; i<secondDigitArray.length; i++ ) {
- String secondDigitString = new StringBuilder().append(firstDigit)
- .append(secondDigitArray[i]).toString(); //AA or AB or AC given 2
- if (i==0)
- for (String word : secondDigit1) { //check for begins with AA
- if (!word.substring(0, 2).toUpperCase().contentEquals(secondDigitString) ) {
- secondDigit1.remove(word);
- }
- }
- else if (i ==1)
- for (String word : secondDigit2) { //check for begins with AB
- if (!word.substring(0, 2).toUpperCase().contentEquals(secondDigitString) ) {
- secondDigit2.remove(word);
- }
- }
- else if (i ==2)
- for (String word : secondDigit3) { //check for begins with AC
- if (!word.substring(0, 2).toUpperCase().contentEquals(secondDigitString) ) {
- secondDigit3.remove(word);
- }
- }
- }
- Set<String> union = new HashSet<>(secondDigit1); //words that begin with any combination of A-C, two letter combination
- union.addAll(secondDigit2);
- union.addAll(secondDigit3);
- System.out.println(union);
- // for (char thirdDigit : thirdDigitSet) {
- // String thirdDigitString = new StringBuilder(firstDigit).append(secondDigit)
- // .append(thirdDigit).toString();
- // for (String word : remainder) {
- // if (!word.substring(0, 2).toLowerCase().contentEquals(thirdDigitString) ) {
- // remainder.remove(word);
- // }
- // }
- // System.out.println("ok");
- // }
- // }
- }
- return remainder;
- }
- /*This is the "error" method.
- It is meant to produce an error message if the user
- enters a number that is either greater or less than 7
- and contains either a number "1" or a number "0".
- If there is an error message which is reported, after it reports
- the message it starts the original request again.
- */
- public static boolean errorCheck (String input) {
- if(input.contains("0")||input.contains("1")||input.length()<7||input.length()>7)
- return true;
- else return false; //no errors
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement