tags:

views:

102

answers:

2
import java.util.Arrays;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Stack;
import java.util.StringTokenizer;


public class shift {

    @SuppressWarnings("unchecked")
    public static void main(String args[])
    {


        String speech = "Sentence:NounPhrase VerbPhrase:NounPhrase :Art Noun:VerbPhrase : Verb | Adverb Verb: Art : the | a : Verb :jumps | sings |: Noun:dog | cat | ";

        HashMap<String, String> hashmap = new HashMap<String, String>();
        String a;
        StringTokenizer st = new StringTokenizer(speech,":");

        while (st.hasMoreTokens()) {
          String key=st.nextToken().trim();
          String value=st.nextToken().trim();



          StringTokenizer st1 = new StringTokenizer(value,"|");

          while (st1.hasMoreTokens()) {

              a=st1.nextToken().trim();

              hashmap.put(key, a);


          }


        }


        Set set = hashmap.entrySet(); 
        Iterator ia = set.iterator();





    while(ia.hasNext()) {
    Map.Entry me = (Map.Entry)ia.next(); 

            System.out.println(me.getKey()+"->"+me.getValue());


     }
}

}

the output is
Noun->cat
NounPhrase->Art Noun
Art->a
Sentence->NounPhrase VerbPhrase
Verb->sings
VerbPhrase->Adverb Verb

this code is missing some values to return such as the the jumps etc are not show

+6  A: 

Not sure I get your question fully, but keep in mind that a HashMap can only store one value per key.

If you want to store multiple verbs for the key "Verb", then you would have to declare the map using something like:

HashMap<String, Set<String>> hashmap = new HashMap<String, Set<String>>();

and store the words mapped to by "Verb" in a set.

Here is a brushed up (working) version of the code:

import java.util.*;
public class Shift {

    public static void main(String args[]) {

        String speech = "Sentence:NounPhrase VerbPhrase:NounPhrase :Art " +
                        "Noun:VerbPhrase : Verb | Adverb Verb: Art : the | " +
                        "a : Verb :jumps | sings |: Noun:dog | cat | ";

        Map<String, Set<String>> hashmap = new HashMap<String, Set<String>>();

        StringTokenizer st = new StringTokenizer(speech, ":");

        while (st.hasMoreTokens()) {
            String key = st.nextToken().trim();
            String value = st.nextToken().trim();

            StringTokenizer st1 = new StringTokenizer(value, "|");

            while (st1.hasMoreTokens()) {
                String a = st1.nextToken().trim();

                if (!hashmap.containsKey(key))
                    hashmap.put(key, new HashSet<String>());

                hashmap.get(key).add(a);
            }
        }

        for (String key : hashmap.keySet())
            System.out.printf("%s -> %s%n", key, hashmap.get(key));
    }
}
aioobe
ohh thats the problem i have one key and many values
raju
is there is any way to get the values as one by one like [NounPhrase ,VerbPhrase] as NounPhrase and then VerbPhrase
raju
There are implementation of multi maps already existing, e.g. in Apache Commons Collections, or Guave. If you use such you can leave the algorithm as it is.
Wolfgang
+2  A: 

You're overwriting the existing value when you call hashmap.put(key, a), since you're assigning a value to a key that already has a value.

Sam T.