Results 1 to 6 of 6
  1. #1
    Dana Zra is offline Member
    Join Date
    Oct 2008
    Posts
    4
    Rep Power
    0

    Default [SOLVED] HashMap & Spell checking

    Hi, I'm fairly new to Java, and especially to HashMaps. Basically what I need to do is make a spell checker that reads a dictionary file (txt) and maps it in a hashmap, so that every key is a word and every value is a non-null object (so that every "word" with value Null is certain to be incorrect or not part of the dictionary):

    Key1: a Value1: 1
    key2: Aa Value2: 2
    key23: Atomic bomb Value23: 23

    I've made the HashMap, but now I need to find every word that has an edit distance of 2 (or less) compared to a word I need to check spelling of. How would I do this? I know how to check whether the max edit distance is 2, but how do I find the words in the dictionary, without having to compare every single word?

  2. #2
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,873
    Rep Power
    25

    Default

    Can you explain your problem a bit more?
    What are the rules for setting the value for words from the dictionary file?
    I need to find every word that has an edit distance of 2
    You don't define what "edit distance" is.
    Are those words in the HashMap?
    so that every "word" with value Null
    Is that another way of saying that the "word" is not in the HashMap?
    how do I find the words in the dictionary
    Given a word, the get() method will tell you if the word is in the HashMap.

  3. #3
    Dana Zra is offline Member
    Join Date
    Oct 2008
    Posts
    4
    Rep Power
    0

    Default

    Is that another way of saying that the "word" is not in the HashMap?
    Yes it is. what I need is to make a dictionary with a HashMap, so that I can check if a word is present in that HashMap. There aren't any specifications as to the rules of value, I can use any kind of values I want. i just need there to be values that arent Null.

    You don't define what "edit distance" is.
    Are those words in the HashMap?
    What I'm refering to here is the Levenshtein distance or edit distance between two words, a way of comparing the character difference between them. The Spell Checker has to check if a word exists in the dictionary hashmap. ths I have done with the containskey() method. What I need is for the spell checker to find every word that has an edit distance of max 2, meaning every word that has 2 or less characters different, and put them in an array. For example, if I have the word "helpp":

    "helpp"
    found in Dictionary: "help"
    "helps"
    "hell"

    it should then put help, helps and hell, and whichever other words it finds, in an array.

    My question is, how would I get it to do this?

  4. #4
    Dana Zra is offline Member
    Join Date
    Oct 2008
    Posts
    4
    Rep Power
    0

    Default

    By the way, here is the code I was given for finding the max edit distance between two words:


    public boolean maxEditDistance ( String s1 , String s2 , int m ) {
    if ( s1.length ( ) == 0 )
    return ( s2.length ( ) <= m) ;
    if ( s2.length ( ) == 0 )
    return ( s1.length ( ) <= m) ;
    if ( Math.abs ( s1.length ( ) − s2.length ( ) ) > m )
    return false ;
    if ( s1.charAt ( 0 ) == s2.charAt ( 0 ) )
    return maxEditDistance ( s1.substring( 1 ) , s2.substring( 1 ) , m ) ;
    else {
    boolean b1 = maxEditDistance ( s1 , s2.substring ( 1 ) , m−1 ) ;
    boolean b2 = maxEditDistance ( s1.substring( 1 ) , s2 , m−1 ) ;
    return ( b1 | | b2 ) ;
    }
    }

  5. #5
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,873
    Rep Power
    25

    Default

    check if a word is present in that HashMap
    Have you read the API doc for HashMap?
    What does get(word) return if the word is not a key in HashMap?
    Or if you allow null values, then use containsKey(word);

    every word that has 2 or less characters different
    Do xxlp and help qualify?
    Or xelx and help
    Or hexx and help
    etc

    Is the order important or just the total number of different characters?
    ehpl and help have the same characters

    Don't see how a HashMap can help, but this isn't a trival problem and will take some thought.

    A quick thought is to use the hashCode() method to map words with the same letters to the same hash, without reguard to order.
    Last edited by Norm; 10-19-2008 at 09:59 PM.

  6. #6
    Dana Zra is offline Member
    Join Date
    Oct 2008
    Posts
    4
    Rep Power
    0

    Default

    Have you read the API doc for HashMap?
    What does get(word) return if the word is not a key in HashMap?
    Or if you allow null values, then use containsKey(word);
    sorry from my part. I read the assignment rules wrong, and I am allowed to use Null values. from my understanding the containsKey(String word) checks if the key is present in the hashmap. this is what I do.
    The get(int index) returns the actual key, at the index position. so get(2442) might get me the string "Hello".

    Anyway, a friend of mine solved the problem for me. thanks anyway.

Similar Threads

  1. Spell check in Textarea.
    By surya_dks in forum Advanced Java
    Replies: 2
    Last Post: 08-22-2008, 01:33 AM
  2. Checking ResultSet (second approach)
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 02-09-2008, 09:39 PM
  3. Spell check feature
    By ravian in forum Advanced Java
    Replies: 2
    Last Post: 12-27-2007, 10:28 AM
  4. spell check
    By kazitula in forum Java Applets
    Replies: 2
    Last Post: 12-20-2007, 12:37 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •