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
(EASY 15 POINTS) What are two indications in a browser that a secure connection is being used?
jeyben [28]
<span><span>High Visibility Online merchants want you to see these site seals. They want you to know they have made every effort to make their site a safe shopping experience. Therefore, the site seal is usually located where you, the customer, can easily see it.</span><span>Difficult to Duplicate The site seals are designed to be difficult for thieves and scammers to duplicate. Many times the site seal will have a date and time stamp on it.</span></span>
4 0
3 years ago
We cannot share software in computer network true or false​
Usimov [2.4K]

Answer:

false

Explanation:

false but other thing is that it can be very risky increasing the chances of getting infected by a virus

6 0
3 years ago
Why would you browse by entering a URL rather than use a link in a Web page
lesya [120]

Answer:

Rather than entering a URL in a web browser, I prefer to browse the webpage. Because if you do browse in the browser you can see other alternative options to that website.

7 0
3 years ago
friend wanted us to decode this message ----- .---- ----- ----- .---- ----- ----- .---- / ----- .---- .---- .---- ----- .---- .-
hodyreva [135]

Answer:

01001001?01110111?01100001?01111001?00100000?01101001?01101011?01100101?01101100?01100001?01111001?00100000?01110101?01101110?01100001?01101100?01100001?01111001?00101110

3 0
3 years ago
Convert the following C program to C++.
ki77a [65]

Answer:

The program equivalent in C++ is:

<em>#include <cstdio></em>

<em>#include <cstdlib></em>

#define SIZE 5

using namespace std;

<em>int main(int argc, char *argv[]) {</em>

<em>int numerator = 25;</em>

<em>int denominator = 10;</em>

<em>FILE * inPut = fopen(argv[1], "r");</em>

<em>FILE * outPut = fopen(argv[2], "w");</em>

<em>float result = (float)numerator/denominator;</em>

<em>fprintf(outPut,"Result is %.2f\n", result);</em>

<em>float arr[SIZE];</em>

<em>for(int i = 0; i < SIZE; i++) {</em>

<em>fscanf(inPut, "%f", &arr[i]);</em>

<em>fprintf(outPut, "%7.4f\n", arr[i]);</em>

<em>}</em>

<em>return 0;</em>

<em>}</em>

Explanation:

See attachment for explanation.

Each line were numbered with equivalent line number in the C program

Download cpp
8 0
3 years ago
Other questions:
  • If you only use part of a quotation, it is not necessary to include a citation
    6·1 answer
  • Easy 25 points answer what is it called that you use only 1 and 0 like this 1010010011001100011110001010010101001010101010010101
    5·1 answer
  • What mistake do you think you might make related to changing the data in a cell that's used in a formula?
    14·1 answer
  • How do i beat the final battle in lara croft: temple of osiris?
    9·1 answer
  • Using the same Apple ID for multiple devices will definitely sync them all together <br> True/False
    7·2 answers
  • What is a phonograph?
    15·2 answers
  • They are correct? thank you!
    5·1 answer
  • Which of the following shows the assignment of a string to a variable? Select 3 options.
    11·2 answers
  • 15. It is the process of capturing data or translating information to recording format
    12·1 answer
  • What web browser feature would be particularly useful when using public computers?
    7·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!