import java.math.*;
import javax.imageio.*;
import java.awt.image.*;
import java.awt.event.*;
import java.awt.*;
import java.security.spec.*;
import java.security.*;
import java.lang.management.*;
import java.lang.ref.*;
import java.lang.reflect.*;
import java.net.*;
import java.io.*;
import javax.swing.table.*;
import javax.swing.text.*;
import javax.swing.event.*;
import javax.swing.*;
import java.util.concurrent.atomic.*;
import java.util.concurrent.*;
import java.util.regex.*;
import java.util.List;
import java.util.zip.*;
import java.util.*;
public class main {
// a persistent map using a clever combination of persisting and logging
// (well, only logging as of now)
// Note: don't put in static initializer (programID not set yet)
static class PersistentMap extends AbstractMap {
Map m = new TreeMap();
File file;
PersistentMap(String fileName) {
this(getProgramFile(fileName));
}
PersistentMap(String progID, String fileName) {
this(getProgramFile(progID, fileName));
}
PersistentMap(File file) {
this.file = file;
for (String s : scanLog(file)) try {
List l = (List) ( unstructure(s));
if (eq(l.get(0), "add"))
m.put((A) l.get(1), (B) l.get(2));
else if (eq(l.get(0), "remove"))
m.remove((A) l.get(1));
else
print("Unknown command in log: " + l.get(0));
} catch (Throwable __e) { printStackTrace(__e); }
}
// just delegates
public int size() {
return m.size();
}
public B get(Object a) {
return m.get(a);
}
// TODO: calling remove() in the iterator will have unpersisted
// effects.
public Set> entrySet() {
return m.entrySet();
}
// delegates with logging
public B put(A a, B b) {
B result = m.put(a, b);
logQuoted(file, structure(litlist("add", a, b)));
return result;
}
public B remove(Object a) {
B result = m.remove(a);
logQuoted(file, structure(litlist("remove", a)));
return result;
}
}
static class Matches {
String[] m;
String get(int i) { return m[i]; }
String unq(int i) { return unquote(m[i]); }
String fsi(int i) { return formatSnippetID(unq(i)); }
boolean bool(int i) { return "true".equals(unq(i)); }
String rest() { return m[m.length-1]; } // for matchStart
}
static String url = "http://bots.tinybrain.de/1002076/raw";
static String param = "q";
static List> qlist = new ArrayList>(); // questions and answers
static List protocol = new ArrayList();
static class TuringScore {
String url;
long score, n;
long date;
}
static PersistentMap scores; // key = url
public static void main(String[] args) throws Exception {
scores = new PersistentMap("turing-scores.map");
tests();
prot("It is unix day " + unixDay() + ", and we are doing some Turing testing!");
prot("Test ID: " + formatSnippetID(getProgramID()));
prot();
prot("Made " + l(qlist) + " questions.");
prot();
prot("Checking " + url + "...");
int score = 0;
{ long __time_startTime = now(); try {
for (List qa : qlist) try {
String q = qa.get(0), a = qa.get(1);
String result = loadPage(url + hquery(param, q));
result = htmldecode(result);
prot(quote(q) + " => " + quote(result));
boolean ok = matchStart(a, result) || matchStart(quote(a), result);
if (ok) { ++score; prot("OK!"); }
} catch (Throwable __e) { printStackTrace(__e); }
} finally { __time_startTime = now()-__time_startTime; saveTiming(__time_startTime); } }
prot("");
prot(format("Score for *: * / *", url, score, l(qlist)));
TuringScore ts = new TuringScore();
ts.date = now();
ts.url = url;
ts.score = score;
ts.n = l(qlist);
logStructure(getProgramFile("turing-tests.log"), ts);
scores.put(url, ts);
saveTextFile(getProgramFile("protocol-" + urlencode(url)), fromLines(protocol));
}
static void add(String q, boolean yes) {
qlist.add(litlist(q, yes ? "yes" : "no"));
}
static void add(String q, long number) {
qlist.add(litlist(q, str(number)));
}
static void add(String q, String s) {
qlist.add(litlist(q, s));
}
static void prot(String s) {
print(s);
protocol.add(s);
}
static void prot() { prot(""); } // Turing
static void tests() {
elements();
}
static void elements() {
for (int _repeat_0 = 0; _repeat_0 < 5; _repeat_0++) {
List l = randomList();
add(format3("how many elements are in the list *?", l), l(l));
}
for (int _repeat_1 = 0; _repeat_1 < 2; _repeat_1++) {
List l = randomList(2, 4);
add(format3("what is the first element in the list *?", l), first(l));
add(format3("what is the last element in the list *?", l), last(l));
add(format3("what is the 2nd element in the list *?", l), get(l, 1));
}
}
static List randomList() {
int n = rand(5);
return randomList(n, n);
}
static List randomList(int minLen, int maxLen) {
int n = minLen + rand(maxLen-minLen+1);
List l = new ArrayList();
for (int _repeat_2 = 0; _repeat_2 < n; _repeat_2++) { l.add(randomID(1)); }
return l;
}
static int rand(int n) {
return random(n);
}
static void logStructure(File logFile, Object o) {
logQuoted(logFile, structure(o));
}
static long unixDay() {
return unixHour()/24;
}
static String hquery(Map params) {
return htmlQuery(params);
}
static String hquery(Object... data) {
return htmlQuery(data);
}
static long unixHour() {
return unixTime()/(60*60);
}
static String htmlQuery(Map params) {
return params.isEmpty() ? "" : "?" + makePostData(params);
}
static String htmlQuery(Object... data) {
return htmlQuery(litmap(data));
}
static long unixTime() {
return now()/1000;
}
static int random(int n) {
return new Random().nextInt(n);
}
static String str(Object o) {
return String.valueOf(o);
}
static File getProgramFile(String progID, String fileName) {
return new File(getProgramDir(progID), fileName);
}
static File getProgramFile(String fileName) {
return getProgramFile(getProgramID(), fileName);
}
static String makePostData(Map