#include <iostream>
#include <sstream>
#include <fstream>
#include <map>
usingnamespace std;
int main()
{
// For convenience
typedef std::map<std::string, int> FreqMap;
std::string word; // the input word
FreqMap wf; // the frequencies of each word
// Open some text file
std::ifstream infile("2.txt");
// Read all words from the file
while (infile >> word)
{
// See if the key/value pair is already
// in the map
FreqMap::iterator it = wf.find(word);
// If it is present, increment the count (value)
if (it != wf.end())
it->second++; // Same as: (*it).second++
else
{
// Create a new pair with value set to 1
std::pair<std::string, int> pr(word, 1);
wf.insert(pr);
}
}
// Print out all of the key/value pairs
for (FreqMap::iterator it = wf.begin(); it != wf.end(); ++it)
std::cout << it->second << " " << it->first << std::endl;
}
1.txt
"And that's because the population has increasingly become dominated
by older and larger animals. This is a result of a decline in the
number of krill entering the population - what we call juvenile
recruitment."
okay now, if i read the file it will be like this
" - no of occurence
And
This
a
animals.
because
become
by
decline
dominated
call
entering
the
that's
has
increasingly
in
is
juvenile
krill
larger
result
number
older
of
population
-
what
we
recruitment.
"
as you can see, theres a character " ". and also in (that's) has some ' but now i want it to be removed. also i want to removed the word that's bcs there is some character in there
the output will be in ascending order alphabetically, the number of occurence will be next to the word, if there is double word, it will output only one and show the no of occurence
#include <iostream>
#include <sstream>
#include <fstream>
#include <map>
usingnamespace std;
int main()
{
const string path = "2.txt";
ifstream input( path );
if ( !input )
{
cerr << "Error opening file.\n";
return 0;
}
multimap< string, int > words;
char buf[ 255 ];
for ( int line = 1; input.getline( buf, sizeof buf ); line++ )
{
for ( char *p = buf; *p; ++p )
if ( !isalpha( *p ) )
*p = ' ';
string word;
for ( istringstream iss( buf ); iss >> word; )
words.insert( make_pair( word, line ) );
}
for ( auto it1 = words.begin(); it1 != words.end(); )
{
auto it2 = words.upper_bound( it1->first );
cout << it1->first << " : ";
for ( ; it1 != it2; it1++ )
cout << it1->second << ' ';
cout << '\n';
}
}
I tried this code but this will eliminate all character and number which i only want to eliminate character not number, also, it shows which the line for each word which i dont want, i want it only to count the word exist not showing the line in where the word exist
side note : the text file is too long so i only post little bit of it
while (infile >> word)
{
if (word == "-") continue;
word.erase(std::remove(word.begin(), word.end(), '\"'), word.end()); // <--- Removes the quotes.
word.erase(std::remove(word.begin(), word.end(), '.'), word.end()); // <--- Removes the period.
word.erase(std::remove(word.begin(), word.end(), '\''), word.end()); // <--- Removes the apostrophe.
// See if the key/value pair is already
// in the map
FreqMap::iterator it = wf.find(word);
After that the rest of the program works.
Someone more familiar with "regex" may be able to do all of this with one or two lines of code.
Sorry in my haste I forgot to mention to include the "algorithm" header file.
A thought I have is that the line of code may not work under the C++11 standards because it was not available until the C++14 standards and the reason it worked for me is that My IDE and compiler are set for the C++14 standards.
It may not be as neat and compact, but I will see what I can come up with that will work in the C++11 standards.
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <cctype>
#include <map>
usingnamespace std;
string filter( string input )
{
string output;
for ( char c : input ) if ( isalnum( c ) ) output += tolower( c );
return output;
}
int main()
{
map<string,int> freq;
// ifstream in( "input.txt" );
stringstream in( "We hold these truths to be self-evident, that all men are created equal, ""that they are endowed by their Creator with certain unalienable Rights, ""that among these are Life, Liberty and the pursuit of Happiness." );
for ( string word; in >> word; )
{
word = filter( word );
if ( word.size() ) freq[word]++;
}
for ( auto p : freq ) cout << p.first << " " << p.second << '\n';
}
all 1
among 1
and 1
are 3
be 1
by 1
certain 1
created 1
creator 1
endowed 1
equal 1
happiness 1
hold 1
liberty 1
life 1
men 1
of 1
pursuit 1
rights 1
selfevident 1
that 3
the 1
their 1
these 2
they 1
to 1
truths 1
unalienable 1
we 1
with 1
bool remover(char c)
{ return (c=='.' || c=='\'' || c == '\"'); } //could be lambda but my compiler does not have
s.erase(std::remove_if(s.begin(), s.end(),remover),s.end());
but I think the filter version is what was really wanted.