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
26 * This class tokenizes a input string.
\r
29 * It also allows for the string to be mutated. The result after the spell
\r
30 * checking is completed is available to the call to getFinalText
\r
33 * @author Jason Height(jheight@chariot.net.au)
\r
34 * @author Anthony Roy (ajr@antroy.co.uk)
\r
36 public abstract class AbstractWordTokenizer implements WordTokenizer {
\r
38 //~ Instance/static variables ...............................................
\r
40 /** The word being analyzed */
\r
41 protected Word currentWord;
\r
42 /** The word finder used to filter out words which are non pertinent to
\r
44 protected WordFinder finder;
\r
45 /** An iterator to work through the sentence */
\r
46 protected BreakIterator sentenceIterator;
\r
48 /** The cumulative word count that have been processed */
\r
49 protected int wordCount = 0;
\r
51 //~ Constructors ............................................................
\r
54 * Creates a new AbstractWordTokenizer object.
\r
56 * @param text the text to process.
\r
58 public AbstractWordTokenizer(String text) {
\r
59 this(new DefaultWordFinder(text));
\r
63 * Creates a new AbstractWordTokenizer object.
\r
65 * @param wf the custom WordFinder to use in searching for words.
\r
67 public AbstractWordTokenizer(WordFinder wf) {
\r
71 //~ Methods .................................................................
\r
74 * Returns the current number of words that have been processed
\r
76 * @return number of words so far iterated.
\r
78 public int getCurrentWordCount() {
\r
84 * Returns the end of the current word in the text
\r
86 * @return index in string of the end of the current word.
\r
87 * @throws WordNotFoundException current word has not yet been set.
\r
89 public int getCurrentWordEnd() {
\r
91 if (currentWord == null) {
\r
92 throw new WordNotFoundException("No Words in current String");
\r
95 return currentWord.getEnd();
\r
99 * Returns the index of the start of the current word in the text
\r
101 * @return index in string of the start of the current word.
\r
102 * @throws WordNotFoundException current word has not yet been set.
\r
104 public int getCurrentWordPosition() {
\r
106 if (currentWord == null) {
\r
107 throw new WordNotFoundException("No Words in current String");
\r
110 return currentWord.getStart();
\r
114 * Returns true if there are more words that can be processed in the string
\r
116 * @return true if there are further words in the text.
\r
118 public boolean hasMoreWords() {
\r
120 return finder.hasNext();
\r
124 * Returns searches for the next word in the text, and returns that word.
\r
126 * @return the string representing the current word.
\r
127 * @throws WordNotFoundException search string contains no more words.
\r
129 public String nextWord() {
\r
130 currentWord = finder.next();
\r
132 return currentWord.getText();
\r
136 * Replaces the current word token
\r
138 * @param newWord replacement word.
\r
139 * @throws WordNotFoundException current word has not yet been set.
\r
141 public abstract void replaceWord(String newWord);
\r
144 * Returns the current text that is being tokenized (includes any changes
\r
145 * that have been made)
\r
147 * @return the text being tokenized.
\r
149 public String getContext() {
\r
151 return finder.toString();
\r
155 * returns true if the current word is at the start of a sentence
\r
157 * @return true if the current word starts a sentence.
\r
158 * @throws WordNotFoundException current word has not yet been set.
\r
160 public boolean isNewSentence() {
\r
162 return finder.startsSentence();
\r