word count

Thread Solved
Reply

Join Date: Jun 2008
Posts: 92
Reputation: JackDurden is an unknown quantity at this point 
Solved Threads: 0
JackDurden JackDurden is offline Offline
Junior Poster in Training

word count

 
0
  #1
Nov 25th, 2008
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.

  1. #include <string>
  2. #include <iostream>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7. struct mytree
  8. {
  9. string item;
  10. mytree* left;
  11. mytree* right;
  12. int frequency;
  13. };
  14.  
  15. class TreeofWords
  16. {
  17. private:
  18. mytree *start;
  19.  
  20. public:
  21. TreeofWords();
  22. void inserted(string aword);
  23. void print();
  24. int Frequency(string freakword);
  25. };
  26. void main()
  27. {
  28. ifstream file;
  29. string filename;
  30. string word;
  31. TreeofWords display;
  32.  
  33. cout<<"Enter file name.\n";
  34. cin>>filename;
  35.  
  36. file.open(filename.c_str());
  37.  
  38. while(!file)
  39. {
  40. cout << "Unable to open file. Enter a different name: ";
  41. file.clear();
  42. cin >> filename;
  43. file.open(filename.c_str());
  44. }
  45. while(file>>word)
  46. {
  47. cout<<display.Frequency(word);
  48. display.inserted(word);
  49. }
  50.  
  51. display.print();
  52.  
  53. }
  54.  
  55. TreeofWords::TreeofWords()
  56. {
  57. start=NULL;
  58. }
  59.  
  60. void insert(mytree*& start, string aword)
  61. {
  62. if(start == NULL)
  63. {
  64. start = new mytree;
  65. start->right = NULL;
  66. start->left = NULL;
  67. start->item = aword;
  68. }
  69. else if(aword < start->item)
  70. insert(start->left, aword);
  71. else
  72. insert(start->right, aword);
  73. }
  74.  
  75. void TreeofWords::inserted(string aword)
  76. {
  77. insert(start, aword);
  78. }
  79. void printme(mytree*& start)
  80. {
  81. if(start != NULL)
  82. {
  83. printme(start->left);
  84. cout<<start->item<<endl;
  85. printme(start->right);
  86. }
  87. }
  88.  
  89. void TreeofWords::print()
  90. {
  91. printme(start);
  92. }
  93. int searchFrequency(mytree *start, string freakword)
  94. {
  95. if(start == NULL)
  96. {
  97. return 0;
  98. }
  99. else if(start->item<freakword)
  100. {
  101. searchFrequency(start->left, freakword);
  102. }
  103. else if(start->item>freakword)
  104. {
  105. searchFrequency(start->right, freakword);
  106. }
  107. else(start->item==freakword);
  108. return 1;
  109.  
  110. }
  111. int TreeofWords::Frequency(string freakword)
  112. {
  113. return searchFrequency(start, freakword);
  114. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 320
Reputation: cikara21 is an unknown quantity at this point 
Solved Threads: 63
cikara21's Avatar
cikara21 cikara21 is offline Offline
Posting Whiz

Re: word count

 
0
  #2
Nov 25th, 2008
I'am sure there is an error..Not only failed to display the freakword count..
.:-cikara21-:.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,851
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: word count

 
0
  #3
Nov 25th, 2008
> 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?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,765
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 493
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: word count

 
0
  #4
Nov 25th, 2008
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.


  1. void insert(mytree*& start, string aword)
  2. {
  3. if(start == NULL)
  4. {
  5. start = new mytree;
  6. start->right = NULL;
  7. start->left = NULL;
  8. start->item = aword;
  9. }
  10. else if(aword < start->item)
  11. insert(start->left, aword);
  12. else
  13. insert(start->right, aword);
  14. }

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

  1. class node
  2. {
  3. // data members below
  4. node* right;
  5. node* left;
  6. string word;
  7. int frequency;
  8.  
  9. // one member function below
  10. insert (string aword)
  11. {
  12. // check whether word is the same as aword
  13. // if so, increment frequency, don't create new node.
  14. // if not, check whether to go right or left and if
  15. // child is null, create a new node. Otherwise,
  16. // recurse as you do.
  17. }
  18.  
  19. // more member functions/constructor
  20. };

  1. class tree
  2. {
  3. // data member
  4. node* top;
  5.  
  6. // constructor
  7. tree ()
  8. {
  9. top = NULL;
  10. }
  11.  
  12. void insert (string aword)
  13. {
  14. if (top == NULL)
  15. // create new top node with call to node constructor
  16. else
  17. top->insert (aword);
  18. }
  19. };
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 92
Reputation: JackDurden is an unknown quantity at this point 
Solved Threads: 0
JackDurden JackDurden is offline Offline
Junior Poster in Training

Re: word count

 
0
  #5
Nov 25th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,765
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 493
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: word count

 
1
  #6
Nov 25th, 2008
Originally Posted by JackDurden View Post
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.
It ran for me too and it alphabetized, which surprised me. I didn't think the < operator would work. Learn something new every day.

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.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 320
Reputation: cikara21 is an unknown quantity at this point 
Solved Threads: 63
cikara21's Avatar
cikara21 cikara21 is offline Offline
Posting Whiz

Re: word count

 
0
  #7
Nov 25th, 2008
Just a sample..Easy way!!..
  1. // member of TreeofWords
  2. static int size=0;
  3. void TreeofWords::setsize()
  4. {
  5. size++;
  6. }
  7. int TreeofWords::getsize()
  8. {return size;}
  9.  
  10. void main()
  11. {
  12. // ...
  13. while(file>>word)
  14. {
  15. display.setsize();
  16. // ...
  17. }
  18. std::cout<<"freak word count : "<<display.getsize();
  19. // ...
  20. }// done
Last edited by cikara21; Nov 25th, 2008 at 2:39 pm.
.:-cikara21-:.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC