1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
12   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
13   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
14   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
15   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17   * SOFTWARE.
18   */
19  
20  package com.liferay.portlet.words.util;
21  
22  import com.liferay.portal.kernel.log.Log;
23  import com.liferay.portal.kernel.log.LogFactoryUtil;
24  import com.liferay.portal.kernel.util.ListUtil;
25  import com.liferay.portal.kernel.util.Randomizer;
26  import com.liferay.portal.kernel.util.StringUtil;
27  import com.liferay.portal.kernel.util.UnmodifiableList;
28  import com.liferay.portal.util.ContentUtil;
29  import com.liferay.portlet.words.ScramblerException;
30  import com.liferay.util.jazzy.BasicSpellCheckListener;
31  import com.liferay.util.jazzy.InvalidWord;
32  
33  import com.swabunga.spell.engine.SpellDictionaryHashMap;
34  import com.swabunga.spell.event.DefaultWordFinder;
35  import com.swabunga.spell.event.SpellChecker;
36  import com.swabunga.spell.event.StringWordTokenizer;
37  
38  import java.io.IOException;
39  import java.io.StringReader;
40  
41  import java.util.ArrayList;
42  import java.util.Collections;
43  import java.util.HashSet;
44  import java.util.List;
45  import java.util.Set;
46  
47  /**
48   * <a href="WordsUtil.java.html"><b><i>View Source</i></b></a>
49   *
50   * @author Brian Wing Shun Chan
51   *
52   */
53  public class WordsUtil {
54  
55      public static List<InvalidWord> checkSpelling(String text) {
56          return _instance._checkSpelling(text);
57      }
58  
59      public static List<String> getDictionaryList() {
60          return _instance._getDictionaryList();
61      }
62  
63      public static Set<String> getDictionarySet() {
64          return _instance._getDictionarySet();
65      }
66  
67      public static String getRandomWord() {
68          return _instance._getRandomWord();
69      }
70  
71      public static boolean isDictionaryWord(String word) {
72          return _instance._isDictionaryWord(word);
73      }
74  
75      public static String[] scramble(String word) throws ScramblerException {
76          Scrambler scrambler = new Scrambler(word);
77  
78          return scrambler.scramble();
79      }
80  
81      public static String[] unscramble(String word) throws ScramblerException {
82          return _instance._unscramble(word);
83      }
84  
85      private WordsUtil() {
86          _dictionaryList = ListUtil.fromArray(StringUtil.split(
87              ContentUtil.get("com/liferay/portlet/words/dependencies/words.txt"),
88              "\n"));
89  
90          _dictionaryList = new UnmodifiableList(_dictionaryList);
91  
92          _dictionarySet = new HashSet<String>(_dictionaryList.size());
93  
94          _dictionarySet.addAll(_dictionaryList);
95  
96          _dictionarySet = Collections.unmodifiableSet(_dictionarySet);
97  
98          try {
99              _spellDictionary = new SpellDictionaryHashMap();
100 
101             String[] dics = new String[] {
102                 "center.dic", "centre.dic", "color.dic", "colour.dic",
103                 "eng_com.dic", "english.0", "english.1", "ise.dic", "ize.dic",
104                 "labeled.dic", "labelled.dic", "yse.dic", "yze.dic"
105             };
106 
107             for (int i = 0; i < dics.length; i++) {
108                 _spellDictionary.addDictionary(new StringReader(
109                     ContentUtil.get(
110                         "com/liferay/portlet/words/dependencies/" + dics[i])));
111             }
112         }
113         catch (IOException ioe) {
114             _log.error(ioe);
115         }
116     }
117 
118     private List<InvalidWord> _checkSpelling(String text) {
119         SpellChecker checker = new SpellChecker(_spellDictionary);
120 
121         BasicSpellCheckListener listener = new BasicSpellCheckListener(text);
122 
123         checker.addSpellCheckListener(listener);
124 
125         checker.checkSpelling(
126             new StringWordTokenizer(new DefaultWordFinder(text)));
127 
128         return listener.getInvalidWords();
129     }
130 
131     private List<String> _getDictionaryList() {
132         return _dictionaryList;
133     }
134 
135     private Set<String> _getDictionarySet() {
136         return _dictionarySet;
137     }
138 
139     private String _getRandomWord() {
140         int pos = Randomizer.getInstance().nextInt(_dictionaryList.size());
141 
142         return _dictionaryList.get(pos);
143     }
144 
145     private boolean _isDictionaryWord(String word) {
146         return _dictionarySet.contains(word);
147     }
148 
149     private String[] _unscramble(String word) throws ScramblerException {
150         List<String> validWords = new ArrayList<String>();
151 
152         String[] words = scramble(word);
153 
154         for (int i = 0; i < words.length; i++) {
155             if (_dictionarySet.contains(words[i])) {
156                 validWords.add(words[i]);
157             }
158         }
159 
160         return validWords.toArray(new String[validWords.size()]);
161     }
162 
163     private static Log _log = LogFactoryUtil.getLog(WordsUtil.class);
164 
165     private static WordsUtil _instance = new WordsUtil();
166 
167     private List<String> _dictionaryList;
168     private Set<String> _dictionarySet;
169     private SpellDictionaryHashMap _spellDictionary;
170 
171 }