def get_precision(topicid, docs):
	j = Judge()
	rels = []
	for docno in docs:
		rel = j.check(topicid, docno)
		rels.append(rel)
	ndcg = get_ndcg(rels)
		
	return (sum(rels[:5]) / 5., sum(rels) /10., ndcg, rels)
			pass
			
	if old_userid != userid:
		recid = 1

	recid_full = "%s %s %s %s" % (system, userno, topicid, recid)
	if recid_full not in recs:
		recs.append(recid_full)

	for doc in docs:
		try:
			docno, docx, docy = doc.split(",")
		except:
			if doc == "":
				continue
			print ">>> Error in ", doc
			sys.exit(1)
			
		rel = j.check(topicid, docno)
		# rel = 1
		print system, userno, topicid, recid, dimx, dimy, docno, docx, docy, rel, isum
					
	recid += 1
	old_userid = userid
	
	sys.stderr.write("\r%d/%d" % (i, N))
	sys.stderr.flush()
			
for rec in recs:
	print "#", rec
#!/usr/bin/python
import sys; sys.path.append("../common")
from tdt4rel import Judge
import MySQLdb

j = Judge()

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

cur.execute("select userid, object from history where action like 'open_doc%' and userid like 'vs%' and  userid != 'vst'")

for row in cur:
	userid, docid = row
	system, user, topic = userid.split("-")
	print system,  user, topic, docid, j.check(topic, docid)
		
		dim = info.split("\t")[0]
		docs = info.split("\t")[2]
		
		if len(docs) == 5:
			continue
		
		for doc in docs.split():
			temp = doc.split(",")
			docid = temp[0]
	
			locx = temp[1]
			locy = temp[2]
			
			vis[docid] = (locx, locy)
			visx.append(int(locx))
			
	if action.startswith('open_doc'):
		print userid, info, docno,

		print j.check(topic, docno),
		try:
			docx = int(vis[docno][0])
			vis_width = max(visx) - min(visx)
			docx_within = docx - min(visx)
			# print docx, vis_width , docx_within, float(docx_within) / vis_width, max(visx), min(visx)
			print docx, float(docx_within) / vis_width
		except:
			print -1, -1
			
			
con = MySQLdb.connect('localhost', 'root', '!!berkeley', 'yournews-tdt4')
cur = con.cursor()

cur.execute("select unix_timestamp(datetime), userid, action, object from history where userid like 'vs%' and  userid != 'vst' order by userid, datetime asc")

data = cur.fetchall()

um_on = 0
um_vis = 0

for i in range(len(data)):
	time, userid, action, docid = data[i]
	
	if action == 'open_doc':
		system, user, topic = userid.split("-")
		
		next_time, next_action = get_next_action(data, i)
		
		# print "%s %s %s %s %d time_span:%d action:%s um_on:%d um_vis:%d" % (system, user, topic, docid, j.check(topic, docid), next_time - time, next_action, um_on, um_vis)
		print "%s %s %s %s %d %d %s %d %d" % (system, user, topic, docid, j.check(topic, docid), next_time - time, next_action, um_on, um_vis)

	if action == 'login':
		um_on = 0
		um_vis = 0
		
	if action == 'save_note':
		um_on = 1
		
	if um_on == 1 and (action in ['search', 'reset_visualization']):
		um_vis = 1
	if action == 'nav_page':
		page = int(info.split("=")[1])
		
	if action == 'search': # reset page <= check again
		page = 1
	
	if system == 'vsb' and action == 'response':
		docinfo = {}
		docs = info.split()[2:]
		for rank, docno in enumerate(docs):
			docinfo[docno] = 10 * (page - 1) + rank + 1
		
	if (system == 'vse' or system == 'vsn') and action == 'reset_visualization':
		docinfo = {}
		docs = info.split('\t')[2].split(":")[1].split(" ")
		for rank, docno in enumerate(docs):
			docinfo[docno.split(",")[0]] = rank + 1
			
	if action == 'open_doc':
		print system, "open_doc", userno, obj,
		print j.check(topicid, obj),
		if docinfo.has_key(obj):
			print docinfo[obj]
		else:
			print "-1"
			
	if action == 'save_note':
		save_docno = info.split()[0].split("=")[1]
		print system, "save_note", userno, save_docno,
		print j.check(topicid, save_docno),
		print docinfo[save_docno]