OSDN Git Service

NeverNote 0.88.
[neighbornote/NeighborNote.git] / src / com / swabunga / spell / engine / Word.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.engine;\r
21 \r
22 import java.util.Comparator;\r
23 \r
24 /**\r
25  * The Word object holds information for one suggested spelling.\r
26  * It contains both the suggested word string and the distance cost, which represents how different the suggested\r
27  * word is from the misspelling.\r
28  *  <p>This class is now immutable.\r
29  *  </p>\r
30  */\r
31 @SuppressWarnings("unchecked")\r
32 public class Word implements Comparator {\r
33   private String word;\r
34   private final int score;\r
35 \r
36   /**\r
37    * Constructs a new Word.\r
38    * @param word The text of a word.\r
39    * @param score The word's distance cost\r
40    */\r
41   public Word(String word, int score) {\r
42     this.word = word;\r
43     this.score = score;\r
44   }\r
45 \r
46   /**\r
47    * Constructs a new Word.\r
48    */\r
49   public Word() {\r
50     this.word = "";\r
51     this.score = 0;\r
52   }\r
53 \r
54   /**\r
55    * Compares two words, mostly for the purpose of sorting words.\r
56    * @param o1 the first word\r
57    * @param o2 the second word\r
58    * @return -1 if the first word is more similar to the misspelled word\r
59    * <br>1 if the second word is more similar to the misspelled word\r
60    * <br>0 if both words are equally similar\r
61    *\r
62    */\r
63   public int compare(Object o1, Object o2) {\r
64     if (((Word) o1).getCost() < ((Word) o2).getCost()) return -1;\r
65     if (((Word) o1).getCost() == ((Word) o2).getCost()) return 0;\r
66     return 1;\r
67   }\r
68 \r
69   /**\r
70    * Indicates if this word is equal to another one.\r
71    * @param o The other word to compare\r
72    * @return The indication of equality\r
73    */\r
74   @Override\r
75 public boolean equals(Object o) {\r
76     if (o instanceof Word)  // added by bd\r
77       return(((Word)o).getWord().equals(getWord()));\r
78     return false;\r
79   }\r
80   \r
81   /**\r
82    * gets suggested spelling\r
83    * @return the actual text of the suggest spelling\r
84    */\r
85   public String getWord() {\r
86     return word;\r
87   }\r
88 \r
89   /**\r
90    * sets suggested spelling\r
91    * @param word The text to set for suggestd spelling\r
92    */\r
93   public void setWord(String word) {\r
94     this.word = word;\r
95   }\r
96 \r
97   /**\r
98    * A cost measures how close a match this word was to the original word\r
99    * @return 0 if an exact match. Higher numbers are worse matches.\r
100    * @see EditDistance\r
101    */\r
102   public int getCost() {\r
103     return score;\r
104   }\r
105 \r
106   /**\r
107    * returns the suggested spelling\r
108    * @return The word's text \r
109    */\r
110   @Override\r
111 public String toString() {\r
112     return word;\r
113   }\r
114 }\r
115 \r