001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
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    /**
041     * @author Brian Wing Shun Chan
042     */
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(StringUtil.splitLines(
067                            ContentUtil.get(
068                                    "com/liferay/portal/words/dependencies/words.txt")));
069    
070                    _dictionaryList = new UnmodifiableList<String>(_dictionaryList);
071    
072                    _dictionarySet = new HashSet<String>(_dictionaryList.size());
073    
074                    _dictionarySet.addAll(_dictionaryList);
075    
076                    _dictionarySet = Collections.unmodifiableSet(_dictionarySet);
077    
078                    try {
079                            _spellDictionaryHashMap = new SpellDictionaryHashMap();
080    
081                            String[] dics = new String[] {
082                                    "center.dic", "centre.dic", "color.dic", "colour.dic",
083                                    "eng_com.dic", "english.0", "english.1", "ise.dic", "ize.dic",
084                                    "labeled.dic", "labelled.dic", "yse.dic", "yze.dic"
085                            };
086    
087                            for (int i = 0; i < dics.length; i++) {
088                                    _spellDictionaryHashMap.addDictionary(new UnsyncStringReader(
089                                            ContentUtil.get(
090                                                    "com/liferay/portal/words/dependencies/" + dics[i])));
091                            }
092                    }
093                    catch (IOException ioe) {
094                            _log.error(ioe);
095                    }
096            }
097    
098            private List<InvalidWord> _checkSpelling(String text) {
099                    SpellChecker spellChecker = new SpellChecker(_spellDictionaryHashMap);
100    
101                    BasicSpellCheckListener basicSpellCheckListener =
102                            new BasicSpellCheckListener(text);
103    
104                    spellChecker.addSpellCheckListener(basicSpellCheckListener);
105    
106                    spellChecker.checkSpelling(
107                            new StringWordTokenizer(new DefaultWordFinder(text)));
108    
109                    return basicSpellCheckListener.getInvalidWords();
110            }
111    
112            private List<String> _getDictionaryList() {
113                    return _dictionaryList;
114            }
115    
116            private Set<String> _getDictionarySet() {
117                    return _dictionarySet;
118            }
119    
120            private String _getRandomWord() {
121                    int pos = Randomizer.getInstance().nextInt(_dictionaryList.size());
122    
123                    return _dictionaryList.get(pos);
124            }
125    
126            private boolean _isDictionaryWord(String word) {
127                    return _dictionarySet.contains(word);
128            }
129    
130            private static Log _log = LogFactoryUtil.getLog(WordsUtil.class);
131    
132            private static WordsUtil _instance = new WordsUtil();
133    
134            private List<String> _dictionaryList;
135            private Set<String> _dictionarySet;
136            private SpellDictionaryHashMap _spellDictionaryHashMap;
137    
138    }