001
014
015 package com.liferay.portlet.words.util;
016
017 import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
018 import com.liferay.portal.kernel.log.Log;
019 import com.liferay.portal.kernel.log.LogFactoryUtil;
020 import com.liferay.portal.kernel.util.ListUtil;
021 import com.liferay.portal.kernel.util.Randomizer;
022 import com.liferay.portal.kernel.util.StringUtil;
023 import com.liferay.portal.kernel.util.UnmodifiableList;
024 import com.liferay.portal.util.ContentUtil;
025 import com.liferay.portlet.words.ScramblerException;
026 import com.liferay.util.jazzy.BasicSpellCheckListener;
027 import com.liferay.util.jazzy.InvalidWord;
028
029 import com.swabunga.spell.engine.SpellDictionaryHashMap;
030 import com.swabunga.spell.event.DefaultWordFinder;
031 import com.swabunga.spell.event.SpellChecker;
032 import com.swabunga.spell.event.StringWordTokenizer;
033
034 import java.io.IOException;
035
036 import java.util.ArrayList;
037 import java.util.Collections;
038 import java.util.HashSet;
039 import java.util.List;
040 import java.util.Set;
041
042
045 public class WordsUtil {
046
047 public static List<InvalidWord> checkSpelling(String text) {
048 return _instance._checkSpelling(text);
049 }
050
051 public static List<String> getDictionaryList() {
052 return _instance._getDictionaryList();
053 }
054
055 public static Set<String> getDictionarySet() {
056 return _instance._getDictionarySet();
057 }
058
059 public static String getRandomWord() {
060 return _instance._getRandomWord();
061 }
062
063 public static boolean isDictionaryWord(String word) {
064 return _instance._isDictionaryWord(word);
065 }
066
067 public static String[] scramble(String word) throws ScramblerException {
068 Scrambler scrambler = new Scrambler(word);
069
070 return scrambler.scramble();
071 }
072
073 public static String[] unscramble(String word) throws ScramblerException {
074 return _instance._unscramble(word);
075 }
076
077 private WordsUtil() {
078 _dictionaryList = ListUtil.fromArray(StringUtil.split(
079 ContentUtil.get("com/liferay/portlet/words/dependencies/words.txt"),
080 "\n"));
081
082 _dictionaryList = new UnmodifiableList<String>(_dictionaryList);
083
084 _dictionarySet = new HashSet<String>(_dictionaryList.size());
085
086 _dictionarySet.addAll(_dictionaryList);
087
088 _dictionarySet = Collections.unmodifiableSet(_dictionarySet);
089
090 try {
091 _spellDictionary = new SpellDictionaryHashMap();
092
093 String[] dics = new String[] {
094 "center.dic", "centre.dic", "color.dic", "colour.dic",
095 "eng_com.dic", "english.0", "english.1", "ise.dic", "ize.dic",
096 "labeled.dic", "labelled.dic", "yse.dic", "yze.dic"
097 };
098
099 for (int i = 0; i < dics.length; i++) {
100 _spellDictionary.addDictionary(new UnsyncStringReader(
101 ContentUtil.get(
102 "com/liferay/portlet/words/dependencies/" + dics[i])));
103 }
104 }
105 catch (IOException ioe) {
106 _log.error(ioe);
107 }
108 }
109
110 private List<InvalidWord> _checkSpelling(String text) {
111 SpellChecker checker = new SpellChecker(_spellDictionary);
112
113 BasicSpellCheckListener listener = new BasicSpellCheckListener(text);
114
115 checker.addSpellCheckListener(listener);
116
117 checker.checkSpelling(
118 new StringWordTokenizer(new DefaultWordFinder(text)));
119
120 return listener.getInvalidWords();
121 }
122
123 private List<String> _getDictionaryList() {
124 return _dictionaryList;
125 }
126
127 private Set<String> _getDictionarySet() {
128 return _dictionarySet;
129 }
130
131 private String _getRandomWord() {
132 int pos = Randomizer.getInstance().nextInt(_dictionaryList.size());
133
134 return _dictionaryList.get(pos);
135 }
136
137 private boolean _isDictionaryWord(String word) {
138 return _dictionarySet.contains(word);
139 }
140
141 private String[] _unscramble(String word) throws ScramblerException {
142 List<String> validWords = new ArrayList<String>();
143
144 String[] words = scramble(word);
145
146 for (int i = 0; i < words.length; i++) {
147 if (_dictionarySet.contains(words[i])) {
148 validWords.add(words[i]);
149 }
150 }
151
152 return validWords.toArray(new String[validWords.size()]);
153 }
154
155 private static Log _log = LogFactoryUtil.getLog(WordsUtil.class);
156
157 private static WordsUtil _instance = new WordsUtil();
158
159 private List<String> _dictionaryList;
160 private Set<String> _dictionarySet;
161 private SpellDictionaryHashMap _spellDictionary;
162
163 }