alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Jazzy example source code file (Word.java)

This example Jazzy source code file (Word.java) is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Java - Jazzy tags/keywords

comparator, comparator, object, object, string, string, util, word, word

The Jazzy Word.java source code

/*
Jazzy - a Java library for Spell Checking
Copyright (C) 2001 Mindaugas Idzelis
Full text of license can be found in LICENSE.txt

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/
package com.swabunga.spell.engine;

import java.util.Comparator;

/**
 * The Word object holds information for one suggested spelling.
 * It contains both the suggested word string and the distance cost, which represents how different the suggested
 * word is from the misspelling.
 *  <p>This class is now immutable.
 *  </p>
 */
public class Word implements Comparator {
  private String word;
  private int score;

  public Word(String word, int score) {
    this.word = word;
    this.score = score;
  }

  public Word() {
    this.word = "";
    this.score = 0;
  }

  /**
   * Compares two words, mostly for the purpose of sorting words.
   * @param o1 the first word
   * @param o2 the second word
   * @return -1 if the first word is more similar to the mispelled word
   * <br>1 if the second word is more similar to the misspelled word
   * <br>0 if both words are equally similar
   *
   */
  public int compare(Object o1, Object o2) {
    if (((Word) o1).getCost() < ((Word) o2).getCost()) return -1;
    if (((Word) o1).getCost() == ((Word) o2).getCost()) return 0;
    return 1;
  }

  public boolean equals(Object o) {
    if (o instanceof Word)  // added by bd
      return(((Word)o).getWord().equals(getWord()));
    return false;
  }
  
  /**
   * gets suggested spelling
   * @return the actual text of the suggest spelling
   */
  public String getWord() {
    return word;
  }

  /**
   * sets suggested spelling
   */
  public void setWord(String word) {
    this.word = word;
  }

  /**
   * A cost measures how close a match this word was to the original word
   * @return 0 if an exact match. Higher numbers are worse matches.
   * @see EditDistance
   */
  public int getCost() {
    return score;
  }

  /**
   * returns the suggested spelling
   */
  public String toString() {
    return word;
  }
}

Other Jazzy examples (source code examples)

Here is a short list of links related to this Jazzy Word.java source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.