Question

This assignment involves using a binary search tree (BST) to keep track of all words in...

This assignment involves using a binary search tree (BST) to keep track of all words in a text document. It produces a cross-reference, or a concordance. This is very much like assignment 4, except that you must use a different data structure. You may use some of the code you wrote for that assignment, such as input parsing, for this one.

Remember that in a binary search tree, the value to the left of the root is less than the root and the value to the right is greater than the root.

The program will ask for the name of a text file. It will then read the file and keep track each of the words in the file, the number of times it occurs, and which line numbers contain the word. If a word occurs more than once in a line, count it more than once but do not duplicate the line number. Words in the document are separated by spaces and punctuation, which are the following: ? , . !;:-. That is, question mark, period, comma, exclamation point, semicolon, colon, and hyphen. Ignore parentheses and quotation marks. Contractions such as “don’t” are considered a single word. Your test data will not contain numbers. It may contain blank lines, which count in the line numbering but which, containing no words, are ignored. Plurals and variations of a word are considered different. Ignore capitalization; Word and word are the same. Your program will exit after printing the output.

Since part of this is learning how to use classes, you will have to create your own binary search tree node and binary search tree classes. You may not use those classes from the textbook nor any other source. Write only those functions you need to fulfill the assignment.

Remember how string functions work. Remember to write small “helper” functions for various tasks.

Print the text as you read it, preceded by a line number. Once you have reached the end of the file, print a blank line, then the output.

Your output will be the words in alphabetical order, the number of times the word occurs in the file, and the line numbers on which it occurs. Separate the line numbers with a comma and a space, as shown. To make things a little more interesting, ignore the following words: the, a, an.

Sample output for the above paragraph would start thus:

alphabetical        1 1

and                        1 1

word                      1 1

words                    2 1,3

your                       1 1

At the end print the total number of words, the total number of unique words, and the total number of lines.

Homework Answers

Answer #1
import re
file = open("C:\data.txt", "rt")
data = file.read()
word_list=re.split('; |, |:| | ? |- | ! | .',data)

print('Number of words in text file :', len(words))
unique=0
for word in word_list:
        if word not in word_list:
            unique=unique+1
  
print('Number of unique words in text file :', unique)
Content = file.read() 
CoList = Content.split("\n") 
  
for i in CoList: 
    if i: 
        Counter += 1
          
print("This is the number of lines in the file") 
print(Counter) 
Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
You are given a reference to the root node of a binary search tree, that implements...
You are given a reference to the root node of a binary search tree, that implements a dictionary data structure. Please print all the elements in depths 500 through 510, all in sorted order. A node in a binary search tree is at depth x, if it takes x hops to get from the root. So the root is at depth 0, the children of the root are at depth 1, and so on. The class TreeNode defines a single...
In this lab, you will write a program that creates a binary search tree based on...
In this lab, you will write a program that creates a binary search tree based on user input. Then, the user will indicate what order to print the values in. **Please write in C code** Start with the bst.h and bst.c base code provided to you. You will need to modify the source and header file to complete this lab. bst.h: #ifndef BST_H #define BST_H typedef struct BSTNode { int value; struct BSTNode* left; struct BSTNode* right; } BSTNode; BSTNode*...
Binary Search Tree Code in Java Write a program that prompts user to enter however many...
Binary Search Tree Code in Java Write a program that prompts user to enter however many numbers they want to add to the binary search tree. Then have the user input numbers until the tree contains that many elements. If the user enters a number that already exists in the tree, then a message should be displayed "Number is already in the tree". Once all elements are added to the tree print its contents in sorted order. Assume all user...
Here is a picture of a Binary Search Tree. First, construct the Binary Search Tree using...
Here is a picture of a Binary Search Tree. First, construct the Binary Search Tree using the following BinaryNode as we discussed in class. public class BinaryNode { private int value; private BinaryNode leftChild; private BinaryNode rightChild; public BinaryNode(int value) { this.value = value; leftChild = null; rightChild = null; } public BinaryNode(int value, BinaryNode leftChild, BinaryNode rightChild) { this.value = value; this.leftChild = leftChild; this.rightChild = rightChild; } public int getValue() { return value; } public void setValue(int value)...
Create an add method for the BST (Binary Search Tree) class. add(self, new_value: object) -> None:...
Create an add method for the BST (Binary Search Tree) class. add(self, new_value: object) -> None: """This method adds new value to the tree, maintaining BST property. Duplicates must be allowed and placed in the right subtree.""" Example #1: tree = BST() print(tree) tree.add(10) tree.add(15) tree.add(5) print(tree) tree.add(15) tree.add(15) print(tree) tree.add(5) print(tree) Output: TREE in order { } TREE in order { 5, 10, 15 } TREE in order { 5, 10, 15, 15, 15 } TREE in order {...
Write a python program that will perform text analysis on an input text using all of...
Write a python program that will perform text analysis on an input text using all of the following steps: 1. Take the name of an input file as a command line argument. For example, your program might be called using python3 freq.py example1 to direct you to process a plain text file called example. More information is given on how to do this below. 2. Read the contents of the file into your program and divide the text into a...
C++ PROJECT Objectives • To solve problems using vectors • To apply sorting Introduction Two words...
C++ PROJECT Objectives • To solve problems using vectors • To apply sorting Introduction Two words are anagrams of each other if one can be produced by a reordering of letters of the other. For example, “resistance” and “ancestries” are a pair of anagrams. Another anagram pair is “admirer” and “married”. Each anagram forms an equivalence relation since it is: • reflexive (each word is an anagram of itself) • symmetric (if w1 is an anagram of w2 then w2...
6.27 At the end of this and other textbooks, there usually is an index that lists...
6.27 At the end of this and other textbooks, there usually is an index that lists the pages where a certain word appears. In this problem, you will create an index for a text but, instead of page number, you will use the line numbers. You will implement function index() that takes as input the name of a text file and a list of words. For every word in the list, your function will find the lines in the text...
The Binary Search Tree implementation for bst.zip. The code in the destructor of the BST class...
The Binary Search Tree implementation for bst.zip. The code in the destructor of the BST class is empty. Complete the destructor so the memory allocated for each node in the BST is freed. Make a couple of different trees in your main method or in a function to test the destructor (the program should not crash upon exiting). bst.zip (includes the following files below in c++): bst.h: #pragma once #include #include "node.cpp" using namespace std; template class BST { public:...
Description The word bank system maintains all words in a text file named words.txt. Each line...
Description The word bank system maintains all words in a text file named words.txt. Each line in the text file stores a word while all words are kept in an ascending order. You may assume that the word length is less than 20. The system should support the following three functions: Word lookup: to check whether a given word exists in the word bank. Word insertion: to insert a new word into the word bank. No insertion should be made...