Mega Code Archive

 
Categories / Java / Data Type
 

Look for particular sequences in sentences

// : c12:AnalyzeSentence.java // Look for particular sequences in sentences. // From 'Thinking in Java, 3rd ed.' (c) Bruce Eckel 2002 // www.BruceEckel.com. See copyright notice in CopyRight.txt. import java.util.StringTokenizer; public class AnalyzeSentence {   public static void main(String[] args) {     analyze("I am happy about this");     analyze("I am not happy about this");     analyze("I am not! I am happy");     analyze("I am sad about this");     analyze("I am not sad about this");     analyze("I am not! I am sad");     analyze("Are you happy about this?");     analyze("Are you sad about this?");     analyze("It's you! I am happy");     analyze("It's you! I am sad");   }   private static StringTokenizer st;   private static void analyze(String s) {     System.out.println("\nnew sentence >> " + s);     boolean sad = false;     st = new StringTokenizer(s);     while (st.hasMoreTokens()) {       String token = next();       // Look until you find one of the       // two starting tokens:       if (!token.equals("I") && !token.equals("Are"))         continue; // Top of while loop       if (token.equals("I")) {         String tk2 = next();         if (!tk2.equals("am")) // Must be after I           break; // Out of while loop         else {           String tk3 = next();           if (tk3.equals("sad")) {             sad = true;             break; // Out of while loop           }           if (tk3.equals("not")) {             String tk4 = next();             if (tk4.equals("sad"))               break; // Leave sad false             if (tk4.equals("happy")) {               sad = true;               break;             }           }         }       }       if (token.equals("Are")) {         String tk2 = next();         if (!tk2.equals("you"))           break; // Must be after Are         String tk3 = next();         if (tk3.equals("sad"))           sad = true;         break; // Out of while loop       }     }     if (sad)       System.out.println("Sad detected");   }   static String next() {     if (st.hasMoreTokens()) {       String s = st.nextToken();       System.out.println(s);       return s;     } else       return "";   } } ///:~