+++ /dev/null
-/*\r
-Jazzy - a Java library for Spell Checking\r
-Copyright (C) 2001 Mindaugas Idzelis\r
-Full text of license can be found in LICENSE.txt\r
-\r
-This library is free software; you can redistribute it and/or\r
-modify it under the terms of the GNU Lesser General Public\r
-License as published by the Free Software Foundation; either\r
-version 2.1 of the License, or (at your option) any later version.\r
-\r
-This library is distributed in the hope that it will be useful,\r
-but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\r
-Lesser General Public License for more details.\r
-\r
-You should have received a copy of the GNU Lesser General Public\r
-License along with this library; if not, write to the Free Software\r
-Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA\r
-*/\r
-package com.swabunga.spell.engine;\r
-\r
-import java.io.BufferedReader;\r
-import java.io.File;\r
-import java.io.FileNotFoundException;\r
-import java.io.FileReader;\r
-import java.io.FileWriter;\r
-import java.io.IOException;\r
-import java.util.HashMap;\r
-import java.util.LinkedList;\r
-import java.util.List;\r
-import java.util.Vector;\r
-\r
-/**\r
- * The SpellDictionary class holds the instance of the dictionary.\r
- * <p>\r
- * This class is thread safe. Derived classes should ensure that this preserved.\r
- * </p>\r
- * <p>\r
- * There are many open source dictionary files. For just a few see:\r
- * http://wordlist.sourceforge.net/\r
- * </p>\r
- * <p>\r
- * This dictionary class reads words one per line. Make sure that your word list\r
- * is formatted in this way (most are).\r
- * </p>\r
- */\r
-public class GenericSpellDictionary extends SpellDictionaryASpell {\r
-\r
-//tech_monkey: the alphabet / replace list stuff has been moved into the Transformator classes,\r
-//since they are so closely tied to how the phonetic transformations are done.\r
-// /**\r
-// * This replace list is used if no phonetic file is supplied or it doesn't\r
-// * contain the alphabet.\r
-// */\r
-// protected static final char[] englishAlphabet =\r
-\r
-\r
- /** A field indicating the initial hash map capacity (16KB) for the main\r
- * dictionary hash map. Interested to see what the performance of a\r
- * smaller initial capacity is like.\r
- */\r
- private final static int INITIAL_CAPACITY = 16 * 1024;\r
-\r
- /**\r
- * The hashmap that contains the word dictionary. The map is hashed on the doublemeta\r
- * code. The map entry contains a LinkedList of words that have the same double meta code.\r
- */\r
- @SuppressWarnings("unchecked")\r
-protected HashMap mainDictionary = new HashMap(INITIAL_CAPACITY);\r
-\r
- /** Holds the dictionary file for appending*/\r
- private File dictFile = null;\r
-\r
-\r
- /**\r
- * Dictionary constructor that uses the DoubleMeta class with the\r
- * English alphabet.\r
- * @param wordList The file containing dictionary as a words list.\r
- * @throws java.io.FileNotFoundException when the words list file could not \r
- * be located on the system.\r
- * @throws java.io.IOException when problems occurs while reading the words \r
- * list file\r
- */\r
- public GenericSpellDictionary(File wordList) throws FileNotFoundException, IOException {\r
- this(wordList, (File) null);\r
- }\r
-\r
- /**\r
- * Dictionary constructor that uses an aspell phonetic file to\r
- * build the transformation table.\r
- * If phonetic is null, then DoubleMeta is used with the English alphabet\r
- * @param wordList The file containing dictionary as a words list.\r
- * @param phonetic The file containing the phonetic transformation \r
- * information.\r
- * @throws java.io.FileNotFoundException when the words list or phonetic \r
- * file could not be located on the system\r
- * @throws java.io.IOException when problems occurs while reading the \r
- * words list or phonetic file\r
- */\r
- public GenericSpellDictionary(File wordList, File phonetic) throws FileNotFoundException, IOException {\r
-\r
- super(phonetic);\r
- dictFile = wordList;\r
- createDictionary(new BufferedReader(new FileReader(wordList)));\r
- }\r
-\r
-\r
- /**\r
- * Add a word permanently to the dictionary (and the dictionary file).\r
- * <p>This needs to be made thread safe (synchronized)</p>\r
- * @param word The word to add to the dictionary\r
- */\r
- public void addWord(String word) {\r
- putWord(word);\r
- if (dictFile == null)\r
- return;\r
- try {\r
- FileWriter w = new FileWriter(dictFile.toString(), true);\r
- // Open with append.\r
- w.write(word);\r
- w.write("\n");\r
- w.close();\r
- } catch (IOException ex) {\r
- System.out.println("Error writing to dictionary file");\r
- }\r
- }\r
-\r
- /**\r
- * Constructs the dictionary from a word list file.\r
- * <p>\r
- * Each word in the reader should be on a separate line.\r
- * <p>\r
- * This is a very slow function. On my machine it takes quite a while to\r
- * load the data in. I suspect that we could speed this up quite allot.\r
- */\r
- protected void createDictionary(BufferedReader in) throws IOException {\r
- String line = "";\r
- while (line != null) {\r
- line = in.readLine();\r
- if (line != null) {\r
- line = new String(line.toCharArray());\r
- putWord(line);\r
- }\r
- }\r
- }\r
-\r
- /**\r
- * Allocates a word in the dictionary\r
- */\r
- @SuppressWarnings("unchecked")\r
-protected void putWord(String word) {\r
- String code = getCode(word);\r
- LinkedList list = (LinkedList) mainDictionary.get(code);\r
- if (list != null) {\r
- list.add(word);\r
- } else {\r
- list = new LinkedList();\r
- list.add(word);\r
- mainDictionary.put(code, list);\r
- }\r
- }\r
-\r
- /**\r
- * Returns a list of strings (words) for the code.\r
- * @param code The phonetic code we want to find words for\r
- * @return the list of words having the same phonetic code\r
- */\r
- @SuppressWarnings("unchecked")\r
-@Override\r
-public List getWords(String code) {\r
- //Check the main dictionary.\r
- List mainDictResult = (List) mainDictionary.get(code);\r
- if (mainDictResult == null)\r
- return new Vector();\r
- return mainDictResult;\r
- }\r
-\r
- /**\r
- * Returns true if the word is correctly spelled against the current word list.\r
- * @param word The word to checked in the dictionary\r
- * @return indication if the word is in the dictionary\r
- */\r
- @SuppressWarnings("unchecked")\r
-@Override\r
-public boolean isCorrect(String word) {\r
- List possible = getWords(getCode(word));\r
- if (possible.contains(word))\r
- return true;\r
- //JMH should we always try the lowercase version. If I dont then capitalised\r
- //words are always returned as incorrect.\r
- else if (possible.contains(word.toLowerCase()))\r
- return true;\r
- return false;\r
- }\r
-}\r