[1014] | 1 | package com.swabunga.spell.engine; |
---|
| 2 | |
---|
| 3 | import java.util.Comparator; |
---|
| 4 | |
---|
| 5 | /** |
---|
| 6 | * The Word object holds information for one suggested spelling. |
---|
| 7 | * It contains both the suggested word string and the distance cost, which represents how different the suggested |
---|
| 8 | * word is from the misspelling. |
---|
| 9 | * <p>This class is now immutable. |
---|
| 10 | * </p> |
---|
| 11 | */ |
---|
| 12 | public class Word implements Comparator { |
---|
| 13 | private String word; |
---|
| 14 | private int score; |
---|
| 15 | |
---|
| 16 | public Word(String word, int score) { |
---|
| 17 | this.word = word; |
---|
| 18 | this.score = score; |
---|
| 19 | } |
---|
| 20 | |
---|
| 21 | public Word() { |
---|
| 22 | this.word = ""; |
---|
| 23 | this.score = 0; |
---|
| 24 | } |
---|
| 25 | |
---|
| 26 | /** |
---|
| 27 | * Compares two words, mostly for the purpose of sorting words. |
---|
| 28 | * @param o1 the first word |
---|
| 29 | * @param o2 the second word |
---|
| 30 | * @return -1 if the first word is more similar to the mispelled word |
---|
| 31 | * <br>1 if the second word is more similar to the misspelled word |
---|
| 32 | * <br>0 if both words are equally similar |
---|
| 33 | * |
---|
| 34 | */ |
---|
| 35 | public int compare(Object o1, Object o2) { |
---|
| 36 | if (((Word) o1).getCost() < ((Word) o2).getCost()) return -1; |
---|
| 37 | if (((Word) o1).getCost() == ((Word) o2).getCost()) return 0; |
---|
| 38 | return 1; |
---|
| 39 | } |
---|
| 40 | |
---|
| 41 | public boolean equals(Object o) { |
---|
| 42 | return(((Word)o).getWord().equals(getWord())); |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | /** |
---|
| 46 | * gets suggested spelling |
---|
| 47 | * @return the actual text of the suggest spelling |
---|
| 48 | */ |
---|
| 49 | public String getWord() { |
---|
| 50 | return word; |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | /** |
---|
| 54 | * A cost measures how close a match this word was to the original word |
---|
| 55 | * @return 0 if an exact match. Higher numbers are worse matches. |
---|
| 56 | * @see EditDistance |
---|
| 57 | */ |
---|
| 58 | public int getCost() { |
---|
| 59 | return score; |
---|
| 60 | } |
---|
| 61 | |
---|
| 62 | /** |
---|
| 63 | * returns the suggested spelling |
---|
| 64 | */ |
---|
| 65 | public String toString() { |
---|
| 66 | return word; |
---|
| 67 | } |
---|
| 68 | } |
---|
| 69 | |
---|