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
|
#include <iostream>
#include <string>
#include <cctype>
#include <map>
#include <sstream>
#include <iomanip>
// remove non-alphanumeric characters, convert to all lower case
std::string clean( const std::string& str )
{
std::string clean_str ;
for( char c : str ) if( std::isalnum(c) ) clean_str += std::tolower(c) ;
return clean_str ;
}
// holds the count of occurrences of each token
using token_map = std::map< std::string, int > ;
token_map tokenise( const std::string& str )
{
token_map map ;
// extract white space delimited tokens, add/update map
std::istringstream stm(str) ; // construct a string stream to read the characters in str
std::string token ;
while( stm >> token ) ++map[ clean(token) ] ; // add/update cleaned token in the map
return map ;
}
bool contains( const token_map& tok_set, const token_map& tok_subset )
{
for( const auto& pair : tok_subset )
{
if( !pair.first.empty() ) // ignore tokens with no alphanumeric characters
{
const auto iter = tok_set.find(pair.first) ;
// iter == tok_set.end(): token was not found
// iter->second < pair.second: token was found, but number of occurrences were inadequate
if( iter == tok_set.end() || iter->second < pair.second ) return false ;
}
}
return true ;
}
bool contains( const std::string& line, const std::string& phrase )
{ return contains( tokenise(line), tokenise(phrase) ) ; }
void test( const std::string& a, const std::string& b )
{
std::cout << std::quoted(a) << " contains " << std::quoted(b) << " ? "
<< std::boolalpha << contains(a,b) << '\n' ;
}
int main()
{
const std::string a = "(computer systems and Architecture etc)" ;
const std::string b = "(COMPUTER architecture!)" ;
test(a,b) ; // true
test( "to be, or not to be, that is the question:", "question ? BE : not BE" ) ; // true
test( "TOBE, or NOTTOBE, that is the question:", "question ? to be : not to be" ) ; // false
test( "TOBE, or NOTTOBE, that is the question:", "question ? to-be : not-to-be" ) ; // true
test( "to be, or not to be, that is the question:", "question ? be be : be not be" ) ; // false
}
|