Question

6.27 At the end of this and other textbooks, there usually is an index that lists...

6.27 At the end of this and other textbooks, there usually is an index that lists the pages where a certain word appears. In this problem, you will create an index for a text but, instead of page number, you will use the line numbers. You will implement function index() that takes as input the name of a text file and a list of words. For every word in the list, your function will find the lines in the text file where the word occurs and print the corresponding line numbers (where the numbering starts at 1). You should open and read the file only once.

>>> index('raven.txt', ['raven', 'mortal', 'dying', 'ghost', File: raven.txt 'ghastly', 'evil','demon'])

ghost 9

dying 9

demon 122

evil 99, 106

ghastly 82

mortal 30

raven 44, 53, 55, 64, 78, 97, 104, 111, 118, 120

Homework Answers

Answer #1

def index(filename, wordList):

#created a dictionary with key as words to be indexed and value as a list of line nums
wordCount={}
#creating a key value pair for each word
for word in wordList:
wordCount[word]=[]

lineCount=0
#reading the file line by line
for line in open(filename):
lineCount=lineCount+1
#splitting the line into a list for proper searching
words=line.split()
for word in wordList:
if word in words:
wordCount[word].append(str(lineCount))
  
for word in wordCount:
lineNums=",".join(wordCount[word])
print ("%s : %s")%(word,lineNums)

index("raven.txt",['raven','dead','mortal','grave','ghost'])

sample output:

I have created a file named raven.txt and got this output

you can use your own raven.txt and word of list

ghost : 2
mortal :
grave : 6
dead : 3,6
raven : 4,5

-----------‐------------------------

Please give me a UPVOTE. Thank you ?.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Use Python to Complete the following on a single text file and submit your code and...
Use Python to Complete the following on a single text file and submit your code and your output as separate documents. For each problem create the necessary list objects and write code to perform the following examples: Sum all the items in a list. Multiply all the items in a list. Get the largest number from a list. Get the smallest number from a list. Remove duplicates from a list. Check a list is empty or not. Clone or copy...
You can complete this assignment individually or as a group of two people. In this assignment...
You can complete this assignment individually or as a group of two people. In this assignment you will create a ​​Sorted Singly-Linked List​ that performs basic list operations using C++. This linked list should not allow duplicate elements. Elements of the list should be of type ‘ItemType’. ‘ItemType’ class should have a private integer variable with the name ‘value’. Elements in the linked list should be sorted in the ascending order according to this ‘value’ variable. You should create a...
(Very Easy)You have been provided with the following string matching algorithm: KMP_matcher.py(see below). Using the algorithm,...
(Very Easy)You have been provided with the following string matching algorithm: KMP_matcher.py(see below). Using the algorithm, I want you to find the word(s) that have: 1. Longest Continual Substring of: - Vowels - Consonants 2. Longest Continual Prefix of: - Vowels - Consonants 3. Longest Continual Suffix of: - Vowels - Consonants Example: ["Hello", "World", "Face", "Asthma", "Spring", "Because", "Thorough", "Sequoia" ]. In this list of strings, "Sequoia" has the longest substring of consecutive vowels. "Asthma" has the longest substring...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT