concept search algorithm in category nlp

appears as: search algorithm, search algorithm, search algorithms
Getting Started with Natural Language Processing

This is an excerpt from Manning's book Getting Started with Natural Language Processing.

This chapter will focus on algorithms for information search, which also has a more technical name – information retrieval. It will explain the steps in the search algorithm from beginning to end, and by the end of this chapter you will be able to implement your own search algorithm.

Listing 12 Code to run search algorithm for a given query on the set of documents   
from operator import itemgetter    #A
 
query = qry_vectors.get("3")    #B
results = {}
 
for doc_id in doc_vectors.keys():
    document = doc_vectors.get(doc_id)
    cosine = calculate_cosine(query, document)   
    results[doc_id] = cosine    #C
 
for items in sorted(results.items(), key=itemgetter(1), reverse=True)[:44]:
    print(items[0])    #D

This piece of code returns a list of 44 documents identified by the search algorithm as relevant to query 3, ordered by cosine similarity starting with the most relevant one. A quick glance over the first 10 returned documents (that is how many you would see on the first page in the Internet browser) shows that 8 out of 10 documents are also included in the gold standard. Perhaps even more importantly the top 2 documents in the returned list are relevant according to the gold standard – and you might not even need to look any further than the first couple of documents! This looks like a good result, but how can you get a more comprehensive overview of the results over all queries?