Question

In: Computer Science

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.

Solutions

Expert Solution

In the below program , it will count the number of occurences of all the words.Then it will replace the single occurance word to (unique).This will be easier so that you can have the number of occurences of all the words.

The program is as follows:

text = open ("sample.txt" , "r")   //Opens a file in read mode

d = dict() //just creating an empty Dictionary

for line in text : //Loop to go through each line of the file

line= line. strip()   //Removes the spaces for counting

line=line. lower() //converts to lower case for easy identification

words = line. split() //Split the lines into words

for word in words :   //Iterate over each word in a line

if word in d : //Check if the word is in the directory

d[word]=d[word]+1   //Increment the number of times

else :

d[word]= 1

print(d)

if d[word] ==1 : //If the word is present only once

print("unique") //Prints unique

Note: Read the linewise comments for better understanding.


Related Solutions

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
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...
Read an unsorted keywords file once to determine how many words are in the file. Allocate...
Read an unsorted keywords file once to determine how many words are in the file. Allocate memory dynamically to store the unsorted keywords in an array of strings or an array of c-strings. (Hint: be sure to clear your input file stream before re-reading the file) Reread the keywords file a second time and store the words in the dynamically allocated array of strings or c-strings Sort the array of key words. (Hint: be sure to check your sorted array...
Read an unsorted keywords file once to determine how many words are in the file. Allocate...
Read an unsorted keywords file once to determine how many words are in the file. Allocate memory dynamically to store the unsorted keywords in an array of strings or an array of c-strings. (Hint: be sure to clear your input file stream before re-reading the file) Reread the keywords file a second time and store the words in the dynamically allocated array of strings or c-strings Sort the array of key words. (Hint: be sure to check your sorted array...
Write a C program to find out the number of words in an input text file...
Write a C program to find out the number of words in an input text file (in.txt). Also, make a copy of the input file. Solve in C programming.
Java Write a method that reads a text file and prints out the number of words...
Java Write a method that reads a text file and prints out the number of words at the end of each line
Done in c++, Read an unsorted keywords file once to determine how many words are in...
Done in c++, Read an unsorted keywords file once to determine how many words are in the file. Allocate memory dynamically to store the unsorted keywords in an array of strings or an array of c-strings. (Hint: be sure to clear your input file stream before re-reading the file) Reread the keywords file a second time and store the words in the dynamically allocated array of strings or c-strings Sort the array of key words. (Hint: be sure to check...
How would I read only the first line of text file into C++ For instance, the...
How would I read only the first line of text file into C++ For instance, the first line of a text file is: 5 (space) 5. I need to read these numbers into a row variable and a column variable. I am just not sure how to do it. I can only use the #include header. I can't use any other header. The project mentions using redirected input in Visual Studio for our text file.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT