Source Code Cross Referenced for AspellWrapper.java in  » Content-Management-System » hippo-cms » nl » hippo » cms » spellchecking » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Content Management System » hippo cms » nl.hippo.cms.spellchecking 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.