Advertisement
bogolyubskiyalexey

Frequency words dictionary

Jul 3rd, 2019
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.85 KB | None | 0 0
  1. #include <iostream>
  2. #include <unordered_map>
  3. #include <string_view>
  4. #include <fstream>
  5. #include <list>
  6.  
  7. using ui64 = uint64_t;
  8.  
  9. class TFrequencyDictionary {
  10. public:
  11.     void Add(const std::string& word);
  12.  
  13.     ui64 GetFrequency(const std::string& word) const;
  14.     const std::unordered_map<std::string, ui64>& GetMap() const;
  15.  
  16.     friend std::ostream& operator<< (std::ostream& output, const TFrequencyDictionary& dict);
  17.     friend std::istream& operator>> (std::istream& input, TFrequencyDictionary& dict);
  18.  
  19. private:
  20.     std::string TransformString(const std::string& word) const;
  21.  
  22. private:
  23.     std::unordered_map<std::string, ui64> Counter;
  24. };
  25.  
  26. void TFrequencyDictionary::Add(const std::string& word) {
  27.     Counter[TransformString(word)]++;
  28. }
  29.  
  30. ui64 TFrequencyDictionary::GetFrequency(const std::string& word) const {
  31.     const auto it = Counter.find(word);
  32.     return it == Counter.end() ? 0 : it->second;
  33. }
  34.  
  35. const std::unordered_map<std::string, ui64>& TFrequencyDictionary::GetMap() const {
  36.     return Counter;
  37. }
  38.  
  39. std::string TFrequencyDictionary::TransformString(const std::string& word) const {
  40.     std::string buf;
  41.     buf.reserve(word.length());
  42.     buf.resize(word.length());
  43.     std::transform(word.begin(), word.end(), buf.begin(), ::tolower);
  44.     return buf;
  45. }
  46.  
  47. std::istream& operator>> (std::istream& input, TFrequencyDictionary& dict) {
  48.     std::string word;
  49.     for (char c; input.get(c);) {
  50.         if (isalpha(c)) {
  51.             word += c;
  52.         }
  53.         else {
  54.             if (!word.empty()) {
  55.                 dict.Add(word);
  56.                 word.clear();
  57.             }
  58.         }
  59.     }
  60.     return input;
  61. }
  62.  
  63. std::ostream& operator<< (std::ostream& output,  const TFrequencyDictionary& dict) {
  64.     std::list<std::pair<ui64, std::string_view>> items;
  65.     for (auto&& item : dict.GetMap()) {
  66.         items.emplace_back(item.second, item.first);
  67.     }
  68.     items.sort();
  69.     for (auto it = items.rbegin(); it != items.rend(); ++it) {
  70.         ui64 frequency = it->first;
  71.         std::string_view word = it->second;
  72.         output << frequency << "\t" << word << std::endl;
  73.     }
  74.     return output;
  75. }
  76.  
  77. int main(int argc, char* argv[]) {
  78.     if (argc != 3) {
  79.         std::cerr << "invalid number of arguments. Usage: ./freq input_file output_file" << std::endl;
  80.         return 1;
  81.     }
  82.     const std::string inputFilename(argv[1]);
  83.     const std::string outputFilename(argv[2]);
  84.  
  85.     TFrequencyDictionary dict;
  86.     try {
  87.         std::ifstream input(inputFilename);
  88.         if (input.fail()) {
  89.             std::cerr << "Cannot open input file " << std::endl;
  90.             return 1;
  91.         }
  92.         input >> dict;
  93.     } catch(std::exception& e) {
  94.         std::cerr << "Cannot read from input file: " <<  e.what() << std::endl;
  95.         return 1;
  96.     }
  97.  
  98.     try {
  99.         std::ofstream output(outputFilename);
  100.         output << dict;
  101.     } catch(std::exception& e) {
  102.         std::cerr << "Cannot write to output file: " <<  e.what() << std::endl;
  103.         return 1;
  104.     }
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement