def DCG(sysID,queryID,rank): DCG = 0.0#iniatialize #input qrels and results from documents qrels = doc_process.qrels_input() results = doc_process.results_input('S'+str(sysID)+'.results') #sepreate value and docID from qrels FN = [item[0] for item in qrels[queryID][1:]] IR_values = [item[1] for item in qrels[queryID][1:]] #extract docID from retrieved documents FP_all = [item[1] for item in results[queryID]] '''trying to get the relvant docID and its value from retrieved system documents store the index of docID and value of docID together in the list: indexandValue_of_rank, for the calculation of DG ''' for i,x in enumerate(FP_all): #care '=' causer i start with 0 and rank start with 1 if i >= rank: break if x in FN: #[0] in indexandValue_of_rank means the rank in retrieved system, index start at 0 #[1] in indexandValue_of_rank means the value of the docID in [0] position #then calculate the DG with the rank[0] and value[1],this list just for understanding the #process easily, not necessary #indexandValue_of_rank.append([i,IR_values[FN.index(x)]]) #calculation of DG ,the index should start with 1 DCG += float(DG(i+1,IR_values[FN.index(x)])) return(DCG)
def r_Precision(sysID): qrels = doc_process.qrels_input() r_Precision = [] r_Precisions = [] for i,item in enumerate(qrels): #Call precision_recall_atrank function with rank input = len(item)-1 : delete the first queryID element r_Precisions.append(precision_recall_atrank(sysID,len(item)-1,i)[0]) return r_Precisions
def iDCG(sysID,queryID,rank): iDCG = 0.0#iniatialize #data input qrels = doc_process.qrels_input() results = doc_process.results_input('S'+str(sysID)+'.results') iDCG_values = sorted([item[1] for item in qrels[queryID][1:]],reverse=True) for i,item in enumerate(iDCG_values): if i >= rank: break iDCG += DG(i+1,int(item)) return iDCG
def precision_recall_atrank(sysID,rank,queryID): Precisions = [] Recalls = [] qrels = doc_process.qrels_input() results = doc_process.results_input('S'+str(sysID)+'.results') #FN = qrels[i][1:] #extract relevant docIDs from query as FN FN = [item[0] for item in qrels[queryID][1:]] #FP = results[i][0:10] #extract retrieved docIDs for specifc query as FP FP = [item[1] for item in results[queryID][0:rank]] TP = set(FN) & set(FP) Pres = Precision(len(TP),len(FP),len(FN)) recall = Recall(len(TP),len(FP),len(FN)) return Pres,recall
def AP(sysID,queryID): #data inputs qrels = doc_process.qrels_input() results = doc_process.results_input('S'+str(sysID)+'.results') Precisions = [] #FN = qrels[i][1:] FN = [item[0] for item in qrels[queryID][1:]] #FP = results[i][0:10] FP_all = [item[1] for item in results[queryID]] index_of_rank = [i for i, x in enumerate(FP_all) if x in FN] #index in FP_all from zero # for i,index in enumerate(index_of_rank): Pres = float(i+1)/(index+1) Precisions.append(Pres) if Precisions == []: return 0 else: return sum(Precisions)/len(FN)