/** * 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 misspelled 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 static void main(String[] args) { List<Word> freq = histogram("a brown cat eats a white cat."); for (Word word : freq) { System.out.printf("%4d %s\n", word.getFrequency(), word.getWord()); } } }
public Word put(Word s) { for (Word w : al) { if (w.s.compareTo(s.getWord()) == 0) { w.setfrequency(); // found it, now stop searching return w; } } // didn't find it; insert al.add(s); return s; }
private Vector getWordsFromCode(String word, Hashtable codes) { Configuration config = Configuration.getConfiguration(); Vector result = new Vector(); int[][] matrix = new int[0][0]; final int configDistance = config.getInteger(Configuration.SPELL_THRESHOLD); for (Enumeration i = codes.keys(); i.hasMoreElements();) { String code = (String) i.nextElement(); List simwordlist = getWords(code); for (Iterator iter = simwordlist.iterator(); iter.hasNext();) { String similar = (String) iter.next(); int distance = EditDistance.getDistance(word, similar, matrix); if (distance < configDistance) { Word w = new Word(similar, distance); result.addElement(w); } } } return result; }
public Word put(Word s) { if (al.contains(s)) { for (Word w : al) { if (w.s.compareTo(s.getWord()) == 0) { s.setfrequency(); }// else { //Remove this else, as you add element outside // al.add(s); //} } } else al.add(s); return s; }
public int compareTo(Word other) { return word.compareTo(other.getWord()); }
public class WordContainer { public static ArrayList<Word> al = new ArrayList<Word>(); public Word put(Word s) { for (Word w : al) { if (w.s.compareTo(s.getWord()) == 0) { //s.setfrequency(); w.setfrequency(); // <-- set frequency of existing word in al return w; // <-- return word object that is in al } // no need to add new word to list after every failed comparison // unless that's what you want to do. } al.add(s); return s; }
List<Word> ordered = new ArrayList<Word>(histo.values()); Collections.sort(ordered, new Comparator<Word>() { public int compare(Word a, Word b) { int fa = a.getFrequency(); int fb = b.getFrequency(); if (fa < fb) return 1; if (fa > fb) return -1; return a.getWord().compareTo(b.getWord()); } }); return ordered; }
buf.setCharAt(0, Character.toUpperCase(word.charAt(0))); Vector suggestion = new Vector(); suggestion.addElement(new Word(buf.toString(), 0)); SpellCheckEvent event = new BasicSpellCheckEvent(word, suggestion, tokenizer); terminated = fireAndHandleEvent(tokenizer, event);
public void removeWord(String inputWord) { for (Word z : wordlist) { if (z.getWord().equals(inputWord)) { wordlist.remove(z); System.out.println("the word" + inputWord + "is removed"); break; } } System.out.println(wordlist.size()); }
public class Word implements Comparable<Word> { private String word; private TreeSet<Word> synonyms; //getter and setter public void addSynonym(final Word word) { synonyms.add(word); } @Override public int compareTo(final Word other) { if (this.word == null) { return -1; if (other == null || other.getWord() == null) { return 1; } return this.word.compareTo(other.getWord()); } }
public void spellingError(SpellCheckEvent event) { @SuppressWarnings("unchecked") List<Word> suggestions = (List<Word>) event.getSuggestions(); if (suggestions.size() > 0) { Iterator<Word> suggestedWord = suggestions.iterator(); if (suggestedWord.hasNext()) { String orig = event.getInvalidWord(); String sugg = suggestedWord.next().getWord(); if (_correctionType == SpellCorrectionType.Add) event.replaceWord(sugg + " ( " + orig + " ) ", false); else if (_correctionType == SpellCorrectionType.Replace) event.replaceWord(sugg, false); } } }
JPopupMenu jpm = new JPopupMenu(); for (final Word s : l ) { JMenuItem jmi=new JMenuItem(s.getWord()); jpm.add(jmi); final int f_pos=pos;