001
014
015 package com.liferay.portal.words;
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.security.SecureRandomUtil;
021 import com.liferay.portal.kernel.util.ListUtil;
022 import com.liferay.portal.kernel.util.StringUtil;
023 import com.liferay.portal.kernel.util.UnmodifiableList;
024 import com.liferay.util.ContentUtil;
025 import com.liferay.util.jazzy.BasicSpellCheckListener;
026 import com.liferay.util.jazzy.InvalidWord;
027
028 import com.swabunga.spell.engine.SpellDictionaryHashMap;
029 import com.swabunga.spell.event.DefaultWordFinder;
030 import com.swabunga.spell.event.SpellChecker;
031 import com.swabunga.spell.event.StringWordTokenizer;
032
033 import java.io.IOException;
034
035 import java.util.Collections;
036 import java.util.HashSet;
037 import java.util.List;
038 import java.util.Random;
039 import java.util.Set;
040
041
044 public class WordsUtil {
045
046 public static List<InvalidWord> checkSpelling(String text) {
047 return _instance._checkSpelling(text);
048 }
049
050 public static List<String> getDictionaryList() {
051 return _instance._getDictionaryList();
052 }
053
054 public static Set<String> getDictionarySet() {
055 return _instance._getDictionarySet();
056 }
057
058 public static String getRandomWord() {
059 return _instance._getRandomWord();
060 }
061
062 public static boolean isDictionaryWord(String word) {
063 return _instance._isDictionaryWord(word);
064 }
065
066 private WordsUtil() {
067 _dictionaryList = ListUtil.fromArray(
068 StringUtil.splitLines(
069 ContentUtil.get(
070 "com/liferay/portal/words/dependencies/words.txt")));
071
072 _dictionaryList = new UnmodifiableList<String>(_dictionaryList);
073
074 _dictionarySet = new HashSet<String>(_dictionaryList.size());
075
076 _dictionarySet.addAll(_dictionaryList);
077
078 _dictionarySet = Collections.unmodifiableSet(_dictionarySet);
079
080 try {
081 _spellDictionaryHashMap = new SpellDictionaryHashMap();
082
083 String[] dics = new String[] {
084 "center.dic", "centre.dic", "color.dic", "colour.dic",
085 "eng_com.dic", "english.0", "english.1", "ise.dic", "ize.dic",
086 "labeled.dic", "labelled.dic", "yse.dic", "yze.dic"
087 };
088
089 for (int i = 0; i < dics.length; i++) {
090 _spellDictionaryHashMap.addDictionary(
091 new UnsyncStringReader(
092 ContentUtil.get(
093 "com/liferay/portal/words/dependencies/" +
094 dics[i])));
095 }
096 }
097 catch (IOException ioe) {
098 _log.error(ioe);
099 }
100 }
101
102 private List<InvalidWord> _checkSpelling(String text) {
103 SpellChecker spellChecker = new SpellChecker(_spellDictionaryHashMap);
104
105 BasicSpellCheckListener basicSpellCheckListener =
106 new BasicSpellCheckListener(text);
107
108 spellChecker.addSpellCheckListener(basicSpellCheckListener);
109
110 spellChecker.checkSpelling(
111 new StringWordTokenizer(new DefaultWordFinder(text)));
112
113 return basicSpellCheckListener.getInvalidWords();
114 }
115
116 private List<String> _getDictionaryList() {
117 return _dictionaryList;
118 }
119
120 private Set<String> _getDictionarySet() {
121 return _dictionarySet;
122 }
123
124 private String _getRandomWord() {
125 Random random = new Random(SecureRandomUtil.nextLong());
126
127 int pos = random.nextInt(_dictionaryList.size());
128
129 return _dictionaryList.get(pos);
130 }
131
132 private boolean _isDictionaryWord(String word) {
133 return _dictionarySet.contains(word);
134 }
135
136 private static Log _log = LogFactoryUtil.getLog(WordsUtil.class);
137
138 private static WordsUtil _instance = new WordsUtil();
139
140 private List<String> _dictionaryList;
141 private Set<String> _dictionarySet;
142 private SpellDictionaryHashMap _spellDictionaryHashMap;
143
144 }