Question

In: Computer Science

how to count the word of occurance in the text file example input text file :...

how to count the word of occurance in the text file

example input text file : "test1.txt
hello : 1
good : 1
morning: 1
how : 1
are : 2
you : 2
good : 1

example output text file : "test2.txt"
1 : 4
2 : 2
3 : 0
4 : 0
5 : 0

Solutions

Expert Solution

Python Program:

#***********************************************
# EXTRACT DIGIT FROM FILE
#***********************************************

# Opening file in reading mode
file = open('test1.txt', 'r') 

# Reading from the file and store it in 'content' variable
content = file.readlines() 

# Variable for storing the numbers
numList = []

# Iterating through the contents of the file 
for line in content:
    # traverse in each line
    for i in line:        
        # Check numeric or digit in line
        if i.isdigit() == True: 
            numList.append(int(i)) 

#***********************************************
# FREQUENCY OF NUMBER
#***********************************************
# Calculate Frequency of each number
freq = {} 
for items in numList: 
    freq[items] = numList.count(items)

    # Print Frequnecy Table    
print("\n***************************")
print("Number   |    Frequency")
print("***************************")
for key, value in freq.items(): 
    print ("% s :           % d"%(key, value))
print("***************************")

Text - File => test1.txt

hello : 1
good : 1
morning : 1
how : 1
are : 2
you : 2
good : 1

Output:

Thumbs Up Please !!!


Related Solutions

For example, I have an input text file that reads: "Hello, how are you?" "You look...
For example, I have an input text file that reads: "Hello, how are you?" "You look good today." "Today is a great day." How can I write a function that inputs each line into a node in a linked list? Thank you. (Note: the input text files could have a different amount of lines, and some lines could be blank.) This is the linked list code: #include <iostream> #include <cstdlib> #include <fstream> class Node { public: Node* next; int data;...
How to count the number of words that only show up once in a text file,...
How to count the number of words that only show up once in a text file, and replace those words with a character '(unique)' using Python? Without list is better.
C++ Parse text (with delimiter) read from file. If a Text file has input formatted such...
C++ Parse text (with delimiter) read from file. If a Text file has input formatted such as: "name,23" on every line where a comma is the delimiter, how would I separate the string "name" and integer "23" and set them to separate variables for every input of the text file? I am trying to set a name and age to an object and insert it into a vector. #include <iostream> #include <vector> #include <fstream> using namespace std; class Student{    ...
Your task is to count the frequency of words in a text file, and return the...
Your task is to count the frequency of words in a text file, and return the most frequent word with its count. For example, given the following text: there are two ways of constructing a software design one way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies. Based on the example your program should printout the following along with the...
Your task is to count the frequency of words in a text file, and return the...
Your task is to count the frequency of words in a text file, and return the most frequent word with its count. (Must use the code below without changing algorithms) For example, given the following text: there are two ways of constructing a software design one way is to make it so simple that there are obviously no deficiencies and the other way is to make it so complicated that there are no obvious deficiencies. Based on the example your...
Write a program that reads a text file and reports the total count of words of...
Write a program that reads a text file and reports the total count of words of each length. A word is defined as any contiguous set of alphanumeric characters, including symbols. For example, in the current sentence there are 10 words. The filename should be given at the command line as an argument. The file should be read one word at a time. A count should be kept for how many words have a given length. For example, the word...
Part I The input to the program will be a text file containing the information for...
Part I The input to the program will be a text file containing the information for a tolerance table. An example follows using the values from the first lecture on tolerance analysis. These values will be stored in a text file. The data is comma delimited, which means that each data field is separated by a comma. If the first word is ‘PART’ the following values are the nominal size, +/- impact, tolerance, and fixed/variable. If the first word is...
The word count of the text that includes your “own words” should be at least 500...
The word count of the text that includes your “own words” should be at least 500 words (about 700 max.). The text should be typed double-spaced using a 12-point font size in Times New Roman. You are asked to make your own list of the best places to live (it can be one, two, or more) and convince your reader to move there. Build your case by using as many economic indicators you can think of. How important would the...
C++ Write a word search program that searches an input data file for a word specified...
C++ Write a word search program that searches an input data file for a word specified by the user. The program should display the number of times the word appears in the input data file. In addition, the program should count and display the number of grammatical characters in the input data file. Your program must do this by providing the following function: void processFile(ifstream &inFile, string wordSearch, int &wordCount, int &grammaticalCount); void processFile(ifstream &inFile, string wordSearch, int &wordCount, int...
Word Frequencies (Concordance)    1. Use a text editor to create a text file (ex: myPaper.txt)...
Word Frequencies (Concordance)    1. Use a text editor to create a text file (ex: myPaper.txt) It should contain at least 2 paragraphs with around 200 or more words. 2. Write a Python program (HW19.py) that asks the user to provide the name of the text file. Be SURE to check that it exists! Do NOT hard-code the name of the file! Use the entry provided by the user! read from the text file NOTE: (write your program so that...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT