Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <unordered_map>
- #include <string_view>
- #include <fstream>
- #include <list>
- using ui64 = uint64_t;
- class TFrequencyDictionary {
- public:
- void Add(const std::string& word);
- ui64 GetFrequency(const std::string& word) const;
- const std::unordered_map<std::string, ui64>& GetMap() const;
- friend std::ostream& operator<< (std::ostream& output, const TFrequencyDictionary& dict);
- friend std::istream& operator>> (std::istream& input, TFrequencyDictionary& dict);
- private:
- std::string TransformString(const std::string& word) const;
- private:
- std::unordered_map<std::string, ui64> Counter;
- };
- void TFrequencyDictionary::Add(const std::string& word) {
- Counter[TransformString(word)]++;
- }
- ui64 TFrequencyDictionary::GetFrequency(const std::string& word) const {
- const auto it = Counter.find(word);
- return it == Counter.end() ? 0 : it->second;
- }
- const std::unordered_map<std::string, ui64>& TFrequencyDictionary::GetMap() const {
- return Counter;
- }
- std::string TFrequencyDictionary::TransformString(const std::string& word) const {
- std::string buf;
- buf.reserve(word.length());
- buf.resize(word.length());
- std::transform(word.begin(), word.end(), buf.begin(), ::tolower);
- return buf;
- }
- std::istream& operator>> (std::istream& input, TFrequencyDictionary& dict) {
- std::string word;
- for (char c; input.get(c);) {
- if (isalpha(c)) {
- word += c;
- }
- else {
- if (!word.empty()) {
- dict.Add(word);
- word.clear();
- }
- }
- }
- return input;
- }
- std::ostream& operator<< (std::ostream& output, const TFrequencyDictionary& dict) {
- std::list<std::pair<ui64, std::string_view>> items;
- for (auto&& item : dict.GetMap()) {
- items.emplace_back(item.second, item.first);
- }
- items.sort();
- for (auto it = items.rbegin(); it != items.rend(); ++it) {
- ui64 frequency = it->first;
- std::string_view word = it->second;
- output << frequency << "\t" << word << std::endl;
- }
- return output;
- }
- int main(int argc, char* argv[]) {
- if (argc != 3) {
- std::cerr << "invalid number of arguments. Usage: ./freq input_file output_file" << std::endl;
- return 1;
- }
- const std::string inputFilename(argv[1]);
- const std::string outputFilename(argv[2]);
- TFrequencyDictionary dict;
- try {
- std::ifstream input(inputFilename);
- if (input.fail()) {
- std::cerr << "Cannot open input file " << std::endl;
- return 1;
- }
- input >> dict;
- } catch(std::exception& e) {
- std::cerr << "Cannot read from input file: " << e.what() << std::endl;
- return 1;
- }
- try {
- std::ofstream output(outputFilename);
- output << dict;
- } catch(std::exception& e) {
- std::cerr << "Cannot write to output file: " << e.what() << std::endl;
- return 1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement