/*
Usage:
to compile:
	javac HaikuChecker.java
to run:
	java HaikuChecker this is not a haiku
	or
	java HaikuChecker this should be a haiku if i counted correctly you should try it



*/


import java.net.*;
import java.io.*;
import java.util.*;


public class HaikuChecker{
	public static void main(String args[]){
		int sum = 0;
		for(int i=0; i<args.length; i++){	
			sum += numSyllables(args[i]);
		
		}
		System.out.println("Number of Syllables: " + sum);
		if(sum == 17) System.out.println("--- It's a Haiku ----");
	}
	
	public static int numSyllables(String theWord){
		boolean debug = true;
		String s = "";
		String s2 = "";
		theWord = (new String(theWord)).toLowerCase();
		String foundWord = "";
		String foundWordStripped = "";
		int count = 0;
		boolean found = false;
			
		//if(debug) System.out.println("looking up: " + theWord);
			
		try{
			URL wordLink = new URL("http://dictionary.reference.com/search?q=" + theWord);
			URLConnection connection = wordLink.openConnection();
			BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));

			while( (s = in.readLine()) != null && !found){
				if(s.startsWith("<TABLE><TR><TD><b>")){
					found = true;
					foundWord = s.substring(s.indexOf("<b>") + 3, s.indexOf("</b>"));
				}
			}	
		} catch (Exception e){
			System.out.println(e);
		}
		
		if(!found) System.out.println("Error, parse token not found");

			int i = 0;
			int j = 0;
			while(i<foundWord.length()){
				s = foundWord.substring(i, foundWord.length());
				if(s.startsWith("&#183;")){
					//System.out.println("i=" + i + ", j=" + j);
					foundWordStripped += foundWord.substring(j, i);
					i+=6;
					j = i;
					count++;
				} else {
					i++;
				}
			}
			foundWordStripped += foundWord.substring(j, i);
			
			/*
			for(int i=0; i<foundWord.length()-4; i++){
				s = foundWord.substring(i, foundWord.length());
				if(s.startsWith("&#183;")){
					count++;
				}
			}
			*/
			count++;
			if(!theWord.equals(foundWordStripped)){
				if(
					(theWord.equals(foundWordStripped + "ing")) ||
					(theWord.equals(foundWordStripped + "ly")) ||
					(theWord.equals(foundWordStripped + "est")) ||
					(theWord.equals(foundWordStripped + "ed")) ||
					(theWord.equals(foundWordStripped + "er")) ||
					(theWord.equals(foundWordStripped + foundWordStripped.substring(foundWordStripped.length() - 1, foundWordStripped.length())  + "ing")) ||
					(theWord.equals(foundWordStripped + foundWordStripped.substring(foundWordStripped.length() - 1, foundWordStripped.length())  + "er"))
				)
				{
				count++;
				}	

			}
			
			if(count <= 0){
				System.out.println("Error finding: " + theWord);
			}
			
			//System.out.println("----");
			System.out.println(theWord + ": " + count);
			
			if(debug){
			//System.out.println("-Debug-");
			//System.out.println("Parse Word: " + foundWord);
			//System.out.println("Parse Word Stripped: " + foundWordStripped);
			}
			//System.out.println("----");
			return count;
	}
	
}