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]
1 year 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:
Igoryamba1 year 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
Value is always _____________ aligned in a cell. ​
Nadya [2.5K]

Answer:

Right

Explanation:

7 0
1 year ago
Bill needs to make a presentation in which he has to represent data in the form of a pyramid. Which feature or menu option of a
andreyandreev [35.5K]

Answer:If you need to use arrows in a chart, which feature or menu option of a word processing program would you use?

A.  

Picture

B.  

Shapes

C.  

Clip Art

D.  

SmartArt

E.  

Charts

Explanation:

7 0
2 years ago
What is a commonly publicized password sql injection string?
aleksley [76]
The strings "or 1=1" and ""or ''=''"  can be commonly used to trick an SQL WHERE clause into becoming true.

So if you specify <span>' or ''=' as a password, you can log in if the query string would be:

</span><span>select username,pass from users where username='you' and password='' or ''='<span>' limit 0,1;</span></span>
7 0
2 years ago
Write an if-else statement to describe an integer. Print "Positive even number" if isEven and is Positive are both true. Print "
Nina [5.8K]

Answer:

C code explained below

Explanation:

#include <stdio.h>

#include <stdbool.h>

int main(void) {

int userNum;

bool isPositive;

bool isEven;

scanf("%d", &userNum);

isPositive = (userNum > 0);

isEven = ((userNum % 2) == 0);

if(isPositive && isEven){

  printf("Positive even number");

}

else if(isPositive && !isEven){

  printf("Positive number");

}

else{

  printf("Not a positive number");

}

printf("\n");

return 0;

}

6 0
1 year ago
Given positive integer numInsects, write a while loop that prints that number doubled without reaching 200. Follow each number w
Gre4nikov [31]

Answer:

numInsects = 16

while numInsects < 200:

   print(str(numInsects) + " ", end="")

   numInsects *= 2

Explanation:

*The code is in Python.

Set the numInsects as 16

Create a while loop that iterates while numInsects is smaller than 200. Inside the loop, print the value of numInsects followed by a space. Then, multiply the numInsects by 2.

3 0
2 years ago
Other questions:
  • Computer hardware without software is useless while computer software without hardware is meaningless. Discuss. Plz provide exam
    15·1 answer
  • Each frame is composed of a number of colors recorded in digital format; we call these pixels. What information does the number
    15·1 answer
  • Which key retains its uniqueness even after you remove some of the fields?
    14·1 answer
  • . Write a function wordscramble that will receive a word in a string as an input argument. It will then randomly scramble the le
    10·1 answer
  • An array of ints, arr, has been declared and initialized. Write the statements needed to reverse the elements in the array. So,
    10·1 answer
  • Suppose that each row of an n×n array A consists of 1’s and 0’s such that, in any row i of A, all the 1’s come before any 0’s in
    15·1 answer
  • For a class project, Jerome builds a simple circuit with a battery and three light bulbs. On his way to school, Jerome drops his
    9·1 answer
  • A line graph titled Unemployment Percentages and Levels of Education where the x-axis shows dates from November 2007 to November
    11·1 answer
  • How does Accenture help companies harness the power of data to achieve optimal business outcomes?
    9·2 answers
  • A homeowner uses a smart assistant to set the house alarm, get packages delivery updates, and set time on the outdoor lights. Wh
    12·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!