Westonci.ca is the premier destination for reliable answers to your questions, brought to you by a community of experts. Get detailed and precise answers to your questions from a dedicated community of experts on our Q&A platform. Connect with a community of professionals ready to provide precise solutions to your questions quickly and accurately.

(Word Occurrences) Write a program word-occurrences.py that accepts filename (str) as command-line argument and words from standard input; and writes to standard output the word along with the indices (ie, locations where it appears in the file whose name is filename - writes "Word not found" if the word does not appear in the file. >_*/workspace/project 6 $ python3 word_occurrences.py data/Beatles.txt dead Center> dead -> (3297, 4118, 4145, 41971 parrot center> Word not found word occurrences.py from instream import InStream from symboltable import Symbol Table import stdio import sys # Entry point. def main (): # Accept filename (str) as command line argument. # Set in Stream to an input stream built from filename. # Set words to the list of strings read from instream # Set occurrences to a new symbol table object. for 1, word in enumerate (...) # For each word (having index 1) in words... # It word does not exist in occurrences, insert it with an empty list as the value. # Append i to the list corresponding to word in occurrences. while . #As long as standard input is not empty.. # Set word to a string read from standard input. # If word exists in occurrences, write the word and the corresponding list to standard Exercises word occurrences.py #output separated by the string Otherwise, write the message Word not found 1 else main()

Sagot :

Answer:

The solution in Python is as follows:

str = "sample.txt"

a_file = open(str, "r")

words = []

for line in a_file:

stripped_line = line.strip()

eachlist = stripped_line.split()

words.append(eachlist)

a_file.close()

word_list= []

n = int(input("Number of words: "))

for i in range(n):

myword = input("Enter word: ")

word_list.append(myword)

for rn in range(len(word_list)):

index = 0; count = 0

print()

print(word_list[rn]+": ",end = " ")

for i in range(len(words)):

 for j in range(len(words[i])):

  index+=1

  if word_list[rn].lower() == words[i][j].lower():

   count += 1

   print(index-1,end =" ")

if count == 0:

 print("Word not found")

Explanation:

See attachment for complete program where comments are used to explain difficult lines

View image MrRoyal
We hope this information was helpful. Feel free to return anytime for more answers to your questions and concerns. We hope you found this helpful. Feel free to come back anytime for more accurate answers and updated information. Get the answers you need at Westonci.ca. Stay informed by returning for our latest expert advice.