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