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 the programs in JavaScript: Write a program that reads a text file and outputs the...
Write the programs in JavaScript: Write a program that reads a text file and outputs the text file with line numbers at the beginning of each line.
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++
(PYTHON) Write a program that does the following: reads each line from a txt file and...
(PYTHON) Write a program that does the following: reads each line from a txt file and convert it to lowercase counts the number of instances of: the characters 'a', 'e','i','o' and 'u' in the file creates a new file of file type .vowel_profile print outs lines in the file indicating the frequencies of each of these vowels Example input/output files: paragraph_from_wikipedia.txt (sample input) link: https://cs.nyu.edu/courses/fall19/CSCI-UA.0002-007/paragraph_from_wikipedia.txt paragraph_from_wikipedia.vowel_profile (sample output) link: https://cs.nyu.edu/courses/fall19/CSCI-UA.0002-007/paragraph_from_wikipedia.vowel_profile Please help!
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...
Write a program in python that reads the elements of a set from the keyboard, stores...
Write a program in python that reads the elements of a set from the keyboard, stores them in a set, and then determines its powerset. Specifically, the program should repeatedly ask the user: Enter one more element ? [Y/N] If the user answers Y then an new element is read from the keyboard: Enter the new element in the set: This cycle continues until the user answers N to the first question. At that point the program shall compute the...
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
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT