OSDN Git Service

NeverNote 0.88.
[neighbornote/NeighborNote.git] / src / com / swabunga / spell / event / AbstractWordFinder.java
1 /*\r
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
5 \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
10 \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
15 \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
19 */\r
20 package com.swabunga.spell.event;\r
21 \r
22 import java.text.BreakIterator;\r
23 \r
24 /**\r
25  * Defines common methods and behaviour for the various word finding\r
26  * subclasses.\r
27  *\r
28  * @author Anthony Roy  (ajr@antroy.co.uk)\r
29  */\r
30 public abstract class AbstractWordFinder implements WordFinder {\r
31 \r
32   //~ Instance/static variables .............................................\r
33 \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
44 \r
45   //~ Constructors ..........................................................\r
46 \r
47   /**\r
48    * Creates a new AbstractWordFinder object.\r
49    *\r
50    * @param inText the String to iterate through.\r
51    */\r
52   public AbstractWordFinder(String inText) {\r
53     text = inText;\r
54     setup();\r
55   }\r
56 \r
57   /**\r
58    * Creates a new default AbstractWordFinder object.\r
59    */\r
60   public AbstractWordFinder() {\r
61     text = "";\r
62     setup();\r
63   }\r
64   //~ Methods ...............................................................\r
65 \r
66   /**\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
69    *\r
70    * @return the following word.\r
71    */\r
72   public abstract Word next();\r
73 \r
74   /**\r
75    * Return the text being searched. May have changed since first set\r
76    * through calls to replace.\r
77    *\r
78    * @return the text being searched.\r
79    */\r
80   public String getText() {\r
81 \r
82     return text;\r
83   }\r
84   \r
85   /**\r
86    * Defines the text to search.\r
87    * @param newText The text to be analyzed\r
88    */\r
89   public void setText(String newText) {\r
90     text = newText;\r
91     setup();\r
92   }\r
93 \r
94   /**\r
95    * Returns the current word in the iteration .\r
96    *\r
97    * @return the current word.\r
98    * @throws WordNotFoundException current word has not yet been set.\r
99    */\r
100   public Word current() {\r
101 \r
102     if (currentWord == null) {\r
103       throw new WordNotFoundException("No Words in current String");\r
104     }\r
105 \r
106     return currentWord;\r
107   }\r
108 \r
109   /**\r
110    * Indicates if there is some more word to analyze\r
111    * @return true if there are further words in the string.\r
112    */\r
113   public boolean hasNext() {\r
114 \r
115     return nextWord != null;\r
116 \r
117   }\r
118 \r
119   /**\r
120    * Replace the current word in the search with a replacement string.\r
121    *\r
122    * @param newWord the replacement string.\r
123    * @throws WordNotFoundException current word has not yet been set.\r
124    */\r
125   public void replace(String newWord) {\r
126 \r
127     if (currentWord == null) {\r
128       throw new WordNotFoundException("No Words in current String");\r
129     }\r
130 \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
139     }\r
140     text = sb.toString();\r
141 \r
142     sentenceIterator.setText(text);\r
143     int start = currentWord.getStart();\r
144     sentenceIterator.following(start);\r
145     startsSentence = sentenceIterator.current() == start;\r
146 \r
147   }\r
148 \r
149   /**\r
150    * @return true if the current word starts a new sentence.\r
151    * @throws WordNotFoundException current word has not yet been set.\r
152    */\r
153   public boolean startsSentence() {\r
154 \r
155     if (currentWord == null) {\r
156       throw new WordNotFoundException("No Words in current String");\r
157     }\r
158 \r
159     return startsSentence;\r
160   }\r
161 \r
162   /**\r
163    * Return the text being searched. May have changed since first set\r
164    * through calls to replace.\r
165    *\r
166    * @return the text being searched.\r
167    */\r
168   public String toString() {\r
169 \r
170     return text;\r
171   }\r
172 \r
173   /**\r
174    * Adjusts the sentence iterator and the startSentence flag according to the\r
175    * currentWord.\r
176    * @param wd the wd parameter is not presently used.\r
177    */\r
178   protected void setSentenceIterator(Word wd) {\r
179     int current = sentenceIterator.current();\r
180 \r
181     if (current == currentWord.getStart())\r
182       startsSentence = true;\r
183     else {\r
184       startsSentence = false;\r
185 \r
186       if (currentWord.getEnd() > current) {\r
187         sentenceIterator.next();\r
188       }\r
189     }\r
190   }\r
191 \r
192   /**\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
199    */\r
200   //Added more intelligent character recognition (11 Feb '03)\r
201   protected boolean isWordChar(int posn) {\r
202     boolean out = false;\r
203 \r
204     char curr = text.charAt(posn);\r
205 \r
206     if ((posn == 0) || (posn == text.length() - 1)) {\r
207       return Character.isLetterOrDigit(curr);\r
208     }\r
209 \r
210     char prev = text.charAt(posn - 1);\r
211     char next = text.charAt(posn + 1);\r
212 \r
213 \r
214     switch (curr) {\r
215       case '\'':\r
216       case '@':\r
217       case '.':\r
218       case '_':\r
219         out = (Character.isLetterOrDigit(prev) && Character.isLetterOrDigit(next));\r
220         break;\r
221       default  :\r
222         out = Character.isLetterOrDigit(curr);\r
223     }\r
224 \r
225     return out;\r
226   }\r
227 \r
228   /**\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
234    */\r
235   protected boolean isWordChar(char c) {\r
236     boolean out = false;\r
237 \r
238     if (Character.isLetterOrDigit(c) || (c == '\'')) {\r
239       out = true;\r
240     }\r
241 \r
242     return out;\r
243   }\r
244 \r
245   /**\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
249    * detected.\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
255    */\r
256   protected int ignore(int index, char startIgnore) {\r
257     return ignore(index, new Character(startIgnore), null);\r
258   }\r
259 \r
260   /**\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
264    * detected.\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
273    */\r
274   protected int ignore(int index, char startIgnore, char endIgnore) {\r
275     return ignore(index, new Character(startIgnore), new Character(endIgnore));\r
276   }\r
277 \r
278   /**\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
282    * detected.\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
291    */\r
292   protected int ignore(int index, Character startIgnore, Character endIgnore) {\r
293     int newIndex = index;\r
294 \r
295     if (newIndex < text.length()) {\r
296       Character curChar = new Character(text.charAt(newIndex));\r
297 \r
298       if (curChar.equals(startIgnore)) {\r
299         newIndex++;\r
300         while (newIndex < text.length()) {\r
301           curChar = new Character(text.charAt(newIndex));\r
302           if (endIgnore != null && curChar.equals(endIgnore)){\r
303             newIndex++;\r
304             break;\r
305           } else if (endIgnore == null && !Character.isLetterOrDigit(curChar.charValue())){\r
306             break;\r
307           }\r
308           newIndex++;\r
309         }\r
310       }\r
311     }\r
312 \r
313     return newIndex;\r
314   }\r
315 \r
316   /**\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
320    * detected.\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
327    */\r
328   protected int ignore(int index, String startIgnore, String endIgnore) {\r
329 \r
330     //{{{\r
331     int newIndex = index;\r
332     int len = text.length();\r
333     int slen = startIgnore.length();\r
334     int elen = endIgnore.length();\r
335 \r
336     if (!((newIndex + slen) >= len)) {\r
337       String seg = text.substring(newIndex, newIndex + slen);\r
338 \r
339       //            System.out.println(seg + ":" + seg.length()+ ":" + startIgnore + ":" + slen);\r
340       if (seg.equals(startIgnore)) {\r
341         newIndex += slen;\r
342         cycle:          while (true) {\r
343 \r
344           if (newIndex == (text.length() - elen)) {\r
345 \r
346             break cycle;\r
347           }\r
348 \r
349           String ss = text.substring(newIndex, newIndex + elen);\r
350 \r
351           if (ss.equals(endIgnore)) {\r
352             newIndex += elen;\r
353 \r
354             break cycle;\r
355           } else {\r
356             newIndex++;\r
357           }\r
358         }\r
359       }\r
360     }\r
361 \r
362     return newIndex;\r
363   } //}}}\r
364 \r
365   /**\r
366    * Initializes the sentenseIterator\r
367    */\r
368   protected void init() {\r
369     sentenceIterator = BreakIterator.getSentenceInstance();\r
370     sentenceIterator.setText(text);\r
371   }\r
372   \r
373   /**\r
374    * Defines the starting positions for text analysis\r
375    */\r
376   private void setup() {\r
377     currentWord = new Word("", 0);\r
378     nextWord = new Word("", 0);\r
379     startsSentence = true;\r
380 \r
381     init();\r
382 \r
383     try {\r
384       next();\r
385     } catch (WordNotFoundException e) {\r
386       currentWord = null;\r
387       nextWord = null;\r
388     }\r
389   }\r
390 \r
391   \r
392 }\r