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