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
23 * A basic word finder, which searches text for sequences of letters.
\r
25 * @author Anthony Roy (ajr@antroy.co.uk)
\r
27 public class DefaultWordFinder extends AbstractWordFinder {
\r
29 //~ Instance/static variables ...............................................
\r
31 //~ Constructors ............................................................
\r
34 * Creates a new DefaultWordFinder object.
\r
36 * @param inText the String to search
\r
38 public DefaultWordFinder(String inText) {
\r
43 * Creates a new DefaultWordFinder object.
\r
45 public DefaultWordFinder() {
\r
49 //~ Methods .................................................................
\r
52 * This method scans the text from the end of the last word, and returns a
\r
53 * new Word object corresponding to the next word.
\r
55 * @return the next word.
\r
56 * @throws WordNotFoundException search string contains no more words.
\r
58 public Word next() {
\r
59 if (nextWord == null) {
\r
60 throw new WordNotFoundException("No more words found.");
\r
62 currentWord.copy(nextWord);
\r
63 setSentenceIterator(currentWord);
\r
65 int i = currentWord.getEnd();
\r
66 boolean finished = false;
\r
68 while (i < text.length() && !finished) {
\r
69 if (isWordChar(i)) {
\r
70 nextWord.setStart(i);
\r
71 int end = getNextWordEnd(text, i);
\r
72 nextWord.setText(text.substring(i, end));
\r
84 * Returns the position in the string <em>after</em> the end of the next word.
\r
85 * Note that this return value should not be used as an index into the string
\r
86 * without checking first that it is in range, since it is possible for the
\r
87 * value <code>text.length()</code> to be returned by this method.
\r
89 private int getNextWordEnd(String text, int startPos) {
\r
90 // If we're dealing with a possible 'internet word' we need to provide
\r
91 // some special handling
\r
92 if (SpellChecker.isINETWord(text.substring(startPos))) {
\r
93 for (int i = startPos; i < text.length(); i++) {
\r
94 char ch = text.charAt(i);
\r
95 if (Character.isLetterOrDigit(ch))
\r
98 if (ch == '\r' || ch == '\n')
\r
100 // Chop off any characters that might be enclosing the 'internet word'. eg ',",),]
\r
101 if (Character.isSpaceChar(ch))
\r
102 if (i > 0 && Character.isLetterOrDigit(text.charAt(i - 1)))
\r
107 return text.length();
\r
109 for (int i = startPos; i < text.length(); i++) {
\r
110 if (!isWordChar(i))
\r
113 return text.length();
\r