1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Ugo [173]
3 years ago
6

The program 4 should first tell users that this is a word analysis file. For any user-given text file, the program will read, an

alyze, and write each word with the line numbers where the word is found in an output file. A word may appear in multiple lines. A word shows more than once at a line, the line number will be only recorded one time.Ask a user to enter the name of a text file. Using try/except for invalid user input. Then the program reads the contents of the text file and create a dictionary in which the key-value pairs are described as follows:" Key. The key are the individual words found in the file. "Value. Each value is a list that contains the line numbers in the file where the word (the key) is found. Be aware that a list may have only one element.Once the dictionary has been built, the program should create another text file, named "word_index.txt". Next, write the contents of the dictionary to the file as an alphabetical listing of the words that are stored as keys in the dictionary (sorting the keys), along with the line numbers where the words appear in the original file. Please see the sample file for reference.Looking to seeing everyone to submit a well-done program! Here are some tips: "Documents/Comments of your program (Never more) "Testing your program by the given two files, Kennedy.txt and romeo.txt. The output file of the Kennedy.txt, "Kennedy_index.txt" and the output file for romeo.txt, "romeo_index.txt,. Please compare your program files with them. "Text File to be analyzed: romeo.txtArise: 5But: 1It: 3Juliet: 3Who: 7already: 7and: 3, 5, 7breaks: 1east: 3envious: 5fair: 5grief: 7is: 3, 7kill: 5light: 1moon: 5pale: 7sick: 7soft: 1sun: 3, 5the: 3, 5through: 1what: 1window: 1with: 7yonder: 1Text File to be analyzed: Kennedy.txtWe: 1a: 1, 2, 4an: 3as: 4, 5, 6beginning: 4but: 2celebration: 2change: 6end: 3freedom: 3not: 1observe: 1of: 2, 3party: 2renewal: 5signifying: 5symbolizing: 3today: 1victory: 1well: 4, 5romeo.txt But soft what light through yonder window breaks It is the east and Juliet is the sun Arise fair sun and kill the envious moon Who is already sick and pale with griefKennedy.txt We observe today not a victory of party but a celebration of freedom symbolizing an end as well as a beginning signifying renewal as well as change
Computers and Technology
1 answer:
djverab [1.8K]3 years ago
7 0

Answer:

See explaination

Explanation:

import os,string

def readfile(filename):

if os.path.isfile(filename) is not True:return None

line_number=1

word_dict={}

print('Text File to be analyzed: {}'.format(filename))

with open(filename,'r') as infile:

for line in infile.readlines():

words = line.strip().split()

for word in words:

word = word.strip(string.punctuation)

if word.strip()=='':continue

if word_dict.get(word)==None:

word_dict[word]=[line_number]

else:

line_number_list=word_dict.get(word)

line_number_list.append(line_number)

line_number+=1

return word_dict

def write_to_file(word_dict,filename):

with open(filename,'w') as outfile:

for word in sorted(word_dict.keys()):

outfile.write('{0}:\t\t{1}\n'.format(word,','.join([str(n) for n in word_dict.get(word)])))

def main():

filename='D:\\Word.txt'

output_filename='D:\\word_index.txt'

word_dict = readfile(filename)

if word_dict ==None:

print('Unable to read file {}'.format(filename))

else:

for word in sorted(word_dict.keys()):

print('{0}:\t\t{1}'.format(word,','.join([str(n) for n in word_dict.get(word)])))

write_to_file(word_dict,output_filename)

main()

You might be interested in
Why are digital signals an accurate and reliable way to record and send information?
Oduvanchick [21]

Answer:

A wave that has been digitized can be played back as a wave over and over, and it will be the same every time. For that reason, digital signals are a very reliable way to record information—as long as the numbers in the digital signal don’t change, the information can be reproduced exactly over and over again.

Explanation:

6 0
2 years ago
Which Operating System is used most often in households?
pshichka [43]

Answer:

Windows

Explanation:

Windows are one of the most popular household os.

3 0
2 years ago
Read 2 more answers
. Java is a high-level language.. true or false?
frutty [35]

Answer:

the answer is true. it is a high level language

4 0
4 years ago
What are the components that make a computer work?
Schach [20]

Answer: Plastic, metal, rubber, wiring, electricity, technology, wifi,  

Explanation: You need  PLASTIC to make a frame for the computer. METAL

to make an exoskeleton under the plastic frame. RUBBER to hold those loose wires and to make sure the electricity doesn't escape and shock you. ELECTRICITY to charge and make the computer runs.  Technology is used for you to log in and out of your computer and WIFI to make sure it doesn't lag.

8 0
3 years ago
Which one can be used as watermark in a word document?​
otez555 [7]

Answer:

On the Design tab, select Watermark.

In the Insert Watermark dialog, select Text and either type your own watermark text or select one, like DRAFT, from the list. Then, customize the watermark by setting the font, layout, size, colors, and orientation. ...

Select OK.

4 0
3 years ago
Other questions:
  • In which area of engineering does material selection play a vital role a design optimization b forensic engineering c mechanical
    6·1 answer
  • What is cyberbullying?
    14·2 answers
  • A porta power tool is what kind of tool
    9·1 answer
  • When you park on a hill, think about which way _____.
    6·2 answers
  • A group of 8 bits is called a... <br> A. hunch <br> B. byte <br> C. dozen <br> D. snack
    13·2 answers
  • Boardman College maintains two files—one for Sociology majors and another for Anthropology majors. Each file contains students'
    5·1 answer
  • Write a for loop to print all NUM_VALS elements of array hourlyTemp. Separate elements with a comma and space. Ex: If hourlyTemp
    7·1 answer
  • Which of the following is the BEST reason to use cash for making purchases? everfi
    15·1 answer
  • Five varieties of software​
    13·1 answer
  • Your program will be used by many departments at the university. Your comments will be important to their IT people. What would
    13·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!