Advertisement
Guest User

Untitled

a guest
Dec 14th, 2019
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.81 KB | None | 0 0
  1. #pragma once
  2.  
  3. #include <unordered_map>
  4. #include <fstream>
  5. #include <cmath>
  6. #include <sstream>
  7. #include <random>
  8. #include <algorithm>
  9.  
  10. std::mt19937 rng(6274674);
  11. std::uniform_int_distribution<std::mt19937::result_type> dist(0, 25);
  12.  
  13. using Map = std::unordered_map<std::string, double>;
  14.  
  15. inline Map ReadNgrams(const std::string& filename) {
  16.     std::fstream input_file(filename);
  17.     std::string line;
  18.     Map map;
  19.     map["1"] = 1.0;
  20.     map.clear();
  21.     while (getline(input_file, line)) {
  22.         std::stringstream line_stream;
  23.         line_stream << line;
  24.         std::string quadra;
  25.         line_stream >> quadra;
  26.         std::string count;
  27.         line_stream >> count;
  28.         double log_prob = log(strtol(count.c_str(), nullptr, 10));
  29.         map[quadra] = log_prob;
  30.     }
  31.     return map;
  32. }
  33.  
  34. inline char GetChar(char c, const std::vector<int>& permutation) {
  35.     if (!isalpha(c)) {
  36.         return c;
  37.     }
  38.     if (!isupper(c)) {
  39.         int ind = tolower(c) - 'a';
  40.         return char('a' + permutation[ind]);
  41.     } else {
  42.         int ind = c - 'A';
  43.         return char('A' + permutation[ind]);
  44.     }
  45. }
  46.  
  47. inline double GetScore(const std::string& text, const Map& map,
  48.                        const std::vector<int>& permutation) {
  49.     double res = 0.0;
  50.     for (size_t i = 0; i < text.size() - 3; ++i) {
  51.         std::string quad;
  52.         quad += GetChar(text[i], permutation);
  53.         quad += GetChar(text[i + 1], permutation);
  54.         quad += GetChar(text[i + 2], permutation);
  55.         quad += GetChar(text[i + 3], permutation);
  56.         if (map.find(quad) != map.end()) {
  57.             res += map.at(quad);
  58.         }
  59.     }
  60.     return res;
  61. }
  62.  
  63. inline std::string Recode(const std::string& text, const std::vector<int>& permutation) {
  64.     std::string res;
  65.     for (auto elem : text) {
  66.         res += GetChar(elem, permutation);
  67.     }
  68.     return res;
  69. }
  70.  
  71. inline std::string Decode(const std::string& text, const Map& dict) {
  72.     std::string new_text;
  73.     for (auto elem : text) {
  74.         if (isalpha(elem)) {
  75.             new_text += toupper(elem);
  76.         }
  77.     }
  78.     std::vector<int> permutation;
  79.     permutation.reserve(26);
  80.     for (int i = 0; i < 26; ++i) {
  81.         permutation.push_back(i);
  82.     }
  83.     int count = 0;
  84.     double score = GetScore(new_text, dict, permutation);
  85.     while (count <= 10'000) {
  86.        int i = 0, j = 0;
  87.        while (i == j) {
  88.            i = dist(rng);
  89.            j = dist(rng);
  90.        }
  91.        auto new_permutation = permutation;
  92.        std::swap(new_permutation[i], new_permutation[j]);
  93.        double new_score = GetScore(new_text, dict, new_permutation);
  94.        if (new_score > score) {
  95.            permutation = new_permutation;
  96.            score = new_score;
  97.        }
  98.        ++count;
  99.    }
  100.    return Recode(text, permutation);
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement