j = Judge()

# f = open("user_notes_cleaned.txt")
f = open("../../data/user_notes_cleaned.txt")

counter = 1
print "sys userno topicid qno noteno noteid docno prec"
for s in f:
    s = s.strip()
    system, userno, topicid, q, note_no = s.split()

    ## remove qustion mark
    if note_no[-1] == "?" or note_no[-1] == "!":
        note_no = note_no[:-1]

    if note_no == "":
        continue

    print system, userno, topicid, q, note_no,
    nid, docid, note = get_note(system, userno, topicid, note_no)

    note = note.replace("\r\n", " ")
    note = note.lower()

    prec = j.check_annotation(topicid, docid, note)
    print nid, docid, prec[1]

    sys.stderr.write("\rLines processed: %d" % counter)
    sys.stderr.flush()
    counter += 1
j = Judge()

f = open("master_notes.txt", "a")

con = MySQLdb.connect('localhost', 'root', '!!berkeley', 'yournews-tdt4')
cur = con.cursor()

n = cur.execute("select nid, userid, docid, note from notes where userid = 'vst' order by nid desc" ) 

counter = 1
res = []
for row in cur:
	nid, userid, docid, note = row

	note = note.replace("\r\n", " ")
	note = note.lower()
	
	precision = j.check_annotation('40004', docid, note)
	try:
		sys.stdout.write("%s %s %s %s %s %s\n" % (nid, system, user, topic, docid, precision[1])) #, note
	except:
		sys.stdout.write("%s %s %s %s %s error\n" % (nid, system, user, topic, docid))
	
#	sys.stderr.write("\r%d/%d" % (counter, n))
#	sys.stderr.flush()
	counter += 1

	res.append(float(precision[1]))

print sum(res)/len(res)