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