| | |
word count
Thread Solved
![]() |
•
•
Join Date: Jun 2008
Posts: 92
Reputation:
Solved Threads: 0
Im trying to count the frequency of words from a file and cant seem to get it to work. My frequency function recognizes the word is there but doesnt return a count for each word yet. The text file would just hold a bunch of random words...please help.
C++ Syntax (Toggle Plain Text)
#include <string> #include <iostream> #include <fstream> using namespace std; struct mytree { string item; mytree* left; mytree* right; int frequency; }; class TreeofWords { private: mytree *start; public: TreeofWords(); void inserted(string aword); void print(); int Frequency(string freakword); }; void main() { ifstream file; string filename; string word; TreeofWords display; cout<<"Enter file name.\n"; cin>>filename; file.open(filename.c_str()); while(!file) { cout << "Unable to open file. Enter a different name: "; file.clear(); cin >> filename; file.open(filename.c_str()); } while(file>>word) { cout<<display.Frequency(word); display.inserted(word); } display.print(); } TreeofWords::TreeofWords() { start=NULL; } void insert(mytree*& start, string aword) { if(start == NULL) { start = new mytree; start->right = NULL; start->left = NULL; start->item = aword; } else if(aword < start->item) insert(start->left, aword); else insert(start->right, aword); } void TreeofWords::inserted(string aword) { insert(start, aword); } void printme(mytree*& start) { if(start != NULL) { printme(start->left); cout<<start->item<<endl; printme(start->right); } } void TreeofWords::print() { printme(start); } int searchFrequency(mytree *start, string freakword) { if(start == NULL) { return 0; } else if(start->item<freakword) { searchFrequency(start->left, freakword); } else if(start->item>freakword) { searchFrequency(start->right, freakword); } else(start->item==freakword); return 1; } int TreeofWords::Frequency(string freakword) { return searchFrequency(start, freakword); }
> void main()
Has nobody mentioned that main returns in in your previous 60 messages?
Or did you just ignore them.
Where do you
- set frequency to zero?
- determine that the word is already in the tree, and thus increment the frequency
> else(start->item==freakword);
I'd be surprised if this even compiles.
You say you've run it, is this your actual code?
Has nobody mentioned that main returns in in your previous 60 messages?
Or did you just ignore them.
Where do you
- set frequency to zero?
- determine that the word is already in the tree, and thus increment the frequency
> else(start->item==freakword);
I'd be surprised if this even compiles.
You say you've run it, is this your actual code?
•
•
Join Date: Jan 2008
Posts: 3,765
Reputation:
Solved Threads: 493
You have too many nodes. You want one node per UNIQUE word, not one node per word.
I would rename your class to word or node. You should have one tree and several nodes. You don't want to name something "tree" if there is one "tree" per word, in my opinion. If you have something called tree, make sure there is only one of them.
Nowhere do you compare the value of item to aword to see if they are equal and doing something if they are. You should be adjusting frequency as you insert, not later. New words should have a frequency of 1. Words already in the tree should have their frequencies incremented.
Also, I don't think you can use < with strings. String isn't an ordinal type. Consider using compare if you are trying to alphabetize here:
http://www.cplusplus.com/reference/s...g/compare.html
I would rename your class to word or node. You should have one tree and several nodes. You don't want to name something "tree" if there is one "tree" per word, in my opinion. If you have something called tree, make sure there is only one of them.
C++ Syntax (Toggle Plain Text)
void insert(mytree*& start, string aword) { if(start == NULL) { start = new mytree; start->right = NULL; start->left = NULL; start->item = aword; } else if(aword < start->item) insert(start->left, aword); else insert(start->right, aword); }
Nowhere do you compare the value of item to aword to see if they are equal and doing something if they are. You should be adjusting frequency as you insert, not later. New words should have a frequency of 1. Words already in the tree should have their frequencies incremented.
Also, I don't think you can use < with strings. String isn't an ordinal type. Consider using compare if you are trying to alphabetize here:
http://www.cplusplus.com/reference/s...g/compare.html
C++ Syntax (Toggle Plain Text)
class node { // data members below node* right; node* left; string word; int frequency; // one member function below insert (string aword) { // check whether word is the same as aword // if so, increment frequency, don't create new node. // if not, check whether to go right or left and if // child is null, create a new node. Otherwise, // recurse as you do. } // more member functions/constructor };
C++ Syntax (Toggle Plain Text)
class tree { // data member node* top; // constructor tree () { top = NULL; } void insert (string aword) { if (top == NULL) // create new top node with call to node constructor else top->insert (aword); } };
•
•
Join Date: Jan 2008
Posts: 3,765
Reputation:
Solved Threads: 493
•
•
•
•
yes this does run, and setting frequency to zero and determining if a word is already in the tree then incrementing it, is what I need help on.
The naming of the struct mytree threw me. You can keep the single class if you like and call it TreeOfWords as you have it. There's no need to have a node class with an insert method. You can keep the insert method in TreeOfWords like you have it. Keeping node as a struct with no methods will work. But rename it.
I'd get rid of the frequency methods. Handle adjusting frequency in insert. Again, don't create a new word if the word already exists in the tree. Just increment frequency in that case.
edit: I had to change void main to int main to get it to run. Salem is right. Change it even if your compiler lets you get away with it.
Just a sample..Easy way!!..
c++ Syntax (Toggle Plain Text)
// member of TreeofWords static int size=0; void TreeofWords::setsize() { size++; } int TreeofWords::getsize() {return size;} void main() { // ... while(file>>word) { display.setsize(); // ... } std::cout<<"freak word count : "<<display.getsize(); // ... }// done
Last edited by cikara21; Nov 25th, 2008 at 2:39 pm.
![]() |
Similar Threads
- word count (C++)
- Word count help. (C++)
- Can you please help me write this word count program in another way (Python)
- I Need Help Writing A Word Count Program In My Python (Python)
- Need Help, basic word count, string manipulation (VB.NET)
- word count in borland c++ ?? (C++)
- word count (Java)
- I can't implement a word count into my text editor (JAVA) (Java)
Other Threads in the C++ Forum
- Previous Thread: Detect a specific frequency tone
- Next Thread: how to convert char to int
| Thread Tools | Search this Thread |
api array based binary bitmap business c++ c/c++ char class classes code coding commentinghelp compile console conversion count decide delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez graph guess gui homeworkhelp homeworkhelper iamthwee ifpug ifstream incrementoperators infinite input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem proficiency program programming project python random read recursion reference rpg string strings temperature template templates test text text-file tree url variable vector video win32 windows winsock word wordfrequency wxwidgets






