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