001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.words.util;
022:
023: import com.liferay.portal.kernel.util.Randomizer;
024: import com.liferay.portal.kernel.util.StringUtil;
025: import com.liferay.portal.util.ContentUtil;
026: import com.liferay.portlet.words.ScramblerException;
027: import com.liferay.util.CollectionFactory;
028: import com.liferay.util.ListUtil;
029: import com.liferay.util.jazzy.BasicSpellCheckListener;
030:
031: import com.swabunga.spell.engine.SpellDictionaryHashMap;
032: import com.swabunga.spell.event.DefaultWordFinder;
033: import com.swabunga.spell.event.SpellChecker;
034: import com.swabunga.spell.event.StringWordTokenizer;
035:
036: import java.io.IOException;
037: import java.io.StringReader;
038:
039: import java.util.ArrayList;
040: import java.util.Collections;
041: import java.util.List;
042: import java.util.Set;
043:
044: import org.apache.commons.logging.Log;
045: import org.apache.commons.logging.LogFactory;
046:
047: /**
048: * <a href="WordsUtil.java.html"><b><i>View Source</i></b></a>
049: *
050: * @author Brian Wing Shun Chan
051: *
052: */
053: public class WordsUtil {
054:
055: public static List checkSpelling(String text) {
056: return _instance._checkSpelling(text);
057: }
058:
059: public static List getDictionaryList() {
060: return _instance._getDictionaryList();
061: }
062:
063: public static Set getDictionarySet() {
064: return _instance._getDictionarySet();
065: }
066:
067: public static String getRandomWord() {
068: return _instance._getRandomWord();
069: }
070:
071: public static boolean isDictionaryWord(String word) {
072: return _instance._isDictionaryWord(word);
073: }
074:
075: public static String[] scramble(String word)
076: throws ScramblerException {
077: Scrambler scrambler = new Scrambler(word);
078:
079: return scrambler.scramble();
080: }
081:
082: public static String[] unscramble(String word)
083: throws ScramblerException {
084: return _instance._unscramble(word);
085: }
086:
087: private WordsUtil() {
088: _dictionaryList = ListUtil
089: .fromArray(StringUtil
090: .split(
091: ContentUtil
092: .get("com/liferay/portlet/words/dependencies/words.txt"),
093: "\n"));
094:
095: _dictionaryList = Collections.unmodifiableList(_dictionaryList);
096:
097: _dictionarySet = CollectionFactory.getHashSet(_dictionaryList
098: .size());
099:
100: _dictionarySet.addAll(_dictionaryList);
101:
102: _dictionarySet = Collections.unmodifiableSet(_dictionarySet);
103:
104: try {
105: _spellDictionary = new SpellDictionaryHashMap();
106:
107: String[] dics = new String[] { "center.dic", "centre.dic",
108: "color.dic", "colour.dic", "eng_com.dic",
109: "english.0", "english.1", "ise.dic", "ize.dic",
110: "labeled.dic", "labelled.dic", "yse.dic", "yze.dic" };
111:
112: for (int i = 0; i < dics.length; i++) {
113: _spellDictionary
114: .addDictionary(new StringReader(
115: ContentUtil
116: .get("com/liferay/portlet/words/dependencies/"
117: + dics[i])));
118: }
119: } catch (IOException ioe) {
120: _log.error(ioe);
121: }
122: }
123:
124: private List _checkSpelling(String text) {
125: SpellChecker checker = new SpellChecker(_spellDictionary);
126:
127: BasicSpellCheckListener listener = new BasicSpellCheckListener(
128: text);
129:
130: checker.addSpellCheckListener(listener);
131:
132: checker.checkSpelling(new StringWordTokenizer(
133: new DefaultWordFinder(text)));
134:
135: return listener.getInvalidWords();
136: }
137:
138: private List _getDictionaryList() {
139: return _dictionaryList;
140: }
141:
142: private Set _getDictionarySet() {
143: return _dictionarySet;
144: }
145:
146: private String _getRandomWord() {
147: int pos = Randomizer.getInstance().nextInt(
148: _dictionaryList.size());
149:
150: return (String) _dictionaryList.get(pos);
151: }
152:
153: private boolean _isDictionaryWord(String word) {
154: return _dictionarySet.contains(word);
155: }
156:
157: private String[] _unscramble(String word) throws ScramblerException {
158: List validWords = new ArrayList();
159:
160: String[] words = scramble(word);
161:
162: for (int i = 0; i < words.length; i++) {
163: if (_dictionarySet.contains(words[i])) {
164: validWords.add(words[i]);
165: }
166: }
167:
168: return (String[]) validWords.toArray(new String[0]);
169: }
170:
171: private static Log _log = LogFactory.getLog(WordsUtil.class);
172:
173: private static WordsUtil _instance = new WordsUtil();
174:
175: private List _dictionaryList;
176: private Set _dictionarySet;
177: private SpellDictionaryHashMap _spellDictionary;
178:
179: }
|