package com.sipstacks.redneck;

import com.sipstacks.redneck.GetJson;
import java.util.ArrayList;
import java.util.List;
import org.json.simple.*;
import com.sipstacks.redneck.Markov;


public class Redneck 
{
	public static List<String> getStrings() {
		List<String> results = new ArrayList<String>();
		Object resobj = GetJson.get("http://api.urbandictionary.com/v0/random");
		if (! (resobj instanceof JSONObject)) {
			System.err.println("Unexpected response: " + resobj.toString());
			return results;
		}
		JSONObject obj = (JSONObject)resobj;
		JSONArray list = (JSONArray)obj.get("list");
		for(Object item : list) {
			JSONObject defintion = (JSONObject)item;
			Object result = defintion.get("example");
			if(result != null) {
				String sResult = result.toString().replaceAll("\"([^\"]*)\"", "$1");
				sResult = sResult.replaceAll("\r\n", "\n");
				sResult = sResult.replaceAll("\t", " ");
//				sResult = sResult.replaceAll("[a-zA-Z0-9]\n", ".\n");
//				sResult = sResult.replaceAll("[a-zA-Z0-9]$", ".\n");
				sResult = sResult.replaceAll("\\[([^\\]]*)\\]", "$1");
				sResult = sResult.replaceAll("(^|\n)([ ]*)([a-z0-9]+\\.)([^\n]*)", "$4\n");
				sResult = sResult.replaceAll("(^|\n)([ ]*)([a-zA-Z0-9]+\\))([^\n]*)", "$4\n");
				sResult = sResult.replaceAll("(^|\n)([ ]*)([a-zA-Z0-9_# -]+\\:)([^\n]*)", "$4\n");


				// nuke fake bold
				sResult = sResult.replaceAll("\\*([^\\*]*)\\*", "$1");
				// nuke () comments just makes the chain confused
				sResult = sResult.replaceAll("\\(([^\\)]*)\\)", "");


				//System.err.println(sResult);
				//System.err.println("***End of Result****");

				results.add(sResult);
			}
		}
		return results;
	}



	public static void main(String args[]) {
		while(true) {
			for (int i = 0; i < 30; i++) {
				List<String> results = getStrings();
				for (String res : results) {
					if(res != null) {
						Markov.addWords(res);
					}
				}
				try{Thread.sleep(300);}catch(Exception e) {}
			}
			for (int i = 0; i < 1; i++) {
				String result = Markov.generateSentence();
				System.err.println(result);
			}
		}
	}
}



@@ -773,39 +773,100 @@
 ng()
-.replaceAll(%22%5C%22(%5B%5E%5C%22%5D*)%5C%22
+;%09%09%09%09%09%09sResult = sResult.replaceAll(%22%5Cr%5Cn%22, %22%5Cn%22);%0A%09%09%09%09sResult = sResult.replaceAll(%22%5Ct
 %22, %22
-$1
+ 
 %22);%0A
@@ -895,34 +895,34 @@
 replaceAll(%22
-%5Cr%5Cn
+%5B%E2%80%9D%E2%80%9C%5D
 %22, %22%5C
-n
+%22
 %22);%0A%09%09%09%09sRes
@@ -948,26 +948,38 @@
 ceAll(%22%5C
-t
+%22(%5B%5E%5C%22%5D*)%5C%22
 %22, %22
- 
+$1
 %22);%0A
+%0A
 //%09%09%09%09sR

clean up mac style quotes