0% found this document useful (0 votes)
14 views2 pages

example & program for inverted index

Uploaded by

kruti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views2 pages

example & program for inverted index

Uploaded by

kruti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

An inverted index is a data structure used in search engines to map content (like words in

documents) to their locations. It allows for efficient full-text searches. Here’s a simple
example:

Example Documents

1. Doc 1: "apple banana mango"


2. Doc 2: "banana orange apple"
3. Doc 3: "mango orange banana"

Inverted Index

Word Document IDs


apple 1, 2
banana 1, 2, 3
mango 1, 3
orange 2, 3

How It Works

 If a user searches for "apple", the index quickly retrieves documents 1 and 2.
 If a user searches for "banana AND mango", it retrieves documents 1 and 3.

Here’s a simple Python implementation of an inverted index using a


dictionary. It processes multiple documents and allows searching for words
efficiently.

from collections import defaultdict

def build_inverted_index(documents):

inverted_index = defaultdict(set) # Dictionary where values are sets


(to store unique document IDs)

for doc_id, text in enumerate(documents, start=1):

words = text.lower().split() # Convert to lowercase and split words

for word in words:

inverted_index[word].add(doc_id) # Add document ID to the


word's set
return inverted_index

def search(inverted_index, query):

return inverted_index.get(query.lower(), set()) # Return document IDs


or empty set if not found

# Example documents

documents = [

"apple banana mango",

"banana orange apple",

"mango orange banana"

# Build index

index = build_inverted_index(documents)

# Example searches

print("Search for 'apple':", search(index, "apple")) # Output: {1, 2}

print("Search for 'banana':", search(index, "banana")) # Output: {1, 2, 3}

print("Search for 'mango':", search(index, "mango")) # Output: {1, 3}

You might also like