Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Search engines like Google and Bing work by consulting an index of the words found in each Web page that they examine. This index identifies

Search engines like Google and Bing work by consulting an index of the words found in each Web page that they examine. This index identifies the document(s) that contain a particular word (like "dog" or "cat"), as well as the words position(s) in those documents. For example, Google might record that the Web page "pets.html" contains the word "cat" at positions 22, 35, 100, and 209. Later, when a user searches for the word "cat", Google will return "pets.html" as a hit for that search term.

Complete the buildIndex() function. This function takes one argument: a string representing the name of a plain text file whose contents are to be indexed. buildIndex() returns a dictionary whose keys are the unique words in the text file; each keys value is a list of integer positions where that word appears in the original text. The input file may contain multiple lines, but you may assume that every word is separated by a space, and that the file does not contain any external punctuation like periods or commas. If a word appears in varying cases, treat all of those instances as copies of the same word (for example, "Cat", "CAT", and "cat" all count as copies of a single word: "cat").

For each word in the source, if it is already present in the dictionary, append the current index of the word to the dictionary to the existing key. If the word is not yet in the dictionary, add it as a new entry. After processing the entire source file, buildIndex() returns the index of word locations.

NOTE: Here is some basic pseudocode that may help you to get started:

Set "idx" to an empty dictionary Set "text" to the empty string 
For each line in the input file: Convert the current line into all lowercase Append the current line, followed by a single space, to "text" 
Use strip() to remove all external whitespace from "text" Split "text" into a list of individual words 
For each index in the list of words: Use setdefault() to add the current word to "idx" (its default value is [ ]) Append the current index to the list for the current word 

For example, a file containing the text

Archimedes anticipated modern calculus and analysis by applying concepts of infinitesimals and the method of exhaustion to derive and rigorously prove a range of geometrical theorems including the area of a circle 

would return a dictionary like

{"a":[21, 30], "analysis":[5], "and":[4, 11, 18], "anticipated":[1], "applying":[7], "archimedes":[0], "area":[28], "by":[6], "calculus":[3], "circle":[31], "concepts":[8], "derive":[17], "exhaustion":[15], "geometrical":[24], "including":[26], "infinitesimals" "method":[13], "modern":[2], "of":[9, 14, 23, 29], "prove":[20], "range":[22], "rigorous "the":[12, 27], "theorems":[25], "to":[16]} 

(the exact order of keys may vary)

def buildIndex(filename): # ADD YOUR CODE HERE  return {} # CHANGE OR REPLACE THIS LINE 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Build It For The Real World A Database Workbook

Authors: Wilson, Susan, Hoferek, Mary J.

1st Edition

0073197599, 9780073197593

More Books

Students also viewed these Databases questions