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
Garrett wants to search through a csv file to find all rows that have either the name John or Bob in them and display them out t
Olin [163]

Answer:

A.  grep -E "(John|Bob)" salesemployees.csv

Explanation:

The grep command is used to search text. It searches the given file for lines containing a match to the given strings or words.

How to use Grep Command:

grep 'letter' filename – Search any line that contains word 'letter' in filename on Linux

grep -i 'Alphabet' file1 – A case-insensitive search for the word ‘Alphabet’ in Linux and Unix

grep -R 'root' . – Search all files in the current directory and in all of its subdirectories in Linux for the word ‘root’

8 0
2 years ago
Design an application for Bob's E-Z Loans. The application accepts a client's loan amount and monthly payment amount. Output the
zmey [24]

Answer:

part (a).

The program in cpp is given below.

#include <stdio.h>

#include <iostream>

using namespace std;

int main()

{

   //variables to hold balance and monthly payment amounts

   double balance;

   double payment;

   //user enters balance and monthly payment amounts

   std::cout << "Welcome to Bob's E-Z Loans application!" << std::endl;

   std::cout << "Enter the balance amount: ";

   cin>>balance;

   std::cout << "Enter the monthly payment: ";

   cin>>payment;

   std::cout << "Loan balance: " <<" "<< "Monthly payment: "<< std::endl;

   //balance amount and monthly payment computed

   while(balance>0)

   {

       if(balance<payment)    

         { payment = balance;}

       else

       {  

           std::cout << balance <<"\t\t\t"<< payment << std::endl;

           balance = balance - payment;

       }

   }

   return 0;

}

part (b).

The modified program from part (a), is given below.

#include <stdio.h>

#include <iostream>

using namespace std;

int main()

{

   //variables to hold balance and monthly payment amounts

   double balance;

   double payment;

   double charge=0.01;

   //user enters balance and monthly payment amounts

   std::cout << "Welcome to Bob's E-Z Loans application!" << std::endl;

   std::cout << "Enter the balance amount: ";

   cin>>balance;

   std::cout << "Enter the monthly payment: ";

   cin>>payment;

   balance = balance +( balance*charge );

   std::cout << "Loan balance with 1% finance charge: " <<" "<< "Monthly payment: "<< std::endl;

   //balance amount and monthly payment computed

   while(balance>payment)

   {

           std::cout << balance <<"\t\t\t\t\t"<< payment << std::endl;

           balance = balance +( balance*charge );

           balance = balance - payment;

       }

   if(balance<payment)    

         { payment = balance;}          

   std::cout << balance <<"\t\t\t\t\t"<< payment << std::endl;

   return 0;

}

Explanation:

1. The variables to hold the loan balance and the monthly payment are declared as double.

2. The program asks the user to enter the loan balance and monthly payment respectively which are stored in the declared variables.  

3. Inside a while loop, the loan balance and monthly payment for each month is computed with and without finance charges in part (a) and part (b) respectively.

4. The computed values are displayed for each month till the loan balance becomes zero.

5. The output for both part (a) and part (b) are attached as images.

4 0
2 years ago
Arrays are described as immutable because they are two dimensional. are arranged sequentially. can be reordered. cannot be chang
Vlada [557]

Answer:

Arrays are described as immutable because they cannot be changed once they are defined.  (D on Edge)

Explanation:

It's in the notes and I just took the test (2020)

6 0
2 years ago
A program is loaded into _________ while it is being processed on the computer and when the program is not running it is stored
erik [133]
Idkkkkkkkkkkkkkkkkkkk
4 0
1 year ago
Read 2 more answers
1. What should you do if your computer is shared by your entire family and you install a plugin that saves user names and passwo
Citrus2011 [14]

Explanation:

1 make sure only you know the password

2 having weak security on one browser is basically a doorway for someone to get into your network

7 0
2 years ago
Read 2 more answers
Other questions:
  • Which of the following word pairs correctly completes the sentence below?
    15·2 answers
  • 9.10: Reverse ArrayWrite a function that accepts an int array and the array ’s size as arguments . The function should create a
    9·1 answer
  • Which one of the following is the best example of an authorization control? (a)- Biometric device (b)- Digital certificate (c)-A
    11·1 answer
  • QUESTION 2 of 10: New shoes are on SALE. You find a pair you like for $85 dollars. But you only have $45 with you. So, you pay $
    13·1 answer
  • Given a positive integer, output its complement number. The complement strategy is to flip the bits of its binary representation
    6·1 answer
  • A company with a large number of hosts creates three subdomains under a main domain. For easier management of the host records,
    11·1 answer
  • Which two functions are provided to users by the context-sensitive help feature of the Cisco IOS CLI? (Choose two.)
    7·1 answer
  • You are having a problem with your Windows computer that is isolated to a single graphics editing program that you use every day
    12·1 answer
  • A large offshore oil platform needs to transmit signals between different devices on the platform with high reliability and low
    8·1 answer
  • A drive is small enough to be carried in one's pocket.
    6·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!