OSDN Git Service

Remove unused variable.
[neighbornote/NeighborNote.git] / src / com / swabunga / spell / event / DefaultWordFinder.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 /**\r
23  * A basic word finder, which searches text for sequences of letters.\r
24  * \r
25  * @author Anthony Roy  (ajr@antroy.co.uk)\r
26  */\r
27 public class DefaultWordFinder extends AbstractWordFinder {\r
28 \r
29   //~ Instance/static variables ...............................................\r
30 \r
31   //~ Constructors ............................................................\r
32 \r
33   /**\r
34    * Creates a new DefaultWordFinder object.\r
35    * \r
36    * @param inText the String to search\r
37    */\r
38   public DefaultWordFinder(String inText) {\r
39     super(inText);\r
40   }\r
41 \r
42   /**\r
43    * Creates a new DefaultWordFinder object.\r
44    */\r
45   public DefaultWordFinder() {\r
46     super();\r
47   }\r
48 \r
49   //~ Methods .................................................................\r
50 \r
51   /**\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
54    * \r
55    * @return the next word.\r
56    * @throws WordNotFoundException search string contains no more words.\r
57    */\r
58   public Word next() {\r
59     if (nextWord == null) {\r
60       throw new WordNotFoundException("No more words found.");\r
61     }\r
62     currentWord.copy(nextWord);\r
63     setSentenceIterator(currentWord);\r
64 \r
65     int i = currentWord.getEnd();\r
66     boolean finished = false;\r
67 \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
73         finished = true;\r
74       }\r
75       i++;\r
76     }\r
77     if (!finished)\r
78       nextWord = null;\r
79 \r
80     return currentWord;\r
81   }\r
82 \r
83   /**\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
88    */\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
96           continue;\r
97 \r
98         if (ch == '\r' || ch == '\n')\r
99           return i;\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
103             return i;\r
104           else\r
105             return i - 1;\r
106       }\r
107       return text.length();\r
108     } else {\r
109       for (int i = startPos; i < text.length(); i++) {\r
110         if (!isWordChar(i))\r
111           return i;\r
112       }\r
113       return text.length();\r
114     }\r
115   }\r
116 }\r