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