giovedì 24 giugno 2010

How to suggest alternate spellings/2 [ENG]

We learned how to estimate the edit distance of two strings. How do we use this information?

I thought of giving each word in my dictionary a score over the user's input and showing the entries in the list sorted by this score.

Since the "edit distance" is a small number when the words are similar it would be an ascending order (the first entries are closer to user input).

First I defined a Word class:

    public static class Word 
    {
        @Override
        public String toString()
        {
            return "word='" + word + "'; score=" + score;
        }

        Word(String word)
        {
            this.word = word;
        }
        
        private String word;
        private int score;
        
        public void setScore(int score)
        {
            this.score = score;
        }

        public int getScore()
        {
            return score;
        }
        
        public String getWord()
        {
            return word;
        }
    }

To sort this stuff we are going to need a comparator:

    public class WordComparator implements Comparator<Word> 
    {
        public int compare(Word word1, Word word2) 
        {
            return word1.getScore()-word2.getScore();
        }
    }

We are almost there...

Nessun commento: