Skip to content

Instantly share code, notes, and snippets.

@vchahun
Created March 19, 2012 17:03
Show Gist options
  • Save vchahun/2119389 to your computer and use it in GitHub Desktop.
Save vchahun/2119389 to your computer and use it in GitHub Desktop.
Trec evaluation
from contextlib import contextmanager
from tempfile import NamedTemporaryFile
import logging
import re
import requests
EVAL = "http://boston.lti.cs.cmu.edu/classes/11-741/HW/HW3/upload.cgi"
def html2txt(html):
txt = re.sub("<[^>]+>", "\n", html)
txt = "\n".join(l.strip() for l in txt.split('\n') if l.strip())
return txt
class TrecFile:
def __init__(self):
self.f = NamedTemporaryFile()
self.f.write("QueryID\tQ0\tDocID\tRank\tScore\tRunID\n")
def result(self, *cols):
self.f.write("\t".join(map(str, cols))+'\n')
def eval(self):
self.f.seek(0)
logging.info("Sending the file for evaluation...")
r = requests.post(EVAL, files = {"infile": self.f})
return html2txt(r.text)
def __enter__(self):
return self
def __exit__(self, *args):
pass
"""
Example use
"""
with TrecFile() as trec_file:
for qid, d, i, r in my_results:
trec_file.result(qid, "Q0", d, i+1, r, "run-1")
print trec_file.eval()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment