Searching word for word

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2007
Posts: 20
Reputation: The Midnighter is an unknown quantity at this point 
Solved Threads: 0
The Midnighter The Midnighter is offline Offline
Newbie Poster

Searching word for word

 
0
  #1
Feb 19th, 2008
Hey there folks. I am trying to write a program that will take in a paragraph file, and compare it against a "Dictionary" text file, which is being inserted into a Binary Search Tree. Here's what I've got so far:

  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <iomanip>
  5. #include <cctype>
  6. #include <stack>
  7. using namespace std;
  8.  
  9. class Node;
  10. stack<string> theStack_;
  11. typedef Node * nodePtr_;
  12.  
  13. class Node
  14. {
  15. public:
  16. string data_;
  17. nodePtr_ left_;
  18. nodePtr_ right_;
  19. Node() : left_(NULL), right_(NULL), data_("") { }
  20. };
  21.  
  22.  
  23. class BST
  24. {
  25. private:
  26. nodePtr_ root_;
  27. public:
  28. BST() : root_(NULL) { }
  29.  
  30. void insert(string data)
  31. {
  32. insert(data, root_);
  33. }
  34. void insert(string data, nodePtr_ &node)
  35. {
  36. if (node == NULL)
  37. {
  38. node = new Node();
  39. node->data_ = data;
  40. }
  41. else if (data < node->data_)
  42. {
  43. // Traverse left sub tree
  44. insert (data, node->left_);
  45. }
  46. else if ( data > node->data_)
  47. {
  48. // Traverse right sub tree
  49. insert (data, node->right_);
  50. }
  51. else
  52. {
  53. // Node exists
  54. cout << "Node value: " << node->data_ << " already exists!" << endl;
  55. }
  56. }
  57.  
  58. bool spellCheck(string data)
  59. {
  60. bool broken = spellCheck(data, root_, 0);
  61. return broken;
  62. }
  63. bool spellCheck(string data, nodePtr_ &node, int indent)
  64. {
  65. if (node == NULL)
  66. {
  67. return false; // Empty
  68. }
  69. else if (data == node->data_)
  70. {
  71. return true;
  72. }
  73. else if (data < node->data_)
  74. {
  75. return true;
  76. }
  77. else
  78. {
  79. return spellCheck(data, node->right_, indent+8);
  80. }
  81. }
  82. // Recursive printing
  83. void printTree( ostream &output, nodePtr_ &node, int indent)
  84. {
  85. if (node != NULL)
  86. {
  87. printTree (output, node->right_, indent+8);
  88. output << setw(indent) << node->data_ << endl;
  89.  
  90. printTree (output, node->left_, indent+8);
  91. }
  92. }
  93. void removeNode (string data)
  94. {
  95. bool found_ = false;
  96. nodePtr_ node = root_;
  97. nodePtr_ parent_ = NULL;
  98.  
  99.  
  100. while (!found_ && (node != NULL))
  101. {
  102. if (data < node->data_)
  103. {
  104. // Go left
  105. parent_ = node;
  106. node = node->left_;
  107. }
  108. else if (data > node->data_)
  109. {
  110. // go right
  111. parent_ = node;
  112. node = node->right_;
  113. }
  114. else
  115. {
  116. // found!
  117. found_ = true;
  118. }
  119. }
  120.  
  121. if (!found_)
  122. {
  123. return;
  124. }
  125.  
  126. // find the successor
  127. if ((node->left_ != NULL) && (node->right_ != NULL))
  128. {
  129. // Goto left most node
  130. nodePtr_ successor = node->right_;
  131. parent_ = node;
  132. while (successor->left_ != NULL)
  133. {
  134. parent_ = successor;
  135. successor = successor->left_;
  136. }
  137. node->data_ = successor->data_;
  138. node = successor;
  139. }
  140.  
  141. // now we can delete the node with either one child or no child
  142. nodePtr_ subTree = node->left_;
  143.  
  144. if (subTree == NULL)
  145. {
  146. subTree = node->right_;
  147. }
  148. if (parent_ == NULL)
  149. {
  150. // delete root node
  151. root_ = subTree;
  152. }
  153. else if (parent_->left_ == node)
  154. {
  155. // delete a node with left subtree
  156. parent_->left_ = subTree;
  157. }
  158. else
  159. {
  160. // Delete a node with a right subtree
  161. parent_->right_ = subTree;
  162. }
  163. delete node;
  164. }
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171. // Trying to sort
  172. bool treeContains( nodePtr_ &node, string item, int indent)
  173. {
  174. if (node == NULL)
  175. {
  176. return false; // tree is empty
  177. }
  178. else if (item == node->data_)
  179. {
  180. return true;
  181. }
  182. else if (item < node->data_)
  183. {
  184. return true;
  185. }
  186. else
  187. {
  188. return treeContains( node->right_, item, indent+8);
  189. }
  190. }
  191. friend ostream& operator<<( ostream &output, BST &bst);
  192. };
  193. ostream& operator<< (ostream &output, BST &bst)
  194. {
  195. bst.printTree(output, bst.root_, 0);
  196. //bst.printTree(bst.root_);
  197. return output;
  198. }
  199.  
  200.  
  201.  
  202.  
  203.  
  204.  
  205.  
  206.  
  207.  
  208.  
  209. int main()
  210. {
  211. BST bst;
  212. string line;
  213. ifstream dictionaryFile("dictionary.txt");
  214. ifstream paragraphFile("paragraph.txt");
  215. int fileSize = 0; // Size of paragraph
  216. int dictLoaded = 0; // 0 - not loaded, 1 - loaded, 1< - done
  217. bool brokenWord = false;
  218. string search_for = " ";
  219.  
  220. if (dictLoaded == 0)
  221. {
  222. if (dictionaryFile.is_open())
  223. {
  224. while (!dictionaryFile.eof())
  225. {
  226. getline(dictionaryFile,line);
  227. bst.insert(line);
  228. }
  229. }
  230. dictionaryFile.close();
  231. dictLoaded = 1;
  232. }
  233.  
  234. if (dictLoaded == 1)
  235. {
  236. if (paragraphFile.is_open())
  237. {
  238. while (dictLoaded >> line)
  239. {
  240. if (line == search_word)
  241. {
  242. }
  243. //getline(paragraphFile,line);
  244. //brokenWord = bst.spellCheck(line);
  245. //if (brokenWord)
  246. //{
  247. // cout << "Bad word! ";
  248. //}
  249. //else
  250. //{
  251. // cout << "No bad words!";
  252. //}
  253. }
  254. }
  255. dictLoaded = 2;
  256. paragraphFile.close();
  257. }
  258. cout << bst << endl;
  259. return 0;
  260. }

As you can see, my BST is working just fine, loading the dictionary in and is able to show it. My problem lies here, I planned on searching through the paragraph file word for word, and comparing that word against the dictionary tree. I had the idea of doing this with a stack, putting the words into a stack, checking it, and tossing the word if it's okay. Other wise return the word to the client code, and display the error.

Around line 240 I'm starting to try my search, though I don't know the best way to find the word, right now I was just searching for spaces, and then... blank. =(

Thanks for all your help!
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,834
Reputation: niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute niek_e has a reputation beyond repute 
Solved Threads: 297
Moderator
Featured Poster
niek_e's Avatar
niek_e niek_e is offline Offline
Roasting Maven

Re: Searching word for word

 
0
  #2
Feb 19th, 2008
You could just read the file one word at a time with filestreams instead of using getline(), it would save you a lot of time.:

  1. std::ifstream in ( "myfile" );
  2.  
  3. if ( in )
  4. {
  5. std::string word;
  6. while ( in>> word )
  7. {
  8. spellCheck(word);
  9.  
  10. }
  11. }

If you want your line numbers visible, you should put your code between [code=cplusplus][/code] tags

Niek
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 20
Reputation: The Midnighter is an unknown quantity at this point 
Solved Threads: 0
The Midnighter The Midnighter is offline Offline
Newbie Poster

Re: Searching word for word

 
0
  #3
Feb 19th, 2008
Yeah... I figured that out two seconds before I received the email saying you replied.

Thanks bud, much <3
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 7
Reputation: chaosatom333 is an unknown quantity at this point 
Solved Threads: 0
chaosatom333 chaosatom333 is offline Offline
Newbie Poster

Re: Searching word for word

 
0
  #4
Mar 8th, 2008
how do make this work? You haven't declared search_word in main and haven't defined the operator for doing this
dictLoaded >> line
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