943,987 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1152
  • C++ RSS
Nov 25th, 2008
0

word count

Expand Post »
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)
  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. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
JackDurden is offline Offline
92 posts
since Jun 2008
Nov 25th, 2008
0

Re: word count

I'am sure there is an error..Not only failed to display the freakword count..
Reputation Points: 47
Solved Threads: 69
Posting Whiz
cikara21 is offline Offline
340 posts
since Jul 2008
Nov 25th, 2008
0

Re: word count

> 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?
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Nov 25th, 2008
0

Re: word count

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.


C++ Syntax (Toggle Plain Text)
  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

C++ Syntax (Toggle Plain Text)
  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. };

C++ Syntax (Toggle Plain Text)
  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. };
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Nov 25th, 2008
0

Re: word count

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.
Reputation Points: 10
Solved Threads: 0
Junior Poster in Training
JackDurden is offline Offline
92 posts
since Jun 2008
Nov 25th, 2008
1

Re: word count

Click to Expand / Collapse  Quote originally posted by JackDurden ...
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.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Nov 25th, 2008
0

Re: word count

Just a sample..Easy way!!..
c++ Syntax (Toggle Plain Text)
  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.
Reputation Points: 47
Solved Threads: 69
Posting Whiz
cikara21 is offline Offline
340 posts
since Jul 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Detect a specific frequency tone
Next Thread in C++ Forum Timeline: How to block unauthorized windows service stop?????





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC