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