Question

In: Computer Science

Design and write a python program that reads a file of text and stores each unique...

Design and write a python program that reads a file of text and stores each unique word in some node of binary search tree while maintaining a count of the number appearance of that word. The word is stored only one time; if it appears more than once, the count is increased. The program then prints out 1) the number of distinct words stored un the tree, Function name: nword 2) the longest word in the input, function name: longest 3) the most frequent word in the input, function name: mostfreq 4) the first 10 words appearing in reverse sorted order : first10

In all three format Preorder Postorder Inorder

Solutions

Expert Solution

Answer : Given data

* The word is stored only one time; if it appears more than once, the count is increased.

def nword(txt):
    word_list=txt.split() #splitting text into an list of words
    unique_words = set(word_list) #Converting List into set [Set remove Duplicates]
    result=[]
    for word in unique_words:
        if word_list.count(word)==1: #checking count of each word in text is 1 or not
            result.append(word) # Storing them in a list named result
    return result
def longest(txt):
    word_list=txt.split() #splitting text into an list of words
    sorted_list = sorted(word_list, key=len) # Sorting List Using length
    return sorted_list[-1] #return longest word  usually appears at last of list accessed by [-1]
def mostfreq(txt):
    word_list=txt.split() #splitting text into an list of words
    unique_words = set(word_list) #Converting List into set [Set remove Duplicates]
    maxfreq=1
    mostfreq='unknown'
    for word in unique_words:
        if word_list.count(word)>maxfreq: #checking count of each word in text is 1 or not
            mostfreq=word # Storing most fequent word
            maxfreq=word_list.count(word) # updating maximum Frequency
    return mostfreq # returning most fequent word
def first10(txt):
    word_list=txt.split() #splitting text into an list of words
    f10=word_list
    if len(word_list)>=11:
        f10=word_list[:10] #Splitting list to first 10 words
    result=[]
    for i in f10:
        result.append(i[::-1]) #reversing and storing element in result
    result.sort()
    return result #Returning Sorted result
f = open("text.txt", "r") #opening a file
txt=f.read() #reading a file
print(nword(txt)) # the number of distinct words stored in the tree
print(longest(txt)) #the longest word in the input, function name
print(mostfreq(txt)) #the most frequent word in the input
print(first10(txt)) #the first 10 words appearing in reverse sorted order

Related Solutions

Python program: Write a program that reads a text file named test_scores.txt to read the name...
Python program: Write a program that reads a text file named test_scores.txt to read the name of the student and his/her scores for 3 tests. The program should display class average for first test (average of scores of test 1) and average (average of 3 tests) for each student. Expected Output: ['John', '25', '26', '27'] ['Michael', '24', '28', '29'] ['Adelle', '23', '24', '20'] [['John', '25', '26', '27'], ['Michael', '24', '28', '29'], ['Adelle', '23', '24', '20']] Class average for test 1...
Write a program in c that reads the content from the file and stores each line...
Write a program in c that reads the content from the file and stores each line in an int array in heap(using dynamic memory allocation). For example, let the file has elements following (we do not know the size of files, it could be above 100,000 and contents of the file and make sure to convert file elements to int): 10067 26789 6789 3467
Write a C program that Reads a text file(any file)  and writes it to a binary file....
Write a C program that Reads a text file(any file)  and writes it to a binary file. Reads the binary file and converts it to a text file.
Write a program that reads a file called document.txt which is a text file containing an...
Write a program that reads a file called document.txt which is a text file containing an excerpt from a novel. Your program should print out every word in the file that contains a capital letter on a new line to the stdout. For example: assuming document.txt contains the text C++
Write a simple text-formating.cpp file that reads (asks for then reads) a text file and produces...
Write a simple text-formating.cpp file that reads (asks for then reads) a text file and produces another text file in Which blank lines are removed, multiple blanks are replaced with a single blank, and no lines are longer than some given length (let say 80). Put as many words as possible on the same line (as close as possible to 80 characters). You will have to break some lines of the given file, but do not break any words or...
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...
You are given a text file containing a short text. Write a program that 1. Reads...
You are given a text file containing a short text. Write a program that 1. Reads a given text file : shortText.txt 2. Display the text as it is 3. Prints the number of lines 4. Prints the occurences of each letter that appears in the text. [uppercase and lowercase letter is treated the same]. 5. Prints the total number of special characters appear in the text. 6. Thedisplayofstep3,4and5aboveshouldbesaveinanoutputfile:occurencesText.txt write it in C++ programing Language
This is a python file Reads information from a text file into a list of sublists....
This is a python file Reads information from a text file into a list of sublists. Be sure to ask the user to enter the file name and end the program if the file doesn’t exist. Text file format will be as shown, where each item is separated by a comma and a space: ID, firstName, lastName, birthDate, hireDate, salary Store the information into a list of sublists called empRoster. EmpRoster will be a list of sublists, where each sublist...
C Programming Write a program in C that reads in a file, stores its contents as...
C Programming Write a program in C that reads in a file, stores its contents as a character array/pointer (char*) into an unsigned character array/pointer (unsigned char* message). Note: the input file can have one line or multiple lines and vary in length
Could you write a c- program that reads a text file into a linked list of...
Could you write a c- program that reads a text file into a linked list of characters and then manipulate the linked list by making the following replacements 1. In paragraph 1 Replace all “c” with “s” if followed by the characters “e”, “i” or “y”; otherwise 2. In pragraph 2 Replace "We" with v"i" This is the text to be manipulated: Paragraph1 She told us to take the trash out. Why did she do that? I wish she would...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT