/** * Dictionary constructor that uses an aspell phonetic file to * build the transformation table. * If phonetic is null, then DoubleMeta is used with the English alphabet * @param wordList The file containing dictionary as a words list. * @param phonetic The file containing the phonetic transformation * information. * @throws java.io.FileNotFoundException when the words list or phonetic * file could not be located on the system * @throws java.io.IOException when problems occurs while reading the * words list or phonetic file */ public GenericSpellDictionary(File wordList, File phonetic) throws FileNotFoundException, IOException { super(phonetic); dictFile = wordList; createDictionary(new BufferedReader(new FileReader(wordList))); }
/** * Returns true if the word is correctly spelled against the current word list. * @param word The word to checked in the dictionary * @return indication if the word is in the dictionary */ public boolean isCorrect(String word) { List possible = getWords(getCode(word)); if (possible.contains(word)) return true; //JMH should we always try the lowercase version. If I dont then capitalised //words are always returned as incorrect. else if (possible.contains(word.toLowerCase())) return true; return false; } }
/** * Constructs the dictionary from a word list file. * <p> * Each word in the reader should be on a separate line. * <p> * This is a very slow function. On my machine it takes quite a while to * load the data in. I suspect that we could speed this up quite allot. */ protected void createDictionary(BufferedReader in) throws IOException { String line = ""; while (line != null) { line = in.readLine(); if (line != null) { line = new String(line.toCharArray()); putWord(line); } } }
/** * Allocates a word in the dictionary */ protected void putWord(String word) { String code = getCode(word); LinkedList list = (LinkedList) mainDictionary.get(code); if (list != null) { list.add(word); } else { list = new LinkedList(); list.add(word); mainDictionary.put(code, list); } }
/** * Add a word permanently to the dictionary (and the dictionary file). * <p>This needs to be made thread safe (synchronized)</p> * @param word The word to add to the dictionary * @return Whether the word was successfully added. */ public boolean addWord(String word) { putWord(word); if (dictFile!=null) { try { FileWriter w = new FileWriter(dictFile.toString(), true); // Open with append. w.write(word); w.write("\n"); w.close(); } catch (IOException ex) { System.out.println("Error writing to dictionary file"); ex.printStackTrace(); return false; } } return true; }