answer.
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
Bingel [31]
2 years ago
8

When an author produce an index for his or her book, the first step in this process is to decide which words should go into the

index; the second is to produce a list of the pages where each word occurs. Instead of trying to choose words out of our heads, we decided to let the computer produce a list of all the unique words used in the manuscript and their frequency of occurrence. We could then go over the list and choose which words to put into the index.
The main object in this problem is a "word" with associated frequency. The tentative definition of "word" here is a string of alphanumeric characters between markers where markers are white space and all punctuation marks; anything non-alphanumeric stops the reading. If we skip all un-allowed characters before getting the string, we should have exactly what we want. Ignoring words of fewer than three letters will remove from consideration such as "a", "is", "to", "do", and "by" that do not belong in an index.

In this project, you are asked to write a program to read any text file and then list all the "words" in alphabetic order with their frequency together appeared in the article. The "word" is defined above and has at least three letters.

Computers and Technology
1 answer:
Igoryamba2 years ago
7 0

Answer:

import string

dic = {}

book=open("book.txt","r")

# Iterate over each line in the book

for line in book.readlines():

   tex = line

   tex = tex.lower()

   tex=tex.translate(str.maketrans('', '', string.punctuation))

   new = tex.split()

   for word in new:

       if len(word) > 2:

           if word not in dic.keys():

               dic[word] = 1

           else:

               dic[word] = dic[word] + 1

for word in sorted(dic):

   print(word, dic[word], '\n')

                 

book.close()

Explanation:

The code above was written in python 3.

<em>import string </em>

Firstly, it is important to import all the modules that you will need. The string module was imported to allow us carry out special operations on strings.

<em>dic = {} </em>

<em>book=open("book.txt","r") </em>

<em> </em>

<em># Iterate over each line in the book</em>

<em>for line in book.readlines(): </em>

<em> </em>

<em>    tex = line </em>

<em>    tex = tex.lower() </em>

<em>    tex=tex.translate(str.maketrans('', '', string.punctuation)) </em>

<em>    new = tex.split() </em>

<em />

An empty dictionary is then created, a dictionary is needed to store both the word and the occurrences, with the word being the key and the occurrences being the value in a word : occurrence format.

Next, the file you want to read from is opened and then the code iterates over each line, punctuation and special characters are removed from the line and it is converted into a list of words that can be iterated over.

<em />

<em> </em><em>for word in new: </em>

<em>        if len(word) > 2: </em>

<em>            if word not in dic.keys(): </em>

<em>                dic[word] = 1 </em>

<em>            else: </em>

<em>                dic[word] = dic[word] + 1 </em>

<em />

For every word in the new list, if the length of the word is greater than 2 and the word is not already in the dictionary, add the word to the dictionary and give it a value 1.

If the word is already in the dictionary increase the value by 1.

<em>for word in sorted(dic): </em>

<em>    print(word, dic[word], '\n') </em>

<em>book.close()</em>

The dictionary is arranged alphabetically and with the keys(words) and printed out. Finally, the file is closed.

check attachment to see code in action.

You might be interested in
A chemical found in the synaptic vesicles , which , when released . has an effect on the next cell is called a?
ruslelena [56]
Neurotransmitters is the chemical found in the synaptic vesicles that, when released, has an effect on the next cell.
7 0
2 years ago
The terminal window wants to evaluate your current bash knowledge by using the ~/workspace/nested-directories folder:
tia_tia [17]

Answer:

a. cd into the nested directories/ nested - level - 1 / directory using an absolute path

Explanation:

The directory is a location on the hard disk, which is also called a folder. It contains the files and also contains the other directories called sub directories.

A path to a file is merged with a slash and determines the file or directory in the operating system. An absolute path is the location file or directory from the actual file system

The directory's absolute path starts with a slash, and all slashed in the directory separates the directions.

All directions in the absolute path are written on the left side. The last name in the path may belong to the file, and the pwd command can determine the current directory.

The relative path is the location of the file. It begins with the working directory. An absolute path is unambiguous and working with deeply nested directories.

There are two commands which are used such as

  • cd
  • pwd
  1. cd is used for changing directory
  2. pwd is used for the working directory

We easily navigate the file system with the help of an absolute path.

7 0
2 years ago
You would use the _______ conditional formatting options when analyzing a worksheet in which you want to highlight the highest o
nirvana33 [79]

Answer:

Top/bottom conditional formatting

Explanation:

The top/bottom conditional formatting automatically carries out the task of finding the highest, lowest and even average values.

Conditional formatting formatting gives one the opportunity to enhance reports and dashboards as they work on excel.

You use the too/bottom formatting to highlight cells whose values of highest in a dataset and lowest in a dataset

4 0
1 year ago
Windows is displaying an error about incompatible hardware. You enter BIOS/UEFI setup to change the boot priority order so that
Irina18 [472]

Answer:

A

Explanation:

The most likely cause of the problem is that You signed in to BIOS/UEFI with the user power-on password rather than the supervisor power-on password. The User password only enables the machine to boot while the supervisor password allows entering the BIOS settings.

4 0
2 years ago
Write any 5 activities that help to remove bad events from the society?​
vova2212 [387]

Answer:

1. Awareness program

2. Education

3. women empowerment

6 0
2 years ago
Read 2 more answers
Other questions:
  • Prove that any amount of postage greater than or equal to 64 cents can be obtained using only 5-cent and 17-cent stamps?
    15·1 answer
  • What are the pros and cons of using unique closing reserved words on compound statements?
    15·1 answer
  • In the game Beehive, you play the role of a worker bee who must watch over her hive. Your duties include (among others) directin
    12·2 answers
  • In your own words, what is pair-programming? What is the role of the driver? What is the role of the navigator? What are some be
    15·1 answer
  • __________ access control is a form of __________ access control in which users are assigned a matrix of authorizations for part
    8·1 answer
  • python Write a function that computes a future investment value at a given interest rate for a specified number of years. The fu
    11·1 answer
  • Write a method that takes three numerical String values and sums their values.
    14·2 answers
  • 4. When emergency changes have to be made to systems, the system software may have to be modified before changes to the requirem
    13·1 answer
  • Krista needs to configure the default paste options in PowerPoint 2016. Which area of the Options dialog box will she need to us
    14·1 answer
  • Amanda a recently moved into a new home. Everyone has their own tablet, and wants to connect to the same network, no matter wher
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!