Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <iterator>
- #include <map>
- #include <cctype>
- #include <algorithm>
- #include <sstream>
- #include <vector>
- #include <set>
- using namespace std;
- const string content = "In Xanadu did Kubla Khan\n"
- "A stately pleasure-dome decree:\n"
- "Where Alph, the sacred river, ran\n"
- "Through caverns measureless to man\n"
- "Down to a sunless sea.\n"
- "\n"
- "So twice five miles of fertile ground\n"
- "With walls and towers were girdled round:\n"
- "And there were gardens bright with sinuous rills,\n"
- "Where blossomed many an incense-bearing tree;\n"
- "And here were forests ancient as the hills,\n"
- "Enfolding sunny spots of greenery.\n"
- "\n"
- "But oh! that deep romantic chasm which slanted\n"
- "Down the green hill athwart a cedarn cover!\n"
- "A savage place! as holy and enchanted\n"
- "As e'er beneath a waning moon was haunted\n"
- "By woman wailing for her demon-lover\n";
- vector<string> split(const string & content, char spliter){
- stringstream ss(content);
- string item;
- vector<string> res;
- while(getline(ss,item,spliter)){
- res.push_back(item);
- }
- return res;
- }
- string getWord(const string & str){
- string nstr = "";
- for(const char &c: str){
- if(!ispunct(c))
- nstr+=tolower(c);
- };
- return nstr;
- }
- int main()
- {
- float threshold = 0.028;
- map<string, set<int>> gquiz1;
- vector<string> words;
- vector<string> lines = split(content,'\n');
- int linenum = 1;
- int wordCount = 0;
- for (string line: lines){
- line.erase(remove(line.begin(), line.end(), '\r'), line.end());
- vector<string> _words = split(line,' ');
- for(string word:_words){
- wordCount++;
- word = getWord(word);
- if(word.size()>0){
- if(gquiz1.find(word) == gquiz1.end()){
- set<int> _set;
- _set.insert(linenum);
- gquiz1[word] = _set;
- }else{
- gquiz1[word].insert(linenum);
- }
- }
- }
- linenum++;
- }
- int num;
- map<string, set<int>>::iterator itr;
- for (itr = gquiz1.begin(); itr != gquiz1.end(); ++itr) {
- if (((float)(itr->second.size())/(float)wordCount)<threshold){
- cout << itr->first<<": ";
- set<int> _set = itr->second;
- for(auto it=_set.begin();it!=_set.end();it++){
- if (next(it)!=_set.end()){
- cout<<*it<<' ';
- }else{
- cout<<*it;
- }
- }
- cout<<endl;
- }
- }
- cout << endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement