What Is Information Retrieval
Information retrieval (IR) finds documents (text, web pages, papers) that satisfy a user's information need in a large collection of unstructured data. Unlike a database query that returns exact matches, an IR query returns a ranked list of documents that are probably relevant.
Ways to Retrieve Information
- Keyword search: the user types words, and documents containing them are returned.
- Boolean retrieval: keywords combined with AND, OR and NOT; the result is a set without ranking.
- Ranked retrieval (vector space model): documents and the query are vectors of term weights and documents are ranked by similarity.
- Browsing and hyperlink-based ranking (PageRank) on the web.
Indexing
A document is broken into terms by tokenisation; stop words (the, and) are removed and words are reduced to stems. An inverted index maps each term to the list of documents that contain it (with the term frequency), so a query looks at only the lists of its terms.
Ranking by TF-IDF
- TF (term frequency): how often a term occurs in the document, often divided by the document length.
- IDF (inverse document frequency): log(N / df), where N is the number of documents and df the number containing the term; rare terms get a higher weight.
- A document's score for a query is the sum of TF x IDF over the query terms (or the cosine similarity of the vectors).
Evaluating Retrieval
- Precision = relevant documents retrieved / documents retrieved.
- Recall = relevant documents retrieved / all relevant documents.
- F-measure = 2PR / (P + R).
There is a trade-off: retrieving more documents raises recall but usually lowers precision.
Definition: Inverted index. A data structure that maps every term to the list of documents in which it occurs, used to find matching documents without scanning the whole collection.
Table 2.3: Databases and information retrieval
| Point | Database query | IR query |
|---|---|---|
| Data | Structured | Unstructured text |
| Match | Exact | Partial, ranked |
| Result | Set of rows | Ranked list |
| Quality | Correct or not | Precision and recall |
Example 2.3: Precision, recall and ranking
Problem: A collection has 8 documents relevant to a query. The system retrieves 10 documents, of which 6 are relevant. Find the precision, recall and F-measure. Then, for the documents d1 "database systems and database design", d2 "information retrieval systems" and d3 "database retrieval", rank them for the query "database retrieval" (stop word "and" removed, TF divided by length).
Solution:
- Precision = 6 / 10 = 0.6 and recall = 6 / 8 = 0.75.
- F = 2 x 0.6 x 0.75 / (0.6 + 0.75) = 0.667.
- The terms database and retrieval each occur in 2 of 3 documents, so IDF = log10(3 / 2) = 0.1761.
- d1 has 4 terms with database twice: score = (2/4) x 0.1761 = 0.0880. d2 has 3 terms: score = (1/3) x 0.1761 = 0.0587. d3 has 2 terms: score = (1/2 + 1/2) x 0.1761 = 0.1761.
Result: Precision 0.6, recall 0.75 and F 0.667; the ranking is d3, d1, d2.
from math import log10
docs = {"d1": "database systems database design",
"d2": "information retrieval systems",
"d3": "database retrieval"}
index = {}
for d, text in docs.items():
for w in text.split():
index.setdefault(w, {}).setdefault(d, 0)
index[w][d] += 1
print("Index for database:", index["database"])
query = ["database", "retrieval"]
score = {}
for d, text in docs.items():
n = len(text.split())
for w in query:
idf = log10(len(docs) / len(index[w]))
score[d] = score.get(d, 0) + index[w].get(d, 0) / n * idf
print({d: round(s, 4) for d, s in score.items()})
print("Ranking:", sorted(score, key=score.get, reverse=True))
p, r = 6 / 10, 6 / 8
print("Precision", p, "Recall", r, "F", round(2 * p * r / (p + r), 3))
Output
Index for database: {'d1': 2, 'd3': 1}
{'d1': 0.088, 'd2': 0.0587, 'd3': 0.1761}
Ranking: ['d3', 'd1', 'd2']
Precision 0.6 Recall 0.75 F 0.667
Common mistakes
- Treating high precision as the only goal; recall matters as well.
- Counting stop words in the TF-IDF score.
- Expecting exact-match behaviour from an IR system.
- Forgetting to normalise term frequency by document length.
Key takeaways
- IR returns ranked lists from unstructured text.
- Inverted index maps terms to documents.
- TF-IDF = term frequency x log(N / df).
- Precision = relevant retrieved / retrieved; recall = relevant retrieved / all relevant.
Practice questions
- What is an inverted index?
- What does IDF measure?
- How is recall calculated?
Answers to the practice questions
- A mapping from each term to the documents that contain it.
- How rare a term is in the collection.
- Relevant documents retrieved divided by all relevant documents.
Sources
- Official BCA syllabus Advanced Database Systems, course code 25BCA406DS04, Unit II
