1
22
23 package com.liferay.portlet.words.util;
24
25 import com.liferay.portal.kernel.util.ListUtil;
26 import com.liferay.portal.kernel.util.Randomizer;
27 import com.liferay.portal.kernel.util.StringUtil;
28 import com.liferay.portal.kernel.util.UnmodifiableList;
29 import com.liferay.portal.util.ContentUtil;
30 import com.liferay.portlet.words.ScramblerException;
31 import com.liferay.util.jazzy.BasicSpellCheckListener;
32 import com.liferay.util.jazzy.InvalidWord;
33
34 import com.swabunga.spell.engine.SpellDictionaryHashMap;
35 import com.swabunga.spell.event.DefaultWordFinder;
36 import com.swabunga.spell.event.SpellChecker;
37 import com.swabunga.spell.event.StringWordTokenizer;
38
39 import java.io.IOException;
40 import java.io.StringReader;
41
42 import java.util.ArrayList;
43 import java.util.Collections;
44 import java.util.HashSet;
45 import java.util.List;
46 import java.util.Set;
47
48 import org.apache.commons.logging.Log;
49 import org.apache.commons.logging.LogFactory;
50
51
57 public class WordsUtil {
58
59 public static List<InvalidWord> checkSpelling(String text) {
60 return _instance._checkSpelling(text);
61 }
62
63 public static List<String> getDictionaryList() {
64 return _instance._getDictionaryList();
65 }
66
67 public static Set<String> getDictionarySet() {
68 return _instance._getDictionarySet();
69 }
70
71 public static String getRandomWord() {
72 return _instance._getRandomWord();
73 }
74
75 public static boolean isDictionaryWord(String word) {
76 return _instance._isDictionaryWord(word);
77 }
78
79 public static String[] scramble(String word) throws ScramblerException {
80 Scrambler scrambler = new Scrambler(word);
81
82 return scrambler.scramble();
83 }
84
85 public static String[] unscramble(String word) throws ScramblerException {
86 return _instance._unscramble(word);
87 }
88
89 private WordsUtil() {
90 _dictionaryList = ListUtil.fromArray(StringUtil.split(
91 ContentUtil.get("com/liferay/portlet/words/dependencies/words.txt"),
92 "\n"));
93
94 _dictionaryList = new UnmodifiableList(_dictionaryList);
95
96 _dictionarySet = new HashSet<String>(_dictionaryList.size());
97
98 _dictionarySet.addAll(_dictionaryList);
99
100 _dictionarySet = Collections.unmodifiableSet(_dictionarySet);
101
102 try {
103 _spellDictionary = new SpellDictionaryHashMap();
104
105 String[] dics = new String[] {
106 "center.dic", "centre.dic", "color.dic", "colour.dic",
107 "eng_com.dic", "english.0", "english.1", "ise.dic", "ize.dic",
108 "labeled.dic", "labelled.dic", "yse.dic", "yze.dic"
109 };
110
111 for (int i = 0; i < dics.length; i++) {
112 _spellDictionary.addDictionary(new StringReader(
113 ContentUtil.get(
114 "com/liferay/portlet/words/dependencies/" + dics[i])));
115 }
116 }
117 catch (IOException ioe) {
118 _log.error(ioe);
119 }
120 }
121
122 private List<InvalidWord> _checkSpelling(String text) {
123 SpellChecker checker = new SpellChecker(_spellDictionary);
124
125 BasicSpellCheckListener listener = new BasicSpellCheckListener(text);
126
127 checker.addSpellCheckListener(listener);
128
129 checker.checkSpelling(
130 new StringWordTokenizer(new DefaultWordFinder(text)));
131
132 return listener.getInvalidWords();
133 }
134
135 private List<String> _getDictionaryList() {
136 return _dictionaryList;
137 }
138
139 private Set<String> _getDictionarySet() {
140 return _dictionarySet;
141 }
142
143 private String _getRandomWord() {
144 int pos = Randomizer.getInstance().nextInt(_dictionaryList.size());
145
146 return _dictionaryList.get(pos);
147 }
148
149 private boolean _isDictionaryWord(String word) {
150 return _dictionarySet.contains(word);
151 }
152
153 private String[] _unscramble(String word) throws ScramblerException {
154 List<String> validWords = new ArrayList<String>();
155
156 String[] words = scramble(word);
157
158 for (int i = 0; i < words.length; i++) {
159 if (_dictionarySet.contains(words[i])) {
160 validWords.add(words[i]);
161 }
162 }
163
164 return validWords.toArray(new String[validWords.size()]);
165 }
166
167 private static Log _log = LogFactory.getLog(WordsUtil.class);
168
169 private static WordsUtil _instance = new WordsUtil();
170
171 private List<String> _dictionaryList;
172 private Set<String> _dictionarySet;
173 private SpellDictionaryHashMap _spellDictionary;
174
175 }