001: /*
002: * Copyright 2001-2007 Hippo (www.hippo.nl)
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package nl.hippo.cms.spellchecking;
017:
018: import java.io.BufferedOutputStream;
019: import java.io.BufferedReader;
020: import java.io.IOException;
021: import java.io.InputStreamReader;
022: import java.io.PrintStream;
023: import java.util.List;
024: import java.util.StringTokenizer;
025: import java.util.Vector;
026:
027: /**
028: * This is a wrapper around the aspell command line utility
029: *
030: * @author Jeroen Reijn
031: */
032: public class AspellWrapper {
033: /** The process running the aspell command. */
034: private Process process = null;
035: private Process process1 = null;
036:
037: /** The standard output for the aspell command. */
038: private BufferedReader aspellOutputStream = null;
039: private BufferedReader aspellOutputStream1 = null;
040:
041: /** The standard input for the aspell command. */
042: private PrintStream aspellInputStream = null;
043:
044: /** The standard error for the aspell command. */
045: private BufferedReader aspellErrorStream = null;
046:
047: private String aSpellLocation = "";
048: private String aSpellDictionary = "";
049:
050: public AspellWrapper() {
051: //System.out.println("Start up aSpellWrapper");
052: }
053:
054: public void setupAspell() {
055: //System.out.println("Setup Aspell");
056: String[] aCommand = new String[5];
057: aCommand[0] = this .aSpellLocation + "aspell";
058: aCommand[1] = "-a";
059: aCommand[2] = "--keymapping=ispell";
060: aCommand[3] = "--lang=" + this .aSpellDictionary;
061: aCommand[4] = "--encoding=UTF-8";
062:
063: String[] envArray = new String[0];
064: try {
065: process = Runtime.getRuntime().exec(aCommand, envArray);
066: aspellOutputStream = new BufferedReader(
067: new InputStreamReader(process.getInputStream()));
068: aspellInputStream = new PrintStream(
069: new BufferedOutputStream(process.getOutputStream()),
070: true);
071: aspellErrorStream = new BufferedReader(
072: new InputStreamReader(process.getErrorStream()));
073: } catch (IOException ioe) {
074: System.out.println("An error has occured: " + ioe);
075: }
076: }
077:
078: public Vector getDictionaries() {
079: String[] aCommand = new String[3];
080: aCommand[0] = this .aSpellLocation + "aspell";
081: aCommand[1] = "dump";
082: aCommand[2] = "dicts";
083: Vector dicts = new Vector();
084:
085: String[] envArray = new String[0];
086: try {
087: process1 = Runtime.getRuntime().exec(aCommand, envArray);
088: aspellOutputStream1 = new BufferedReader(
089: new InputStreamReader(process1.getInputStream()));
090: String line = "";
091: while ((line = aspellOutputStream1.readLine()) != null) {
092: dicts.add(line);
093: }
094: aspellOutputStream1.close();
095: process1 = null;
096:
097: } catch (IOException ioe) {
098: System.out.println("An error has occured: " + ioe);
099: }
100: return dicts;
101: }
102:
103: /**
104: * Find spelling corrections for a given misspelled word.
105: *
106: * @param pTerm The word to correct.
107: * @return An array of possible corrections.
108: */
109: public String[] find(String pTerm) {
110: String[] candidates = null;
111: String badTerm = pTerm.trim().toLowerCase();
112: aspellInputStream.flush();
113: aspellInputStream.println(badTerm);
114: aspellInputStream.flush();
115: try {
116: String line = aspellOutputStream.readLine();
117: line = aspellOutputStream.readLine();
118: if (line.trim().length() <= 0)
119: aspellInputStream.println();
120: candidates = convertFromAspell(pTerm, line);
121: } catch (Exception e) {
122: }
123: return (candidates);
124: }
125:
126: /**
127: * Find the best spelling correction for a given misspelled word.
128: *
129: * @param pTerm The word to correct.
130: * @return The best possible correction.
131: */
132: public String findMostSimilar(String pTerm) {
133: String[] candidates = find(pTerm);
134: if (candidates == null || candidates.length == 0)
135: return null;
136: return candidates[0];
137: }
138:
139: /**
140: * Find spelling corrections for a given misspelled word.
141: *
142: * @param pTerm The word to correct.
143: * @return A <code>List</code> with the possible corrections.
144: */
145: public List findMostSimilarList(String pTerm) {
146: String[] candidates = find(pTerm);
147: List aux = new Vector();
148: if (candidates != null) {
149: for (int i = 0; i < candidates.length; i++)
150: aux.add(candidates[i]);
151: }
152: return aux;
153: }
154:
155: /**
156: * Converts the result from the aspell spelling checker
157: * into a <code>String</code> array with the possible suggestions.
158: *
159: * @param pTerm The correctly spelled word.
160: * @param pSuggestions A <code>String</code> with the suggestions in aspell format.
161: * @return An array with the suggestions.
162: */
163: private String[] convertFromAspell(String pTerm, String pSuggestions) {
164: String[] candidates = null;
165: int numberOfCandidates = 0;
166: try {
167: if (pSuggestions.equals("*")) {
168: candidates = new String[1];
169: candidates[numberOfCandidates] = pTerm;
170: numberOfCandidates++;
171: } else {
172: StringTokenizer st = new StringTokenizer(pSuggestions,
173: ":", false);
174: if (st.hasMoreTokens()) {
175: st.nextToken();
176: String stuffAfterColon = st.nextToken();
177: StringTokenizer st2 = new StringTokenizer(
178: stuffAfterColon, ",", false);
179: candidates = new String[st2.countTokens()];
180: String suggestion = null;
181: while (st2.hasMoreTokens()) {
182: suggestion = st2.nextToken().trim()
183: .toLowerCase();
184: candidates[numberOfCandidates] = suggestion;
185: numberOfCandidates++;
186: }
187: st2 = null;
188: st = null;
189: }
190: }
191: } catch (Exception e) {
192: }
193: return (candidates);
194: }
195:
196: public void setASpellDictionary(String dictionary) {
197: this .aSpellDictionary = dictionary;
198: }
199:
200: public void setASpellLocation(String location) {
201: this .aSpellLocation = location;
202: }
203:
204: /**
205: * Cleanup the process running aspell.
206: */
207: public void cleanup() {
208: try {
209: aspellOutputStream.close();
210: aspellInputStream.close();
211: aspellErrorStream.close();
212: process = null;
213: } catch (Exception e) {
214: System.out.println("CLEANUP: " + e);
215: }
216: }
217: }
|