# # FIND precision@10 from a vector of files for # one topic (vector of file names) # ############################## ## GLOBAL VARIABLES ############################## qrelsFilePath="D:\\UM Studies_Master_MCS\\Semester_2\\WMGA6320_Special Topics In IS\\Group_Project\\qrels.trec9.main_web" findPrecisionAtTen <-function(currentTopic,systemName,fileNamesForTopic,qrelsMatrix){ # list of documents for specific topic # Qrels file relevantCount<-0 for (j in (1:10)) { if(length(fileNamesForTopic) >= j) { # Get the corresponding entries # for that document ,and then match it # with topic number to check its # relevancy # filter according to file name files_index <- apply(qrelsMatrix, 1, function(row) any (fileNamesForTopic[j] == row)) # filter teh retrieved possibilities to # topic number # note: 4 means one entry.. if(length(qrelsMatrix[files_index,])!=4 && length(qrelsMatrix[files_index,])!=0){ #if you reach here means you have more than one entry topics_index<-apply((qrelsMatrix[files_index,]),1,function(row) any (currentTopic==row)) if(length(qrelsMatrix[files_index,][topics_index,])!=0) { #check if relevant or not if(qrelsMatrix[files_index,][topics_index,4]==1){ # it is relevant document relevantCount=relevantCount+1 }else{ #not relevant ->do nothing } }else{ #do nothing it is not relevant } }else{ # one entry or none.. if(length(qrelsMatrix[files_index,])==4) #one option { #only one entry found -> check the topic and relevancy together. if(qrelsMatrix[files_index,1]==currentTopic && qrelsMatrix[files_index,4]==1){ #relevant relevantCount=relevantCount+1 }else{ # not relevant } }else{ #not in the file -> none relevant } } }else{ #that means that system retrieved less than 10 documents for the topic! # not relevant :) } } #print("current System :") #print(systemName) #print("current Topic : ") #print(currentTopic) #print("precision@10 : ") return (relevantCount/10) } # # START FROM HERE (Main Function) # findPrecsionForTopicsList <- function(qrelsFilePath,runFilePath,systemName,startTopic,endTopic){ # read Qrels File. qrels<-read.table(qrelsFilePath, sep=" ") qrelsMatrix<-as.matrix(qrels) # read System run File. run<-read.table(runFilePath, sep=" ") runMatrix<-as.matrix(run) currentTopic=startTopic#endTopic # Iterate over all the required Topics # and find the corresponding documents array (retrieved by the System) # and use that array and qrels to find P@10 for that topic results <- c(1) i<-1 while(currentTopic <= endTopic){ #filter the documents according to the Topic index <- apply(runMatrix, 1, function(row) any(currentTopic == row)) topicFiles<-(runMatrix[index,3]) answer<-findPrecisionAtTen(currentTopic,systemName,topicFiles,qrelsMatrix) results[i]=answer currentTopic <- currentTopic+1 i<-i+1 } # record the mean of the topics precission@10 results[i]=mean(results) return(results) } initilizeMatrix<-function(start,end,systemsIncluded){ topicStart=start topicEnd=end systemsIncluded=systemsIncluded resultMatrixHieght= topicEnd - topicStart + 3 resultMatrixWidth= systemsIncluded + 1 x <- matrix(1:1, ncol = resultMatrixWidth,nrow= resultMatrixHieght) x[1,1]="T/S" # now fill the corresponding # topics in right places entry=topicStart for(j in (2: resultMatrixHieght)){ x[j,1]=entry entry=entry+1 } x[resultMatrixHieght,1]="Avg" ######################### # end initilize matrix# ######################### return(x) } A <-function(start,end){ ######################### ### START A # ######################### x<-initilizeMatrix(start,end,5) topicStart=start topicEnd=end perSystemPerTopicFileLocation_csv="D:\\UM Studies_Master_MCS\\Semester_2\\WMGA6320_Special Topics In IS\\Group_Project\\results.csv" perSystemPerTopicFileLocation_txt="D:\\UM Studies_Master_MCS\\Semester_2\\WMGA6320_Special Topics In IS\\Group_Project\\results.txt" ########################### #start system Number 1# ########################## systemNumber=1 systemName="input.acsys9mw0" systemFilePath="D:\\UM Studies_Master_MCS\\Semester_2\\WMGA6320_Special Topics In IS\\Group_Project\\input.acsys9mw0" result<-findPrecsionForTopicsList(qrelsFilePath,systemFilePath,systemName,topicStart,topicEnd) x[1,systemNumber+1]=systemName b<-1 for( j in (2:length(result))){ x[j,systemNumber+1]=result[b] b<-b+1 } x[j+1,systemNumber+1]=result[b] ########################### #end system Number 1# ########################## ########################### #start system Number 2# ########################## systemNumber=2 systemName="input.att0010gb" systemFilePath="D:\\UM Studies_Master_MCS\\Semester_2\\WMGA6320_Special Topics In IS\\Group_Project\\input.att0010gb" result<-findPrecsionForTopicsList(qrelsFilePath,systemFilePath,systemName,topicStart,topicEnd) x[1,systemNumber+1]=systemName b<-1 for( j in (2:length(result))){ x[j,systemNumber+1]=result[b] b<-b+1 } x[j+1,systemNumber+1]=result[b] ########################### #end system Number 2# ########################## ########################### #start system Number 3# ########################## systemNumber=3 systemName="input.CWI0000" systemFilePath="D:\\UM Studies_Master_MCS\\Semester_2\\WMGA6320_Special Topics In IS\\Group_Project\\input.CWI0000" result<-findPrecsionForTopicsList(qrelsFilePath,systemFilePath,systemName,topicStart,topicEnd) x[1,systemNumber+1]=systemName b<-1 for( j in (2:length(result))){ x[j,systemNumber+1]=result[b] b<-b+1 } x[j+1,systemNumber+1]=result[b] ########################### #end system Number 3# ########################## ########################### #start system Number 4# ########################## systemNumber=4 systemName="input.dcu00ca" systemFilePath="D:\\UM Studies_Master_MCS\\Semester_2\\WMGA6320_Special Topics In IS\\Group_Project\\input.dcu00ca" result<-findPrecsionForTopicsList(qrelsFilePath,systemFilePath,systemName,topicStart,topicEnd) x[1,systemNumber+1]=systemName b<-1 for( j in (2:length(result))){ x[j,systemNumber+1]=result[b] b<-b+1 } x[j+1,systemNumber+1]=result[b] ########################### #end system Number 4# ########################## ########################### #start system Number 5# ########################## systemNumber=5 systemName="input.Flab9atd2N" systemFilePath="D:\\UM Studies_Master_MCS\\Semester_2\\WMGA6320_Special Topics In IS\\Group_Project\\input.Flab9atd2N" result<-findPrecsionForTopicsList(qrelsFilePath,systemFilePath,systemName,topicStart,topicEnd) x[1,systemNumber+1]=systemName b<-1 for( j in (2:length(result))){ x[j,systemNumber+1]=result[b] b<-b+1 } x[j+1,systemNumber+1]=result[b] ########################### #end system Number 5# ########################## # write the results You got to files... write.table(x, file = perSystemPerTopicFileLocation_csv, append = FALSE, sep = ",",col.names = F, row.names = F) write.table(x, file = perSystemPerTopicFileLocation_txt, append = FALSE, sep = "\t",col.names = F, row.names = F) ######################### ### END A # ######################### }