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

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...
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 many words are in the Gettysburg Address? Write a program that reads any text file,...
How many words are in the Gettysburg Address? Write a program that reads any text file, counts the number of characters, num- ber of letters and number of words in the file and displays the three counts. To test your program, a text file containing Lincoln’s Gettysburg Address is included on the class moodle page. Sample Run Word, Letter, Character Count Program Enter file name: GettysburgAddress.txt Word Count = 268 Letter Count = 1149 Character Count = 1440 Do the...
Write a C++ program to open and read a text file and count each unique token...
Write a C++ program to open and read a text file and count each unique token (word) by creating a new data type, struct, and by managing a vector of struct objects, passing the vector into and out of a function. Declare a struct TokenFreq that consists of two data members: (1) string value; and (2) int freq; Obviously, an object of this struct will be used to store a specific token and its frequency. For example, the following object...
Java 20 most frequent words in a text file. Words are supposed to be stored in...
Java 20 most frequent words in a text file. Words are supposed to be stored in array that counts eah word. Write a program that will read an article, parse each line into words, and keep track of how many times each word occurred. Run this program for each of the two articles and print out the 20 most frequently appearing words in each article. You may think you need to use a StringTokenizer, but it turns out that is...
● Write a program that reads words from a text file and displays all the words...
● Write a program that reads words from a text file and displays all the words (duplicates allowed) in ascending alphabetical order. The words must start with a letter. Must use ArrayList. MY CODE IS INCORRECT PLEASE HELP THE TEXT FILE CONTAINS THESE WORDS IN THIS FORMAT: drunk topography microwave accession impressionist cascade payout schooner relationship reprint drunk impressionist schooner THE WORDS MUST BE PRINTED ON THE ECLIPSE CONSOLE BUT PRINTED OUT ON A TEXT FILE IN ALPHABETICAL ASCENDING ORDER...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT