Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

i need help with this project. You are to write a program to create a frequency list of words in a passage. A frequency list

i need help with this project.

You are to write a program to create a frequency list of words in a passage. A frequency list is a list of the distinct words in the passage together with a cout of the number of times that word occurs. The input will be read from a file (filename provided as first argument on commandline). The output is to be sent to stdout (i.e. cout), sorted alphabetically on the words. The output would also contain an output of the words sorted by frequency count. For each word that is read, we will convert the characters to lower case [so that The and the are counted together] and remove punctuation. The provided wordcollection class does this work for you. You can use a binary tree, sorted on the words, to hold the words and their counts, use the frequency class to make life easier. If a word is seen again, increment the count. I want you to utilize bintree.h to assist in creating and working with the tree. All output of the tree should utilize the inorder, preorder, or postorder convenience traversal functions. Create the tree sorted alphabetically.

// FILE: bintree.h (part of the namespace main_savitch_10)

// PROVIDES: A template class for a node in a binary tree and functions for

// manipulating binary trees. The template parameter is the type of data in

// each node.

//

// TYPEDEF for the binary_tree_node template class:

// Each node of the tree contains a piece of data and pointers to its

// children. The type of the data (binary_tree_node::value_type) is

// the Item type from the template parameter. The type may be any of the C++

// built-in types (int, char, etc.), or a class with a default constructor,

// and an assignment operator.

//

// CONSTRUCTOR for the binary_tree_node class:

// binary_tree_node(

// const item& init_data = Item( ),

// binary_tree_node* init_left = NULL,

// binary_tree_node* init_right = NULL

// )

// Postcondition: The new node has its data equal to init_data,

// and it's child pointers equal to init_left and init_right.

//

// MEMBER FUNCTIONS for the binary_tree_node class:

// const item& data( ) const <----- const version

// and

// Item& data( ) <----- non-const version

// Postcondition: The return value is a reference to the data from

// this binary_tree_node.

//

// const binary_tree_node* left( ) const <----- const version

// and

// binary_tree_node* left( ) <----- non-const version

// and

// const binary_tree_node* right( ) const <----- const version

// and

// binary_tree_node* right( ) <----- non-const version

// Postcondition: The return value is a pointer to the left or right child

// (which will be NULL if there is no child).

//

// void set_data(const Item& new_data)

// Postcondition: The binary_tree_node now contains the specified new data.

//

// void set_left(binary_tree_node* new_link)

// and

// void set_right(binary_tree_node* new_link)

// Postcondition: The binary_tree_node now contains the specified new link

// to a child.

//

// bool is_leaf( )

// Postcondition: The return value is true if the node is a leaf;

// otherwise the return value is false.

//

// NON-MEMBER FUNCTIONS to maniplulate binary tree nodes:

// tempate

// void inorder(Process f, BTNode* node_ptr)

// Precondition: node_ptr is a pointer to a node in a binary tree (or

// node_ptr may be NULL to indicate the empty tree).

// Postcondition: If node_ptr is non-NULL, then the function f has been

// applied to the contents of *node_ptr and all of its descendants, using

// an in-order traversal.

// Note: BTNode may be a binary_tree_node or a const binary tree node.

// Process is the type of a function f that may be called with a single

// Item argument (using the Item type from the node).

//

// tempate

// void postorder(Process f, BTNode* node_ptr)

// Same as the in-order function, except with a post-order traversal.

//

// tempate

// void preorder(Process f, BTNode* node_ptr)

// Same as the in-order function, except with a pre-order traversal.

//

// template

// void print(const binary_tree_node* node_ptr, SizeType depth)

// Precondition: node_ptr is a pointer to a node in a binary tree (or

// node_ptr may be NULL to indicate the empty tree). If the pointer is

// not NULL, then depth is the depth of the node pointed to by node_ptr.

// Postcondition: If node_ptr is non-NULL, then the contents of *node_ptr

// and all its descendants have been written to cout with the << operator,

// using a backward in-order traversal. Each node is indented four times

// its depth.

//

// template

// void tree_clear(binary_tree_node*& root_ptr)

// Precondition: root_ptr is the root pointer of a binary tree (which may

// be NULL for the empty tree).

// Postcondition: All nodes at the root or below have been returned to the

// heap, and root_ptr has been set to NULL.

//

// template

// binary_tree_node* tree_copy(const binary_tree_node* root_ptr)

// Precondition: root_ptr is the root pointer of a binary tree (which may

// be NULL for the empty tree).

// Postcondition: A copy of the binary tree has been made, and the return

// value is a pointer to the root of this copy.

//

// template

// size_t tree_size(const binary_tree_node* node_ptr)

// Precondition: node_ptr is a pointer to a node in a binary tree (or

// node_ptr may be NULL to indicate the empty tree).

// Postcondition: The return value is the number of nodes in the tree.

#ifndef BINTREE_H

#define BINTREE_H

#include // Provides assert

#include // Provides NULL, std::size_t

#include // Provides std::setw

#include // Provides std::cout

namespace FREQPROJ

{

template

class binary_tree_node

{

public:

// TYPEDEF

typedef Item value_type;

// CONSTRUCTOR

binary_tree_node(

const Item& init_data = Item( ),

binary_tree_node* init_left = NULL,

binary_tree_node* init_right = NULL

)

{

data_field = init_data;

left_field = init_left;

right_field = init_right;

}

// MODIFICATION MEMBER FUNCTIONS

Item& data( ) { return data_field; }

binary_tree_node* left( ) { return left_field; }

binary_tree_node* right( ) { return right_field; }

void set_data(const Item& new_data) { data_field = new_data; }

void set_left(binary_tree_node* new_left) { left_field = new_left; }

void set_right(binary_tree_node* new_right) { right_field = new_right; }

// CONST MEMBER FUNCTIONS

const Item& data( ) const { return data_field; }

const binary_tree_node* left( ) const { return left_field; }

const binary_tree_node* right( ) const { return right_field; }

bool is_leaf( ) const { return (left_field == NULL) && (right_field == NULL); }

private:

Item data_field;

binary_tree_node *left_field;

binary_tree_node *right_field;

};

// NON-MEMBER FUNCTIONS for the binary_tree_node:

template

void inorder(Process f, BTNode* node_ptr);

template

void preorder(Process f, BTNode* node_ptr);

template

void postorder(Process f, BTNode* node_ptr);

template

void print(binary_tree_node* node_ptr, SizeType depth);

template

void tree_clear(binary_tree_node*& root_ptr);

template

binary_tree_node* tree_copy(const binary_tree_node* root_ptr);

template

std::size_t tree_size(const binary_tree_node* node_ptr);

//Implementation (formerly bintree.template

template

void inorder(Process f, BTNode* node_ptr)

// Library facilities used: cstdlib

{

if (node_ptr != NULL)

{

inorder(f, node_ptr->left( ));

f( node_ptr->data( ) );

inorder(f, node_ptr->right( ));

}

}

template

void postorder(Process f, BTNode* node_ptr)

// Library facilities used: cstdlib

{

if (node_ptr != NULL)

{

postorder(f, node_ptr->left( ));

postorder(f, node_ptr->right( ));

f(node_ptr->data( ));

}

}

template

void preorder(Process f, BTNode* node_ptr)

// Library facilities used: cstdlib

{

if (node_ptr != NULL)

{

f( node_ptr->data( ) );

preorder(f, node_ptr->left( ));

preorder(f, node_ptr->right( ));

}

}

template

void print(binary_tree_node* node_ptr, SizeType depth)

// Library facilities used: iomanip, iostream, stdlib

{

if (node_ptr != NULL)

{

print(node_ptr->right( ), depth+1);

std::cout << std::setw(4*depth) << ""; // Indent 4*depth spaces.

std::cout << node_ptr->data( ) << std::endl;

print(node_ptr->left( ), depth+1);

}

}

template

void tree_clear(binary_tree_node*& root_ptr)

// Library facilities used: cstdlib

{

binary_tree_node* child;

if (root_ptr != NULL)

{

child = root_ptr->left( );

tree_clear( child );

child = root_ptr->right( );

tree_clear( child );

delete root_ptr;

root_ptr = NULL;

}

}

template

binary_tree_node* tree_copy(const binary_tree_node* root_ptr)

// Library facilities used: cstdlib

{

binary_tree_node *l_ptr;

binary_tree_node *r_ptr;

if (root_ptr == NULL)

return NULL;

else

{

l_ptr = tree_copy( root_ptr->left( ) );

r_ptr = tree_copy( root_ptr->right( ) );

return new binary_tree_node( root_ptr->data( ), l_ptr, r_ptr);

}

}

template

size_t tree_size(const binary_tree_node* node_ptr)

// Library facilities used: cstdlib

{

if (node_ptr == NULL)

return 0;

else

return 1 + tree_size(node_ptr->left( )) + tree_size(node_ptr->right( ));

}

}

#endif

/*

* frequency.h

The Frequency class makes it easier to create a tree of word counts.

For example, by using this class you will be able to do create a binary tree of binary_tree_node >*.

Note that the space after the second closing > is required.

frequency()

Sets Item to blank and frequency to 0. Be careful with this one, recommend the using the other constructor only.

If you use this one you will have to increment frequency when you update the data field.

frequency(Item i)

Constructor with an Item, will initialize frequency to 1

// MODIFICATION MEMBER FUNCTIONS

Item& data( )

Return the data_field that you can change

size_t& operator++()

size_t operator++(int)

Increments the frequency count only

bool isDataInc(Item i)

If i matching the objects data, then it increments the frequency and returns true

// CONST MEMBER FUNCTIONS

const Item& data( ) const

Return the data_field that you cannot change

bool isData(Item i) const

If i matching the objects data, then it returns true

size_t count( ) const

Return the frequency

bool operator< (Item b)

bool operator< (Item b)

bool operator> (Item b)

bool operator> (Item b)

Use this to help decide where a new node should be placed based on data. This will compare the data_field.

Both const and non const versions provided.

bool operator< (size_t b)

bool operator< (size_t b)

bool operator> (size_t b)

bool operator> (size_t b)

Use this to help decide where a node should be placed based on frequency count. This will compare the freq.field.

Both const and non const versions provided.

std::ostream& operator<<(std::ostream& out, const frequency& f)

Outputs the frequency and data_field is a friendly format, no spaces

data_field[freq_field]

*/

#ifndef FREQUENCY_H_

#define FREQUENCY_H_

namespace FREQPROJ

{

template

class frequency

{

public:

frequency() { data_field=Item(); freq_field=0; }

frequency(Item i) { data_field=i; freq_field=1; }

// MODIFICATION MEMBER FUNCTIONS

Item& data( ) { return data_field; }

frequency& operator++() { ++freq_field; return *this; } //pre

frequency operator++(int) { frequency tmp(*this); operator++(); return tmp; } //post

bool isDataInc(Item i) { return (i == data_field && (freq_field += 1)); }

// CONST MEMBER FUNCTIONS

const Item& data( ) const { return data_field; }

bool isData(Item i) const { return i == data_field; }

size_t count( ) const { return freq_field; }

bool operator< (Item b) { return data_field < b; }

bool operator< (Item b) const { return data_field < b; }

bool operator< (size_t b) { return freq_field < b; }

bool operator< (size_t b) const { return freq_field < b; }

bool operator> (Item b) { return data_field > b; }

bool operator> (Item b) const { return data_field > b; }

bool operator> (size_t b) { return freq_field > b; }

bool operator> (size_t b) const { return freq_field > b; }

private:

Item data_field;

size_t freq_field;

};

template

std::ostream& operator<<(std::ostream& out, const frequency& f)

{

out << f.data() << "[" << f.count() << "]"; return out;

}

//More comparisons to get around the fact that FREQ must be on the LEFT without these

template

bool operator > (Item left, frequency& right ) { return left > right.data() ; }

template

bool operator < (Item left, frequency& right ) { return left < right.data() ; }

template

bool operator < (std::size_t left, frequency& right ) { return left < right.count() ; }

template

bool operator > (std::size_t left, frequency& right ) { return left > right.count() ; }

//End of comparisons

} //end namespace

#endif /* FREQUENCY_H_ */

//

// wordscollection.cpp

//

// Created on: Oct 26, 2016

// Author: S. Miller

//

// Constructor

// wordscollection( std::string& filename )

// Precondition: Filename is a local file that EXISTS and is readable, file should be a text file

// Postcondition: File is read into the objects queue for later processing

//

// bool tokenizeFile( std::string& filename )

// Precondition: Filename is a local file that EXISTS and is readable, file should be a text file

// Postcondition: File is read into the objects queue for later processing

//

// bool tokenizeFile( char * filename )

// Convenience function if you wanted to pass a hardcoded filename string (cstr) to tokenize, otherwise same as above

// Precondition: Filename is a local file that EXISTS and is readable, file should be a text file

// Postcondition: File is read into the objects queue for later processing

//

// std::string getNextToken();

// Precondition: WORD Tokens are STILL available in the queue

// Postcondition: Next token is POPPED from QUEUE and returned

//

// bool hasTokens() const;

// Precondition: N/A

// Postcondition: True if the WORD token queue is NOT empty

//

#ifndef WORDSCOLLECTION_H_

#define WORDSCOLLECTION_H_

#include

#include

namespace FREQPROJ {

class wordscollection {

public:

wordscollection() {

//Empty queue

words = std::queue();

}

wordscollection( std::string& );

wordscollection( char * );

~wordscollection() {

//words queue will be destroyed by queue destructor

}

bool tokenizeFile( std::string& );

bool tokenizeFile( char * );

std::string getNextToken();

bool hasTokens() const;

private:

std::queue words;

};

} /* namespace FREQPROJ */

#endif /* WORDSCOLLECTION_H_ */

//

// wordscollection.cpp

//

// Created on: Oct 26, 2016

// Author: S. Miller

//

// Constructor

// wordscollection( std::string& filename )

// Precondition: Filename is a local file that EXISTS and is readable, file should be a text file

// Postcondition: File is read into the objects queue for later processing

//

// bool tokenizeFile( std::string& filename )

// Precondition: Filename is a local file that EXISTS and is readable, file should be a text file

// Postcondition: File is read into the objects queue for later processing

//

// bool tokenizeFile( char * filename )

// Convenience function if you wanted to pass a hardcoded filename string (cstr) to tokenize, otherwise same as above

// Precondition: Filename is a local file that EXISTS and is readable, file should be a text file

// Postcondition: File is read into the objects queue for later processing

//

// std::string getNextToken();

// Precondition: WORD Tokens are STILL available in the queue

// Postcondition: Next token is POPPED from QUEUE and returned

//

// bool hasTokens() const;

// Precondition: N/A

// Postcondition: True if the WORD token queue is NOT empty

//

#include

#include

#include "wordscollection.h"

namespace FREQPROJ {

wordscollection::wordscollection( std::string& f) {

tokenizeFile(f);

}

wordscollection::wordscollection( char * f) {

std::string s(f);

tokenizeFile( s );

}

bool wordscollection::tokenizeFile(std::string& filename) {

std::string s,sWord;

std::ifstream fin;

fin.open( filename.c_str(), std::ifstream::in);

if ( !fin.fail() ) {

while ( fin ) {

s = "";

while ( fin && !isalpha(fin.peek()) )

fin.ignore();

if ( fin && isalpha(fin.peek()) ) {

//fin.ignore();

//Possible HTML Tagname, tagnames end in >, SPACE, [, or newline (carriage return too)

while ( fin && isalpha(fin.peek()) )

s += tolower(fin.get());

//Got word

words.push(s);

}

}

} else {

throw std::runtime_error("Unable to open/read file!");

}

fin.close();

return true;

}

bool wordscollection::tokenizeFile( char * filename ) {

std::string s(filename);

return tokenizeFile( s );

}

std::string wordscollection::getNextToken() {

if ( hasTokens() ) {

std::string s = words.front();

words.pop();

return s;

} else {

throw std::runtime_error("Tokens underflow!");

}

return ""; //To appease the compiler

}

bool wordscollection::hasTokens() const {

return !words.empty();

}

} /* namespace FREQPROJ */

this is what i have now if it helps

#include

#include

#include "bintree.h"

#include "wordscollection.h"

#include "frequency.h"

using namespace FREQPROJ;

using namespace std;

void insertNode(binary_tree_node> &bst, wordscollection &words, size_t &t);

int main(int argc, char * argv[])

{

size_t treeDepth = 0;

for ( size_t i = 1; i

{

//wordscollection(filename);

wordscollection words(argv[i]);

cout << argv[i];

binary_tree_node > bst;

//string root = words.getNextToken();

while(words.hasTokens()){

//create new node

string temp = words.getNextToken();

//insertNode( bst, words, treeDepth);

}

}

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Domain Transfer Learning With 3q Data Processing

Authors: Ahmed Atif Hussain

1st Edition

B0CQS1NSHF, 979-8869061805

More Books

Students also viewed these Databases questions

Question

Define Administration and Management

Answered: 1 week ago