Count the occurrences of "ALL" matching words in an user input sentence?

Feb 23, 2011 at 2:49am
Hello everybody,

Sorry to be back so soon with problems but this has totally worn me out. I worked to build over a 100 of test files from arrays to vector and I keep over 35 windows open for search on my Opera Web browsers that full of google C++ pages for days and I can't take another minute. By now I should be an expert.

How do I make this file count the number of occurrences of EACH word that match a list of words and numbers that is in a text file or a string array (which would be a bonus) when the users input of more than a single word. That what I need!

My setup will only accept one word and that was not the plan :(

I ask Google for Each Word and 100's of other sequences ... for example, I get back 500 links for how to do it for each letter in a string ... but I still check them out anyway... now I'm half blind and brain dead all for at best, 4-8 line of code to be replaced in this program. Sorry, but it been my worse few days ever! IMO asm is a piece of cake compared to C++

Thanks in advance for any help


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
//template <class Key, class T, class Compare = less<Key  >,

//class Allocator = allocator < pair< const Key,T >  > >  class multimap;

// map<Key,T>::iterator it;
// (*it).first;             // the key value (of type Key)
// (*it).second;            // the mapped value (of type T)
// (*it);                   // the "element value" (of type pair<const Key,T>) 

// it->first;               // same as (*it).first   (the key value)
// it->second;              // same as (*it).second  (the mapped value) 


// Member types
// of template <class Key, class T, class Compare=less<Key>, 
//                  class Allocator=allocator<pair <const Key, T> > > class map;


// key_type         Key
// mapped_type      T
// value_type       pair<const Key,T>
// key_compare      Compare
// value_compare Nested class to compare elements (see member function value_comp)
// allocator_type   Allocator
// reference        Allocator::reference
// const_reference  Allocator::const_reference
// iterator         Bidirectional iterator
// const_iterator   Constant bidirectional iterator
// size_type        Unsigned integral type (usually same as size_t)
// difference_type  Signed integral type (usually same as ptrdiff_t)
// pointer          Allocator::pointer
// const_pointer    Allocator::const_pointer
// reverse_iterator reverse_iterator<iterator>
// const_reverse_iterator             reverse_iterator<const_iterator>


#include<map>
#include<iostream>
#include<string>
#include<fstream>
#include<sstream>
using std::istringstream;
using std::ifstream;
using std::string;
using std::cout;
using std::cin;
using std::cerr;
using std::map;
using std::multimap;

char wordMatch_1 [20];
char wordMatch_2 [20];
char wordMatch_3 [20];
char wordMatch_4 [20];
char wordMatch_5 [20];

int  relatedNUM_1;
int  relatedNUM_2;
int  relatedNUM_3;
int  relatedNUM_4;
int  relatedNUM_5;

int main()
{
	multimap <string,int> words;
	map      <int,string> v_VALUE;
	string str;

	ifstream input("yes.txt");
	if(input.fail())
	{
		cerr<<"\nThe file could not be opened.";
		return -1;
	}



	int i=1;
	while(getline(input,str))
	{
		istringstream in(str);
		string s;
		while(in>>s)
		{
			words.insert(make_pair(s,i));
		}
		v_VALUE.insert(make_pair(i,str));
		i++;
	}


	string search;
	cout<<"\nI hope this program to say * Please enter a sentence, we'll search each word:\n\n ";
	cout<<"\nPlease enter (A) word for my weak program to search: ";
	cin>>search;
	cout<<"\nNUMBER OF MATCHES = " << words.count(search) <<'\n';
	multimap<string,int>::iterator it1 = words.lower_bound(search);
	multimap<string,int>::iterator it2 = words.upper_bound(search);


	while(it1!=it2)
	{
		int x = it1->second;

		map<int,string>::iterator iter = v_VALUE.find(x);
		cout << '\n' << x << " ) " << iter->second << '\n';
		it1++;

		while(true)
		{
           //  can do something else maybe here

		//	if(it1!=it2 && it1->second==x)
			{
		//		++it1;
//		cout << "at the end" << '\n';				
			}
//			else
			{
				break;
			}

		}
	}

cout << "at the end" << '\n';
system("pause");
system ("CLS");
return main();

}



yes.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
yes
no
yes
no
nope YES nope YES
a
b
abcd
000
000
000
111
111
222
222
Last edited on Feb 23, 2011 at 2:50am
Feb 23, 2011 at 5:44am
Sorry I didn't understand what it is you wanted - entirely - I did get the main point, I hope:

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
#include <iostream>
#include <string>
#include <vector>
#include <fstream>
#include <sstream>
using namespace std;


int main()
{
	vector<string> words;
	vector<string> foundWords;
	string str;

	ifstream input("yes.txt");
	if(input.fail())
	{
		cerr<<"\nThe file could not be opened.";
		return -1;
	}

	while(getline(input,str))
	{
		istringstream in(str);
		string s;
		while(in>>s)
		{
			words.push_back(s);
		}
	}


	string search;
	cout<<"\nI hope this program to say * Please enter a sentence, we'll search each word:\n\n ";
	cout<<"\nPlease enter sentence for my weak program to search: ";
	getline(cin, search);
	cout<<"\nNUMBER OF MATCHES = ";
	istringstream osl(search);
	int count = 0;
	while(osl)
	{
		string substr;
		osl >> substr;
		for(int i = 0; i < words.size(); i++)
		{
			if(substr == words[i])
			{
				count ++;
				foundWords.push_back(words[i]);
				break;
			}
		}
	}
	cout << count << "\n";
	cout << "These are the words you matched with: " << "\n";
	if(foundWords.size() > 0)
	{
		for(int i = 0; i < foundWords.size(); i++)
		{
			cout << foundWords[i] << "\n";
		}
	}
	cin.get();
}


Sorry - I provided a solution that is completely different to yours - no maps just vectors, they are dynamic arrays, meaning you can change their size at run time, Google is your friend when you know what to search for.
Feb 23, 2011 at 10:37am
ooooohhhh WoW joro550, Thank you so much. I when through pure h*ll and came up with zip. You just wrote the only one on C++ Planet Goggle that really WORK and make since!!! If there are a few out there, I can assure you it's buried so deep satan himself can't find it.

Thanks again joro550

I also need to say THANKS to krishnendu right here because I don't want to disturb that thread until I go to work with the code he posted. I been checking out Pelle's C for the job so I can inline with FASM. After what I read about Vector, multimap, and iterator in the past four days, 24/7, I'm hook. That is some amazing work regardless of those few flaws people speak of. Anyway, I was one nap away from giving up. These two solution is a God sent. Thanks you both.

http://www.cplusplus.com/forum/general/36842/
Feb 24, 2011 at 2:19am
There is one other thing I need to know. How would I insert a loop or something that will also show the total occurrence of each word or number that the user input.

I been trying to add (if) statements to do the looping but I ran out of variables to use. I'm now under the impression that I need to add some of the iterator code I posted. Once I see how this is done I think I can handle anything afterward.
Feb 24, 2011 at 8:26am
Nevermind .. From all I tried today it seems that this is going to take some major surgery or for the C++ creators to include it in the next standards.
Topic archived. No new replies allowed.