2 Jazzy - a Java library for Spell Checking
\r
3 Copyright (C) 2001 Mindaugas Idzelis
\r
4 Full text of license can be found in LICENSE.txt
\r
6 This library is free software; you can redistribute it and/or
\r
7 modify it under the terms of the GNU Lesser General Public
\r
8 License as published by the Free Software Foundation; either
\r
9 version 2.1 of the License, or (at your option) any later version.
\r
11 This library is distributed in the hope that it will be useful,
\r
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
\r
14 Lesser General Public License for more details.
\r
16 You should have received a copy of the GNU Lesser General Public
\r
17 License along with this library; if not, write to the Free Software
\r
18 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
\r
20 package com.swabunga.spell.event;
\r
22 import java.text.BreakIterator;
\r
25 * Defines common methods and behaviour for the various word finding
\r
28 * @author Anthony Roy (ajr@antroy.co.uk)
\r
30 public abstract class AbstractWordFinder implements WordFinder {
\r
32 //~ Instance/static variables .............................................
\r
34 /** The word being analyzed */
\r
35 protected Word currentWord;
\r
36 /** The word following the current one */
\r
37 protected Word nextWord;
\r
38 /** Indicate if the current word starts a new sentence */
\r
39 protected boolean startsSentence;
\r
40 /** Holds the text to analyze */
\r
41 protected String text;
\r
42 /** An iterator to work through the sentence */
\r
43 protected BreakIterator sentenceIterator;
\r
45 //~ Constructors ..........................................................
\r
48 * Creates a new AbstractWordFinder object.
\r
50 * @param inText the String to iterate through.
\r
52 public AbstractWordFinder(String inText) {
\r
58 * Creates a new default AbstractWordFinder object.
\r
60 public AbstractWordFinder() {
\r
64 //~ Methods ...............................................................
\r
67 * This method scans the text from the end of the last word, and returns
\r
68 * a new Word object corresponding to the next word.
\r
70 * @return the following word.
\r
72 public abstract Word next();
\r
75 * Return the text being searched. May have changed since first set
\r
76 * through calls to replace.
\r
78 * @return the text being searched.
\r
80 public String getText() {
\r
86 * Defines the text to search.
\r
87 * @param newText The text to be analyzed
\r
89 public void setText(String newText) {
\r
95 * Returns the current word in the iteration .
\r
97 * @return the current word.
\r
98 * @throws WordNotFoundException current word has not yet been set.
\r
100 public Word current() {
\r
102 if (currentWord == null) {
\r
103 throw new WordNotFoundException("No Words in current String");
\r
106 return currentWord;
\r
110 * Indicates if there is some more word to analyze
\r
111 * @return true if there are further words in the string.
\r
113 public boolean hasNext() {
\r
115 return nextWord != null;
\r
120 * Replace the current word in the search with a replacement string.
\r
122 * @param newWord the replacement string.
\r
123 * @throws WordNotFoundException current word has not yet been set.
\r
125 public void replace(String newWord) {
\r
127 if (currentWord == null) {
\r
128 throw new WordNotFoundException("No Words in current String");
\r
131 StringBuffer sb = new StringBuffer(text.substring(0, currentWord.getStart()));
\r
132 sb.append(newWord);
\r
133 sb.append(text.substring(currentWord.getEnd()));
\r
134 int diff = newWord.length() - currentWord.getText().length();
\r
135 currentWord.setText(newWord);
\r
136 /* Added Conditional to ensure a NullPointerException is avoided (11 Feb 2003) */
\r
137 if (nextWord != null) {
\r
138 nextWord.setStart(nextWord.getStart() + diff);
\r
140 text = sb.toString();
\r
142 sentenceIterator.setText(text);
\r
143 int start = currentWord.getStart();
\r
144 sentenceIterator.following(start);
\r
145 startsSentence = sentenceIterator.current() == start;
\r
150 * @return true if the current word starts a new sentence.
\r
151 * @throws WordNotFoundException current word has not yet been set.
\r
153 public boolean startsSentence() {
\r
155 if (currentWord == null) {
\r
156 throw new WordNotFoundException("No Words in current String");
\r
159 return startsSentence;
\r
163 * Return the text being searched. May have changed since first set
\r
164 * through calls to replace.
\r
166 * @return the text being searched.
\r
168 public String toString() {
\r
174 * Adjusts the sentence iterator and the startSentence flag according to the
\r
176 * @param wd the wd parameter is not presently used.
\r
178 protected void setSentenceIterator(Word wd) {
\r
179 int current = sentenceIterator.current();
\r
181 if (current == currentWord.getStart())
\r
182 startsSentence = true;
\r
184 startsSentence = false;
\r
186 if (currentWord.getEnd() > current) {
\r
187 sentenceIterator.next();
\r
193 * Indicates if the character at the specified position is acceptable as
\r
194 * part of a word. To be acceptable, the character need to be a letter
\r
195 * or a digit. It is also acceptable if the character is one of ''', '@',
\r
196 * '.' or '_' and is preceded and followed by letter or digit.
\r
197 * @param posn The character position to analyze.
\r
198 * @return true if the character is a letter or digit
\r
200 //Added more intelligent character recognition (11 Feb '03)
\r
201 protected boolean isWordChar(int posn) {
\r
202 boolean out = false;
\r
204 char curr = text.charAt(posn);
\r
206 if ((posn == 0) || (posn == text.length() - 1)) {
\r
207 return Character.isLetterOrDigit(curr);
\r
210 char prev = text.charAt(posn - 1);
\r
211 char next = text.charAt(posn + 1);
\r
219 out = (Character.isLetterOrDigit(prev) && Character.isLetterOrDigit(next));
\r
222 out = Character.isLetterOrDigit(curr);
\r
229 * Indicates if the character at the specified character is acceptable as
\r
230 * part of a word. To be acceptable, the character need to be a letter
\r
231 * or a digit or a ' (an apostrophe).
\r
232 * @param c The character to evaluates if it can be part of a word
\r
233 * @return true if the character is a letter, digit or a ' (an apostrophe).
\r
235 protected boolean isWordChar(char c) {
\r
236 boolean out = false;
\r
238 if (Character.isLetterOrDigit(c) || (c == '\'')) {
\r
246 * Ignores or skip over text starting from the index position specified
\r
247 * if it contains the <code>startIgnore</code>, and until the
\r
248 * first non letter or digit character is encountered or end of text is
\r
250 * @param index The start position in text.
\r
251 * @param startIgnore The character that should be at <code>index</code>
\r
252 * position to start skipping through.
\r
253 * @return The index position pointing after the skipped characters or the
\r
254 * original index if the ignore condition could not be met.
\r
256 protected int ignore(int index, char startIgnore) {
\r
257 return ignore(index, new Character(startIgnore), null);
\r
261 * Ignores or skip over text starting from the index position specified
\r
262 * if it contains the <code>startIgnore</code>, and until the
\r
263 * <code>endIgnore</code> character is encountered or end of text is
\r
265 * @param index The start position in text.
\r
266 * @param startIgnore The character that should be at <code>index</code>
\r
267 * position to start skipping through.
\r
268 * @param endIgnore The character which mark the end of skipping through. If
\r
269 * the value of endIgnore is <code>null</code>, skipping characters stop
\r
270 * at first non letter or digit character.
\r
271 * @return The index position pointing after the skipped characters or the
\r
272 * original index if the ignore condition could not be met.
\r
274 protected int ignore(int index, char startIgnore, char endIgnore) {
\r
275 return ignore(index, new Character(startIgnore), new Character(endIgnore));
\r
279 * Ignores or skip over text starting from the index position specified
\r
280 * if it contains the <code>startIgnore</code>, and until the
\r
281 * <code>endIgnore</code> character is encountered or end of text is
\r
283 * @param index The start position in text.
\r
284 * @param startIgnore The character that should be at <code>index</code>
\r
285 * position to start skipping through.
\r
286 * @param endIgnore The character which mark the end of skipping through. If
\r
287 * the value of endIgnore is <code>null</code>, skipping characters stop
\r
288 * at first non letter or digit character.
\r
289 * @return The index position pointing after the skipped characters or the
\r
290 * original index if the ignore condition could not be met.
\r
292 protected int ignore(int index, Character startIgnore, Character endIgnore) {
\r
293 int newIndex = index;
\r
295 if (newIndex < text.length()) {
\r
296 Character curChar = new Character(text.charAt(newIndex));
\r
298 if (curChar.equals(startIgnore)) {
\r
300 while (newIndex < text.length()) {
\r
301 curChar = new Character(text.charAt(newIndex));
\r
302 if (endIgnore != null && curChar.equals(endIgnore)){
\r
305 } else if (endIgnore == null && !Character.isLetterOrDigit(curChar.charValue())){
\r
317 * Ignores or skip over text starting from the index position specified
\r
318 * if it contains the <code>startIgnore</code> string, and until the
\r
319 * <code>endIgnore</code> string is encountered or end of text is
\r
321 * @param index The start position in text.
\r
322 * @param startIgnore The string that should be at <code>index</code>
\r
323 * position to start skipping through.
\r
324 * @param endIgnore The string which mark the end of skipping through.
\r
325 * @return The index position pointing after the skipped characters or the
\r
326 * original index if the ignore condition could not be met.
\r
328 protected int ignore(int index, String startIgnore, String endIgnore) {
\r
331 int newIndex = index;
\r
332 int len = text.length();
\r
333 int slen = startIgnore.length();
\r
334 int elen = endIgnore.length();
\r
336 if (!((newIndex + slen) >= len)) {
\r
337 String seg = text.substring(newIndex, newIndex + slen);
\r
339 // System.out.println(seg + ":" + seg.length()+ ":" + startIgnore + ":" + slen);
\r
340 if (seg.equals(startIgnore)) {
\r
342 cycle: while (true) {
\r
344 if (newIndex == (text.length() - elen)) {
\r
349 String ss = text.substring(newIndex, newIndex + elen);
\r
351 if (ss.equals(endIgnore)) {
\r
366 * Initializes the sentenseIterator
\r
368 protected void init() {
\r
369 sentenceIterator = BreakIterator.getSentenceInstance();
\r
370 sentenceIterator.setText(text);
\r
374 * Defines the starting positions for text analysis
\r
376 private void setup() {
\r
377 currentWord = new Word("", 0);
\r
378 nextWord = new Word("", 0);
\r
379 startsSentence = true;
\r
385 } catch (WordNotFoundException e) {
\r
386 currentWord = null;
\r