1   /**
2    * Copyright (c) 2000-2008 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.util.ListUtil;
26  import com.liferay.portal.kernel.util.Randomizer;
27  import com.liferay.portal.kernel.util.StringUtil;
28  import com.liferay.portal.kernel.util.UnmodifiableList;
29  import com.liferay.portal.util.ContentUtil;
30  import com.liferay.portlet.words.ScramblerException;
31  import com.liferay.util.jazzy.BasicSpellCheckListener;
32  import com.liferay.util.jazzy.InvalidWord;
33  
34  import com.swabunga.spell.engine.SpellDictionaryHashMap;
35  import com.swabunga.spell.event.DefaultWordFinder;
36  import com.swabunga.spell.event.SpellChecker;
37  import com.swabunga.spell.event.StringWordTokenizer;
38  
39  import java.io.IOException;
40  import java.io.StringReader;
41  
42  import java.util.ArrayList;
43  import java.util.Collections;
44  import java.util.HashSet;
45  import java.util.List;
46  import java.util.Set;
47  
48  import org.apache.commons.logging.Log;
49  import org.apache.commons.logging.LogFactory;
50  
51  /**
52   * <a href="WordsUtil.java.html"><b><i>View Source</i></b></a>
53   *
54   * @author Brian Wing Shun Chan
55   *
56   */
57  public class WordsUtil {
58  
59      public static List<InvalidWord> checkSpelling(String text) {
60          return _instance._checkSpelling(text);
61      }
62  
63      public static List<String> getDictionaryList() {
64          return _instance._getDictionaryList();
65      }
66  
67      public static Set<String> getDictionarySet() {
68          return _instance._getDictionarySet();
69      }
70  
71      public static String getRandomWord() {
72          return _instance._getRandomWord();
73      }
74  
75      public static boolean isDictionaryWord(String word) {
76          return _instance._isDictionaryWord(word);
77      }
78  
79      public static String[] scramble(String word) throws ScramblerException {
80          Scrambler scrambler = new Scrambler(word);
81  
82          return scrambler.scramble();
83      }
84  
85      public static String[] unscramble(String word) throws ScramblerException {
86          return _instance._unscramble(word);
87      }
88  
89      private WordsUtil() {
90          _dictionaryList = ListUtil.fromArray(StringUtil.split(
91              ContentUtil.get("com/liferay/portlet/words/dependencies/words.txt"),
92              "\n"));
93  
94          _dictionaryList = new UnmodifiableList(_dictionaryList);
95  
96          _dictionarySet = new HashSet<String>(_dictionaryList.size());
97  
98          _dictionarySet.addAll(_dictionaryList);
99  
100         _dictionarySet = Collections.unmodifiableSet(_dictionarySet);
101 
102         try {
103             _spellDictionary = new SpellDictionaryHashMap();
104 
105             String[] dics = new String[] {
106                 "center.dic", "centre.dic", "color.dic", "colour.dic",
107                 "eng_com.dic", "english.0", "english.1", "ise.dic", "ize.dic",
108                 "labeled.dic", "labelled.dic", "yse.dic", "yze.dic"
109             };
110 
111             for (int i = 0; i < dics.length; i++) {
112                 _spellDictionary.addDictionary(new StringReader(
113                     ContentUtil.get(
114                         "com/liferay/portlet/words/dependencies/" + dics[i])));
115             }
116         }
117         catch (IOException ioe) {
118             _log.error(ioe);
119         }
120     }
121 
122     private List<InvalidWord> _checkSpelling(String text) {
123         SpellChecker checker = new SpellChecker(_spellDictionary);
124 
125         BasicSpellCheckListener listener = new BasicSpellCheckListener(text);
126 
127         checker.addSpellCheckListener(listener);
128 
129         checker.checkSpelling(
130             new StringWordTokenizer(new DefaultWordFinder(text)));
131 
132         return listener.getInvalidWords();
133     }
134 
135     private List<String> _getDictionaryList() {
136         return _dictionaryList;
137     }
138 
139     private Set<String> _getDictionarySet() {
140         return _dictionarySet;
141     }
142 
143     private String _getRandomWord() {
144         int pos = Randomizer.getInstance().nextInt(_dictionaryList.size());
145 
146         return _dictionaryList.get(pos);
147     }
148 
149     private boolean _isDictionaryWord(String word) {
150         return _dictionarySet.contains(word);
151     }
152 
153     private String[] _unscramble(String word) throws ScramblerException {
154         List<String> validWords = new ArrayList<String>();
155 
156         String[] words = scramble(word);
157 
158         for (int i = 0; i < words.length; i++) {
159             if (_dictionarySet.contains(words[i])) {
160                 validWords.add(words[i]);
161             }
162         }
163 
164         return validWords.toArray(new String[validWords.size()]);
165     }
166 
167     private static Log _log = LogFactory.getLog(WordsUtil.class);
168 
169     private static WordsUtil _instance = new WordsUtil();
170 
171     private List<String> _dictionaryList;
172     private Set<String> _dictionarySet;
173     private SpellDictionaryHashMap _spellDictionary;
174 
175 }