Buliding English Dictionary Using Hashmap!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Salha
    New Member
    • Feb 2007
    • 9

    #1

    Buliding English Dictionary Using Hashmap!

    Hello,
    I need your direction in my lab assignment, i dont know how how to start. I am supposed to implement a dictionary that stores English words and their multiple synonyms. Am required to use hashmap to build the dictionary and the data column in the HashMap should be a linkedlist.

    please tell me how can i implement the hashmap with a linkedlist!!!
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by Salha
    Hello,

    I need your direction in my lab assignment, i dont know how how to start. I am supposed to implement a dictionary that stores English words and their multiple synonyms. Am required to use hashmap to build the dictionary and the data column in the HashMap should be a linkedlist.



    please tell me how can i implement the hashmap with a linkedlist!!!
    Design your HashMap first. What is the key type and what is the value type?

    Comment

    • Salha
      New Member
      • Feb 2007
      • 9

      #3
      Originally posted by r035198x
      Design your HashMap first. What is the key type and what is the value type?
      Well, each word must be a key and the the data value have to be a linkedlist, how can i design the HashMap? i never used it and when i read some tutorials i didnt get the complete picture of it.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by Salha
        Well, each word must be a key and the the data value have to be a linkedlist, how can i design the HashMap? i never used it and when i read some tutorials i didnt get the complete picture of it.


        You are done!



        The key is a word (String) the value is a LinkedList of words.

        Now translate that to a Java declaration using generics (You are using 1.5 or later of course?)

        Comment

        • Salha
          New Member
          • Feb 2007
          • 9

          #5
          Hi again,

          i used:

          Code:
           HashMap dictionary = new HashMap();
          		dictionary.put("word", "Value1");
          		dictionary.put("word", "Value2");
          		System.out.println(dictionary.toString());
          Print: {word=Value2} << but i want to store more than one value, i didnt know hot to declare that the value must me a linkedlist!

          please help me :(

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by Salha
            Hi again,

            i used:

            Code:
             HashMap dictionary = new HashMap();
            		dictionary.put("word", "Value1");
            		dictionary.put("word", "Value2");
            		System.out.println(dictionary.toString());
            Print: {word=Value2} << but i want to store more than one value, i didnt know hot to declare that the value must me a linkedlist!

            please help me :(
            Code:
             
            HashMap<String, LinkedList<String>> dictionary = new HashMap<String, LinkedList<String>> ();

            Comment

            • Broke
              New Member
              • Mar 2007
              • 7

              #7
              Hello,
              I have a problem in my assignment please i need ur help the problem is that i'm trying to make a dictionary using hashmap the keys of hash map are words and the values are words are sorted in ArrayList I tried to implement the adding methed to add the words with their synonyms, but i got an error like this: can't find symbol - method add(java.lang.S tring)

              and the code that i wrote like this:
              public String AddWords(String word, String synonyms){

              String[] arr = synonyms.toStri ng().split( "\n" );
              ArrayList<Strin g> lst = lst.add(arr);>> Problem
              for(int i = 0; i< arr.length; i++){

              if(word.equals( m.containsKey(w ord))){
              return "This word is already exist";
              }
              if(synonyms.equ als(m.containsV alue(synonyms)) ){
              return "This synonym is already exist";
              }
              else{
              m.put(word, synonyms);
              }}
              return"";
              }
              plz help me as soon as possible.

              Comment

              • Ganon11
                Recognized Expert Specialist
                • Oct 2006
                • 3651

                #8
                You cannot add an array of Strings to an ArrayList containing Strings. You will have to iterate through your String array, adding each String individually.

                Comment

                • Broke
                  New Member
                  • Mar 2007
                  • 7

                  #9
                  Hello,
                  I made changes in the Add method that i mentioned before but the result that i got is inconvenient with what is reuired from me when i add a word and its synonym i get something like this:- word > null and when i add another word with its synonym the previous one is removed this is the code that i wrote:


                  private HashMap m;
                  private ArrayList<Strin g> lst;
                  public String AddWords(String word, String synonyms){
                  synonyms = synonyms.toStri ng();
                  Iterator it = lst.iterator();

                  while( it.hasNext() ) {
                  lst = (ArrayList)it.n ext();
                  if(word.equals( m.containsKey(w ord))){
                  return "This word is already exist";
                  }
                  if(synonyms.equ als(m.containsV alue(synonyms)) ){
                  return "This synonym is already exist";
                  }
                  else{


                  m.put(word, synonyms);
                  }}
                  return"";
                  }
                  I need your help as soon as possible

                  Comment

                  • r035198x
                    MVP
                    • Sep 2006
                    • 13225

                    #10
                    Originally posted by Broke
                    Hello,
                    I made changes in the Add method that i mentioned before but the result that i got is inconvenient with what is reuired from me when i add a word and its synonym i get something like this:- word > null and when i add another word with its synonym the previous one is removed this is the code that i wrote:


                    private HashMap m;
                    private ArrayList<Strin g> lst;
                    public String AddWords(String word, String synonyms){
                    synonyms = synonyms.toStri ng();
                    Iterator it = lst.iterator();

                    while( it.hasNext() ) {
                    lst = (ArrayList)it.n ext();
                    if(word.equals( m.containsKey(w ord))){
                    return "This word is already exist";
                    }
                    if(synonyms.equ als(m.containsV alue(synonyms)) ){
                    return "This synonym is already exist";
                    }
                    else{


                    m.put(word, synonyms);
                    }}
                    return"";
                    }
                    I need your help as soon as possible
                    That logic is incorrect.

                    If you have declared your dictionary as

                    Code:
                     
                    HashMap<String, ArrayList> dictionary = new  HashMap<String, ArrayList>();
                    (which you should have), then your add word should look like this

                    Code:
                     
                    public void addWord(String word, ArrayList synonyms) {
                    	   if(dictionary.containsKey(word)) {
                    			  ArrayList list = dictionary.get(word);
                    			  //if synonyms contains a word that is not in list,
                    			  //then add that word to list and reset it
                    			   //otherwise flag that the entries are already there
                    	   } 
                    	   else {
                    			   //add the new word
                    		}
                    }

                    Comment

                    • Broke
                      New Member
                      • Mar 2007
                      • 7

                      #11
                      Hello again,
                      I tried to implement what u advised me to do, but i got an error which says inconvertible type: and this is the code that i wrote:

                      public String Addwords(String word, ArrayList syn){
                      ArrayList list = new ArrayList();
                      //instead of dictionary might be dictionary
                      for(int i = 0; i<dictionary.si ze(); i++){
                      if(dictionary.c ontainsKey(word )){
                      list =(ArrayList)wor d.toString();>> the problem
                      }
                      else if(!dictionary. containsKey(wor d)){
                      dictionary.put( word, list);
                      }
                      else if(dictionary.c ontainsValue(sy n)){
                      dictionary.get( syn);
                      syn.toString();
                      }
                      else if(!dictionary. containsValue(s yn)){
                      dictionary.put( word, list);
                      }
                      }
                      return"";
                      }

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #12
                        Originally posted by Broke
                        Hello again,
                        I tried to implement what u advised me to do, but i got an error which says inconvertible type: and this is the code that i wrote:

                        public String Addwords(String word, ArrayList syn){
                        ArrayList list = new ArrayList();
                        //instead of dictionary might be dictionary
                        for(int i = 0; i<dictionary.si ze(); i++){
                        if(dictionary.c ontainsKey(word )){
                        list =(ArrayList)wor d.toString();>> the problem
                        }
                        else if(!dictionary. containsKey(wor d)){
                        dictionary.put( word, list);
                        }
                        else if(dictionary.c ontainsValue(sy n)){
                        dictionary.get( syn);
                        syn.toString();
                        }
                        else if(!dictionary. containsValue(s yn)){
                        dictionary.put( word, list);
                        }
                        }
                        return"";
                        }
                        Which line is giving the error?

                        Comment

                        • Broke
                          New Member
                          • Mar 2007
                          • 7

                          #13
                          list =(ArrayList)wor d.toString();>> the problem

                          Comment

                          • r035198x
                            MVP
                            • Sep 2006
                            • 13225

                            #14
                            Originally posted by Broke
                            list =(ArrayList)wor d.toString();>> the problem
                            That wont work. The idea was to test to see if the new list has any new words from the ones that were already stored for the given word.
                            What are you trying to achieve with that statement?

                            Comment

                            • Broke
                              New Member
                              • Mar 2007
                              • 7

                              #15
                              Can anyone help me i wanna compare between two ArrayLists if there is a factor element between them the third list would display the elements of both ArrayLists, without duplication. The code i wrote doesn't have syntax error but i think it has logical error and this is my code:-

                              public ArrayList checkSym(ArrayL ist art1, ArrayList art2) {

                              Iterator it1 = art1.iterator() ;
                              Iterator it2 = art2.iterator() ;

                              String s = "";
                              while (it1.hasNext()) {
                              s = (String)it1.nex t();
                              while (it2.hasNext()) {
                              if (s.equals((Stri ng)it2.next())) {
                              art1.remove(s);
                              }
                              }

                              }
                              art2.addAll(art 1);
                              return art2;
                              }

                              Comment

                              Working...