collection not returning anything

Hello, I worked my code to read from a file and extract "<BODY>" and "</BODY>" tags but I intended to add "cout" to "std::string collection" just to see if it returns anything and it is not returning anything, can't seem to know why, if I run the function on it's own without adding it to a class it works fine, here is not returning anything. Help is appreciated :D

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
  #include <iostream>
#include <string>
#include <fstream>

using namespace std; 

struct TrieNode
{
	bool isEnd; 
	int HeapIndex; 
	TrieNode* child[26]; 
	int frequency; 
	TrieNode()
	{
		HeapIndex = -1; 
		isEnd = false; 
		frequency = 0; 
		for (int i = 0; i < 26; i++)
		{
			child[i] = NULL; 
		}
	}
};

struct HeapNode
{
	string word; 
	int frequency; 
	TrieNode* TN; 
	HeapNode()
	{
		frequency = 0; 
		TN = nullptr; 
		word = ""; 
	}
};

class Trie
{
private: 
	TrieNode* root; 
public: 
	Trie(); 
	
	TrieNode* insert(string word); 
	TrieNode* search(string word); 
};

Trie::Trie()
{
	root = new TrieNode(); 
}

TrieNode* Trie::insert(string word)
{
	TrieNode* temp = root; 
	for (unsigned int i = 0; i < word.length(); i++)
	{
		if (temp->child[word[i] - 'a'] == 0)
		{
			if (temp->child[word[i] - 'a'] == NULL)
			{
				TrieNode* node = new TrieNode();
				temp->child[word[i] - 'a'] = node;
			}
			temp = temp->child[word[i] - 'a'];
		}
	}
	temp->frequency = 1; 
	temp->isEnd = 1; 
	return temp; 
}

TrieNode* Trie::search(string word)
{
	TrieNode* temp = root; 
	for (unsigned int i = 0; i < word.length(); i++)
	{
		if (temp->child[word[i] - 'a'] == NULL)
		{
			return NULL; 
			temp = temp->child[word[i] - 'a']; 
		}
	}
	if (temp->isEnd)
	{
		return temp; 
	}
	else
	{
		return NULL; 
	}
}

class MinHeap
{
private: 
	int capacity, count; 
	HeapNode* array; 
public: 
	MinHeap() = default; 
	MinHeap(int capacity); 
	void Display(); 
	void minHeapify(int index); 
	void Build(int index); 
	void insert(TrieNode* TN, string word); 
	void TopKFrequentWord(string fileName, int k);
	string ExtractBodyNoTags(const string& text, string tag);
	void stripTags(string& text); 

	
};


MinHeap::MinHeap(int capacity)
{
	this->capacity = capacity; 
	count = 0; 
	array = new HeapNode[capacity]; 
}

void MinHeap::Display()
{
	for (int i = 0; i < capacity; i++)
	{
		cout << array[i].word << "  " << array[i].frequency << endl; 
	}
}

void MinHeap::minHeapify(int index)
{
	int left = 2 * index + 1; 
	int right = 2 * index + 2; 
	int minIndex = index; 
	if (left < count && array[left].frequency < array[minIndex].frequency)
	{
		minIndex = left; 
	}
	else if (right < count && array[right].frequency < array[minIndex].frequency)
	{
		minIndex = right; 
	}
	else if (minIndex != index)
	{
		array[index].TN->HeapIndex = minIndex; 
		array[minIndex].TN->HeapIndex = index;
		swap(array[index], array[minIndex]); 
		minHeapify(minIndex); 
	}
}

void MinHeap::Build(int index)
{
	int n = count - 1; 
	for (int i = (n - 1) / 2; i >= 0; i--)
	{
		minHeapify(i); 
	}
}

void MinHeap::insert(TrieNode* TN, string word)
{
	if (TN->HeapIndex != -1)
	{
		array[TN->HeapIndex].frequency++; 
		minHeapify(TN->HeapIndex); 
	}
	else if (count < capacity)
	{
		array[count].word = word; 
		array[count].frequency = 1;
		array[count].TN = TN; 
		TN->HeapIndex = count; 
		count++; 
		Build(count); 
	}
	else if (TN->frequency > array[0].frequency)
	{
		array[0].TN->HeapIndex = -1; 
		array[0].word = word; 
		array[0].frequency = TN->frequency; 
		array[0].TN = TN; 
		TN->HeapIndex = 0; 
		minHeapify(0); 
	}
}

void MinHeap::TopKFrequentWord(string fileName, int k)
{
	
	MinHeap mh(k);
	Trie T;
	string word;

	TrieNode* TN = T.search(word);
	if (!TN)
	{
		TN = T.insert(word);
	}
	else
	{
		TN->frequency++;
	}
	mh.insert(TN, word);

	mh.Display();
}




string MinHeap::ExtractBodyNoTags(const string& text, string tag)
{
	string collection;
	
	unsigned int pos = 0, start;

	while (true)
	{
		start = text.find("<" + tag, pos);
		if (start == string::npos) return collection;
		start = text.find(">", start);
		start++;

		pos = text.find("</" + tag, start);
		if (pos == string::npos) return collection;
		collection = text.substr(start, pos - start);
		cout << collection; 
	}
}

void MinHeap::stripTags(string& text)
{
	string body;
	unsigned int start = 0, pos;
	while (start < text.size())
	{
		start = text.find("<", start);
		if (start == string::npos)
		{
			break;
		}
		pos = text.find(">", start);
		if (pos == string::npos)
		{
			break;
		}
		
		body = text.substr(pos, start);
		
	}
}


int main()
{
	int k = 10; 
	string file;
	MinHeap mh(k);
	Trie T;
	string word;
	MinHeap foo; 
	string tag = "BODY"; 
	
	string stopword;
	ifstream in("C:\\Users\\Kareem's Laptop\\Desktop\\Reuters-21578\\reut2-000.sgm");
	if (!in.is_open())
	{
		cerr << "FILE NOT FOUND!" << endl; 
		exit(1); 
	}
	if (in.is_open())
	{
		while (!in.eof())
		{
			while (in >> word)
			{

				//foo.ExtractBodyNoTags(word, tag);

				TrieNode* TN = T.search(word);

				if (!TN)
				{
					TN = T.insert(word);
				}
				else
				{
					TN->frequency++;
				}
				mh.insert(TN, word);
				
			}
			mh.Display();
		}
		in.close();
	}
}




Last edited on
Topic archived. No new replies allowed.