"""Evaluates a candidate based on how close it is to the longest fragment of the re-written question in the document returns (distance, length of fragment) """ return literal_question_distance(rewriteQuestion(question), candidate) def align_question_distance(question, (answer, doc_num, index, features, q_id)): """Evaluates a candidate based on how close it is to the alignment of the question in the document returns (distance, score) """ doc = chunker.clean_punctuation(init.get_doc(doc_num)) (score, q, d, (q_start, d_start), (q_end, d_end)) = sw_align(question, doc) words = doc.split() index = len(" ".join(words[0 : index + 1])) return (min(abs(d_start - index), abs(d_end - index), 0 if d_start <= index <= d_end else MAX_INT), score) def align_rewrite_distance(question, candidate): """Evaluates a candidate based on how close it is to the alignment of the re-written question in the document returns (distance, score) """ return align_question_distance(rewriteQuestion(question), candidate) def rewriteQuestion(question):
#!/usr/bin/env python from align import sw_align from question_rewrite import rewriteQuestion import init def question_apposition(question, (answer, doc_num, index, features,q_id)): doc = init.get_doc(doc_num) apposition = question + ", " + answer + "," plain = question + " " + answer (app_score, app, doc_app, starts, ends) = sw_align(apposition, doc) (plain_score, pl, doc_pl, starts, ends) = sw_align(plain, doc) return [max(0, app_score - plain_score)] def rewrite_apposition(question, candidate): return question_apposition(rewriteQuestion(question), candidate) if __name__ == "__main__": init.get_corpus(qNum=209) question = "Who is the inventor of the phonograph?" doc = "SJMN91-06010225" print question_apposition(question, ("joe smith", doc, 700, {})) print rewrite_apposition(question, ("joe smith", doc, 700, {}))
the re-written question in the document returns (distance, length of fragment) """ return literal_question_distance(rewriteQuestion(question), candidate) def align_question_distance(question, (answer, doc_num, index, features, q_id)): """Evaluates a candidate based on how close it is to the alignment of the question in the document returns (distance, score) """ doc = chunker.clean_punctuation(init.get_doc(doc_num)) (score, q, d, (q_start, d_start), (q_end, d_end)) = sw_align(question, doc) words = doc.split() index = len(" ".join(words[0:index + 1])) print q print d return (min(abs(d_start - index), abs(d_end - index), 0 if d_start <= index <= d_end else MAX_INT), score) def align_rewrite_distance(question, candidate): """Evaluates a candidate based on how close it is to the alignment of the re-written question in the document returns (distance, score) """ return align_question_distance(rewriteQuestion(question), candidate)
from align import sw_align from question_rewrite import rewriteQuestion import init def question_apposition(question, (answer, doc_num, index, features,q_id)): doc = init.get_doc(doc_num) apposition = question + ", " + answer + "," plain = question + " " + answer (app_score, app, doc_app, starts, ends) = sw_align(apposition, doc) (plain_score, pl, doc_pl, starts, ends) = sw_align(plain, doc) return max(0, app_score - plain_score) def rewrite_apposition(question, candidate): return question_apposition(rewriteQuestion(question), candidate) if __name__ == "__main__": init.get_corpus(qNum=209) question = "Who is the inventor of the phonograph?" doc = "SJMN91-06010225" print question_apposition(question, ("joe smith", doc, 700, {})) # print rewrite_apposition(question, ("joe smith", doc, 700, {}))