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