Skip to content

Instantly share code, notes, and snippets.

@gibizer
Created December 18, 2019 11:52
Show Gist options
  • Save gibizer/0d8d48ead33d389a076348f6e89abdbd to your computer and use it in GitHub Desktop.
Save gibizer/0d8d48ead33d389a076348f6e89abdbd to your computer and use it in GitHub Desktop.
#include <iostream>
#include <fstream>
#include <sstream>
#include <regex>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
bool comparator (pair<string, int> p1, pair<string, int> p2) {
return p1.second > p2.second;
}
int main () {
ifstream f;
f.open ("sherlock.txt");
stringstream buffer;
buffer << f.rdbuf();
f.close();
string s = buffer.str();
std::regex word("[^A-Za-z]+");
std::sregex_token_iterator iter(
s.begin(), s.end(), word, -1);
std::sregex_token_iterator end;
std::map<std::string, int> counter;
for ( ; iter != end; ++iter) {
string word = *iter;
transform(
word.begin(), word.end(), word.begin(),
[](unsigned char c){ return tolower(c); });
counter[word] += 1;
}
vector<pair<string, int>> pairs;
for (auto pair : counter) {
pairs.push_back(pair);
}
sort(pairs.begin(), pairs.end(), comparator);
for (auto pair: vector<pair<string, int>>(pairs.begin(), pairs.begin() + 20)) {
cout << pair.first << " " << pair.second << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment