/* * method to return a HashSet of words lower-cased and read from the file * passed in as a parameter */ public HashSet<String> readDictionary(FileResource fr) { // create a HashSet which you will be returning HashSet<String> dict = new HashSet<String>(); for (String line : fr.lines()) { // convert to lowercase and add to the dictionary line = line.toLowerCase(); dict.add(line); } return dict; }
public void testReadDictionary() { FileResource fr = new FileResource("dictionaries/English"); HashSet<String> dict = readDictionary(fr); System.out.println("dict has " + dict.size() + " entries"); Iterator<String> it = dict.iterator(); for (int i = 0; i < 25; i++) { if (it.hasNext()) { System.out.println(i + "\t" + it.next()); } } }
public void testMostCommonCharIn() { FileResource fr = new FileResource("dictionaries/English"); HashSet<String> dict = readDictionary(fr); System.out.println("English dict has " + dict.size() + " entries"); char mostCommon = mostCommonCharIn(dict); System.out.println(" most common char: " + mostCommon); FileResource fr1 = new FileResource("dictionaries/Spanish"); HashSet<String> dict1 = readDictionary(fr1); System.out.println("Spanish dict has " + dict1.size() + " entries"); mostCommon = mostCommonCharIn(dict1); System.out.println(" most common char: " + mostCommon); }
/* * returns # of valid words given message and dictionary against which to check words in * the message. Lowercases the word before checking in dictionary */ public int countWords(String message, HashSet<String> dictionary) { int validWords = 0; int totalWords = 0; // split the message into words for (String word : message.split("\\W")) { totalWords++; word = word.toLowerCase(); if (dictionary.contains(word)) { validWords++; } } System.out.println("total words: " + totalWords + " valid words: " + validWords); return validWords; }