tags:

views:

54

answers:

0

hello, i tried to implement shift reduce parser my code is below

    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);
            }
        }
String txt=" the dog jumps";
String t1=null;
StringTokenizer st3 = new StringTokenizer(txt);
Stack <String> sta=new Stack<String>();
while (st3.hasMoreTokens()) {

      String tk = st3.nextToken().trim();

        for (String key1 : hashmap.keySet())

        {
            //System.out.println(hashmap.get("));
          if(hashmap.get(key1).equals(tk))
           {
              t1=tk;
               tk=key1;

               System.out.println(t1+"-->"+tk);

           }

        }
}
    }
}

how to compare the values inside the map or how can i get separated values form an multiple valued map