Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.88 KB | None | 0 0
  1. //////////////////////// TOP OF FILE COMMENT BLOCK ////////////////////////////
  2. //
  3. // Title: Create Word Sets
  4. // Course: CS 200, Fall 2019
  5. //
  6. // Author: Sydney Clark
  7. // Email: slclark4@wisc.edu
  8. // Lecturer's Name: Marc Ranault
  9. //
  10. ///////////////////////////////// CITATIONS ////////////////////////////////////
  11. //
  12. // N/A
  13. /////////////////////////////// 80 COLUMNS WIDE ////////////////////////////////
  14. import java.io.File;
  15.  
  16. import java.io.FileNotFoundException;
  17. import java.io.PrintWriter;
  18. import java.util.ArrayList;
  19. import java.util.Collections;
  20. import java.util.Scanner;
  21.  
  22. public class CreateWordSets {
  23.  
  24. /**
  25. * Returns whether there is a vowel in the word.
  26. * Vowels are the lower case letters: aeiouy
  27. *
  28. * @param word The word to check letters.
  29. * @return true if at least one letter in word is a vowel,
  30. * false otherwise.
  31. */
  32. public static boolean hasVowel(String word) {
  33. boolean vowel = false;
  34. //word = word.toLowerCase();
  35. for(int i=0; i<word.length();i++) {
  36. if(word.charAt(i)== 'a'||word.charAt(i)== 'e'||word.charAt(i)== 'i'||word.charAt(i)== 'o'||word.charAt(i)== 'u'||word.charAt(i)== 'y') {
  37. vowel=true;
  38. }
  39. }
  40. return vowel;
  41. }
  42.  
  43. /**
  44. * Returns whether the word parameter has only lower case letters between
  45. * 'a' and 'z', inclusive.
  46. *
  47. * @param word The word to check for letters.
  48. * @return true if the word only contains letters, false otherwise.
  49. */
  50. public static boolean onlyLetters(String word) {
  51. boolean letters = true;
  52. for(int i=0; i<word.length();i++) {
  53. if(Character.isDigit(word.charAt(i))||Character.isUpperCase(word.charAt(i))||!Character.isLetter(word.charAt(i))) {
  54. letters=false;
  55. }
  56. }
  57. return letters;
  58. }
  59.  
  60. /**
  61. * Opens and reads a dictionary file returning a list of words.
  62. * Each word is checked to see if it contains a vowel (hasVowel) and is
  63. * composed only of letters (onlyLetters). If not the word
  64. * is discarded.
  65. *
  66. * If there is an error reading the file, such as the file cannot be found,
  67. * then the following message is shown:
  68. * Error: Unable to read file <dictionaryFilename>
  69. * with <dictionaryFilename> replaced with the parameter value.
  70. *
  71. * @param dictionaryFilename The dictionary file to read.
  72. * @return An ArrayList of words.
  73. */
  74. public static ArrayList<String> readDictionary(String dictionaryFilename) {
  75. Scanner scnr = null;
  76. ArrayList<String> wordList = new ArrayList<String>();
  77.  
  78. try {
  79. File dictionary = new File(dictionaryFilename);
  80. scnr = new Scanner(dictionary);
  81. while(scnr.hasNextLine()) {
  82. String word= scnr.nextLine();
  83. if(hasVowel(word)&& onlyLetters(word)) {
  84. wordList.add(word);
  85. }
  86.  
  87. }
  88. }catch(FileNotFoundException e) {
  89. System.out.println("Error: Unable to read file " + dictionaryFilename);
  90. }
  91. finally {
  92. if(scnr!=null) {
  93. scnr.close();
  94. }
  95. }
  96. return wordList;
  97. }
  98.  
  99. /**
  100. * This removes all the words from the dictionary that are outside the
  101. * length parameters.
  102. *
  103. * @param dictionary The dictionary to filter.
  104. * @param minLength The minimum length of words in the dictionary.
  105. * @param maxLength The maximum length of words in the dictionary.
  106. */
  107. public static void lengthFilter( ArrayList<String> dictionary, int minLength, int maxLength) {
  108. ArrayList<String> newDictionary = new ArrayList<String>();
  109. for(int i=0;i<dictionary.size();i++) {
  110. String word = dictionary.get(i);
  111. if(word.length()>=minLength && word.length()<=maxLength) {
  112. newDictionary.add(word);
  113. }
  114. }
  115. dictionary.clear();
  116. dictionary.addAll(newDictionary);
  117. return;
  118. }
  119.  
  120. /**
  121. * Returns an array with the count of each letter in the word. This only
  122. * check for the letters from lower case 'a' to 'z' and the 0th index
  123. * in the returned array corresponds to the count of 'a' in the word.
  124. *
  125. * @param word The word to count its letters.
  126. * @return The array of counts of each letter in the word.
  127. */
  128. public static int [] letterCounts(String word) {
  129. int[]letterCount = new int[26];
  130. for(int i=0; i<26; i++) {
  131. for(int j=0; j<word.length(); j++) {
  132. if(word.charAt(j)== (char)(97+i)) {
  133. letterCount[i]= letterCount[i]+1;
  134. }
  135. }
  136. }
  137. return letterCount; //TODO
  138. }
  139.  
  140. /**
  141. * This checks that the check word has no more of any letter than
  142. * the keyword. The counts parameters were created with the letterCounts method.
  143. *
  144. * @param keywordLetterCounts The counts of each letter for the keyword.
  145. * @param checkWordLetterCounts The counts of each letter for the check word.
  146. * @return
  147. */
  148. public static boolean sameLetters(int[]keywordLetterCounts, int[] checkWordLetterCounts) {
  149. boolean sameLetter=true;
  150. for(int i=0; i<checkWordLetterCounts.length;i++) {
  151. if(keywordLetterCounts[i]<checkWordLetterCounts[i]) {
  152. sameLetter=false;
  153. }
  154. }
  155. return sameLetter;
  156. }
  157.  
  158. /**
  159. * For each word in the dictionary that has length keywordLength this creates
  160. * sets of words that are made of the same or subset of the letters.
  161. *
  162. * Algorithm:
  163. * For each word in the dictionary having a length equal to the keywordLength
  164. * Create a new word set.
  165. * Count the number of each letter in the keyword (letterCounts)
  166. * For each word in the dictionary that has the same or a subset of the
  167. * same letters (sameLetters) then add that word to the word set.
  168. * If the word set has at least minWordSetSize words
  169. * Sort the word set (If wordSet is an ArrayList of String then
  170. * use java.util.Collections.sort(wordSet)).
  171. * Write the keyword to the file as the first word on the line,
  172. * followed by a : and space.
  173. * Then write each word of the word set to the same line of the
  174. * file with a space separating each word.
  175. *
  176. * If there is an error writing the file then the following message is shown:
  177. * Error: Unable to write file <wordSetFilename>
  178. * with <wordSetFilename> replaced with the parameter value.
  179. *
  180. * File Example:
  181. * shall: all ash hall has las shall
  182. * award: ada award draw raw war ward
  183. *
  184. * The first words, shall and award, are the keywords, one word that uses
  185. * all the letters. All the other words are made up of a subset (or all) of
  186. * the letters. Also see the wordSets.txt file.
  187. *
  188. * @param dictionary The dictionary of words.
  189. * @param wordSetFilename The name of the file to write the word sets
  190. * @param keywordLength length of the keyword
  191. * @param minWordSetSize The minimum number of words necessary in order to
  192. * save the word set.
  193. */
  194. public static void createSets(ArrayList<String> dictionary,
  195. String wordSetFilename, int keywordLength, int minWordSetSize) {
  196. try {
  197. PrintWriter newFileContent = new PrintWriter(wordSetFilename);
  198. ArrayList<String> wordsForFile = new ArrayList<String>();
  199. for(int i=0; i<dictionary.size(); i++) {
  200. if(keywordLength==dictionary.get(i).length()) {
  201. for(int j=0; j<dictionary.size();j++) {
  202. if( sameLetters(letterCounts(dictionary.get(i)), letterCounts(dictionary.get(j)))) {
  203. wordsForFile.add(dictionary.get(j));
  204. }
  205. }
  206. if(wordsForFile.size()>= minWordSetSize) {
  207. java.util.Collections.sort(wordsForFile);
  208. newFileContent.print(dictionary.get(i) + ": ");
  209. for(int k=0; k<wordsForFile.size(); k++) {
  210. newFileContent.print(wordsForFile.get(k) + " ");
  211. }
  212. System.out.println("");
  213. }
  214. }
  215. }
  216. newFileContent.close();
  217. } catch(FileNotFoundException e) {
  218. System.out.println("Error: Unable to write file " + wordSetFilename);
  219. }
  220. return; //TODO
  221. }
  222.  
  223. /**
  224. * This reads in a dictionary and filters the dictionary to contain only
  225. * words from 3 to 5 letters long. Then sets of words to be used in the
  226. * WordDetective program are saved to a file.
  227. *
  228. * The source dictionary, google10000english.txt, is from google:
  229. * https://github.com/first20hours/google-10000-english/blob/master/google-10000-english-no-swears.txt
  230. *
  231. * @param args unused.
  232. */
  233. public static void main(String[] args) {
  234.  
  235. ArrayList<String> dictionary = readDictionary("google10000english.txt");
  236. lengthFilter(dictionary, 3, 5);
  237. createSets(dictionary, "wordSets.txt", 5, 6);
  238. }
  239. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement