Mike_be

Earliest date search with Suggesting file name and "automat"

Dec 11th, 2017
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.76 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <fstream>
  3. #include <iostream>
  4. #include <string>
  5. #include <vector>
  6. #include <algorithm>
  7. #include <regex>
  8. #include <streambuf>
  9. #include <filesystem>
  10.  
  11. int matrix[3][10] =
  12. { //S, A1, A2, A3, A4, A5, A6, A7, A8, A9
  13.     1, 2,  0,  4,  5,  0,  7,  8,  9,  10, //A for 0-9
  14.     0, 0,  3,  0,  0,  6,  0,  0,  0,  0,  //A for "-./"
  15.     0, 0,  0,  0,  0,  0,  0,  0,  0,  0,  //S for everything else
  16. };
  17.  
  18. int row(char& a)
  19. {
  20.     if (a >= '0' && a <= '9')
  21.         return 0;
  22.     if (a == '.' || a == '-' || a == '/')
  23.         return 1;
  24.     return 2;
  25. }
  26.  
  27. void auto_rifle(std::string& str, std::string& str_out)
  28. {
  29.     std::string date = "";
  30.     int state = 0;
  31.     for (unsigned long long k = 0; k < str.size(); k++)
  32.     {
  33.         if (state == 0)
  34.             date.clear();
  35.         state = matrix[(row(str[k]))][state];
  36.         date.push_back(str[k]);
  37.         if (state == 10)
  38.         {
  39.             str_out += date + " ";
  40.             state = 0;
  41.         }
  42.     }
  43.     return;
  44. }
  45.  
  46. template <typename T>
  47. typename T::size_type levenshtein_distance(const T & src, const T & dst)
  48. {
  49.     const typename T::size_type m = src.size();
  50.     const typename T::size_type n = dst.size();
  51.     if (m == 0)
  52.     {
  53.         return n;
  54.     }
  55.     if (n == 0)
  56.     {
  57.         return m;
  58.     }
  59.  
  60.     std::vector<std::vector<typename T::size_type>> matrix(m + 1);
  61.  
  62.     for (typename T::size_type i = 0; i <= m; ++i)
  63.     {
  64.         matrix[i].resize(n + 1);
  65.         matrix[i][0] = i;
  66.     }
  67.     for (typename T::size_type i = 0; i <= n; ++i)
  68.     {
  69.         matrix[0][i] = i;
  70.     }
  71.  
  72.     typename T::size_type above_cell, left_cell, diagonal_cell, cost;
  73.  
  74.     for (typename T::size_type i = 1; i <= m; ++i)
  75.     {
  76.         for (typename T::size_type j = 1; j <= n; ++j)
  77.         {
  78.             cost = src[i - 1] == dst[j - 1] ? 0 : 1;
  79.             above_cell = matrix[i - 1][j];
  80.             left_cell = matrix[i][j - 1];
  81.             diagonal_cell = matrix[i - 1][j - 1];
  82.             matrix[i][j] = std::min(std::min(above_cell + 1, left_cell + 1), diagonal_cell + cost);
  83.         }
  84.     }
  85.  
  86.     return matrix[m][n];
  87. }
  88.  
  89. #include <Windows.h>
  90.  
  91. using namespace std;
  92. using namespace experimental::filesystem;
  93.  
  94. HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
  95.  
  96. inline bool print_str(string str, int k, int n)
  97. {
  98.     SetConsoleTextAttribute(hConsole, k);
  99.     for (int l = 0; l < int(str.size()); l++)
  100.     {
  101.         cout << str[l];
  102.         Sleep(n);
  103.     }
  104.     SetConsoleTextAttribute(hConsole, 14);
  105.     return true;
  106. }
  107.  
  108. bool yes_no()
  109. {
  110.     bool err = false;
  111.     string temp;
  112.     getline(cin, temp);
  113.     do
  114.     {
  115.         err = false;
  116.         if (temp == "Yes" || temp == "yes" || temp == "y" || temp == "Y") return true; // Returning result for yes answer
  117.         else if (temp == "No" || temp == "no" || temp == "N" || temp == "n") return false; // For no answer
  118.         else
  119.         {
  120.             // If input is incorrect
  121.             print_str("Type [Yes] or [No]: ", 15, 15);
  122.             getline(cin, temp);
  123.             err = true;
  124.         }
  125.     }
  126.     while (err);
  127.     cout << "An error occurred!" << endl;
  128.     return true;
  129. }
  130.  
  131. bool yes_no(const string& str)
  132. {
  133.     bool err = false;
  134.     print_str(str, 15, 10);
  135.     string temp;
  136.     getline(cin, temp);
  137.     do
  138.     {
  139.         err = false;
  140.         if (temp == "Yes" || temp == "yes" || temp == "y" || temp == "Y") return true; // Returning result for yes answer
  141.         else if (temp == "No" || temp == "no" || temp == "N" || temp == "n") return false; // For no answer
  142.         else
  143.         {
  144.             // If input is incorrect
  145.             print_str("Type [Yes] or [No]: ", 15, 15);
  146.             getline(cin, temp);
  147.             err = true;
  148.         }
  149.     }
  150.     while (err);
  151.     cout << "An error occurred!" << endl;
  152.     return true;
  153. }
  154.  
  155. string make_path(string str)
  156. {
  157.     string extension = "";
  158.     unsigned int j;
  159.     if (str.length() >= 4)
  160.     {
  161.         j = str.length() - 4;
  162.         for (int i = 0; i < 4; i++)
  163.         {
  164.             extension.push_back(str[j]);
  165.             j++;
  166.         }
  167.     }
  168.     if (extension == ".txt")
  169.         str = "Books/" + str;
  170.     else
  171.         str = "Books/" + str + ".txt";
  172.     return str;
  173. }
  174.  
  175. void filling_strings(vector<string>& vec)
  176. {
  177.     unsigned int max_l = 0;
  178.     for (unsigned int i = 0; i < vec.size(); i++)
  179.     {
  180.         if (vec[i].length() > max_l)
  181.             max_l = vec[i].size();
  182.     }
  183.     for (unsigned int i = 0; i < vec.size(); i++)
  184.     {
  185.         while (vec[i].length() < max_l)
  186.             vec[i].push_back(' ');
  187.     }
  188. }
  189.  
  190. string shorting_string(string str)
  191. {
  192.     while (str[str.length() - 1] == ' ')
  193.         str.pop_back();
  194.     return str;
  195. }
  196.  
  197. string vec_search(const vector<string>& vec, const string& str)
  198. {
  199.     unsigned int i = 0, k = 0, dist = 100;
  200.     while (k < vec.size())
  201.     {
  202.         if (levenshtein_distance(vec[k], str) < dist)
  203.         {
  204.             dist = levenshtein_distance(vec[k], str);
  205.             i = k;
  206.         }
  207.         k++;
  208.     }
  209.     return vec[i];
  210. }
  211.  
  212. void regex_result(string& str, vector<string>& vec, const regex& rgx)
  213. {
  214.     regex_token_iterator<string::iterator> rend;
  215.     regex_token_iterator<string::iterator> a(str.begin(), str.end(), rgx);
  216.     while (a != rend) vec.push_back(*a++);
  217. }
  218.  
  219. bool empty(const string& str)
  220. {
  221.     bool empty = true;
  222.     if (!str.empty())
  223.     {
  224.         for (unsigned int i = 0; i < str.length(); i++)
  225.         {
  226.             if (str[i] != ' ')
  227.             {
  228.                 empty = false;
  229.             }
  230.         }
  231.     }
  232.     return empty;
  233. }
  234.  
  235. void files(vector<string>& vec)
  236. {
  237.     for (auto p : directory_iterator("Books"))
  238.     {
  239.         vec.push_back(p.path().string());
  240.     }
  241.     filling_strings(vec);
  242. }
  243.  
  244. void check_files(vector<string>& vec)
  245. {
  246.     unsigned int i;
  247.     vector<string> vec_2;
  248.     bool same_name = true;
  249.     for (auto p : directory_iterator("Books"))
  250.     {
  251.         vec_2.push_back(p.path().string());
  252.     }
  253.     filling_strings(vec_2);
  254.     if (vec_2.size() != vec.size())
  255.     {
  256.         vec.clear();
  257.         vec = vec_2;
  258.         filling_strings(vec);
  259.     }
  260.     else
  261.     {
  262.         if (vec.size() > vec_2.size())
  263.             i = vec_2.size();
  264.         else
  265.             i = vec.size();
  266.         for (unsigned j = 0; j < i; j++)
  267.         {
  268.             if (vec[j] != vec_2[j])
  269.             {
  270.                 vec = vec_2;
  271.                 return;
  272.             }
  273.         }
  274.     }
  275. }
  276.  
  277. int main()
  278. {
  279.     print_str(
  280.         "Hello user!\
  281.     \nThis is a program for loading files and searching for earliest date in them. \
  282.     I am your personal assistant, AI Cortana.\
  283.     \nI have few featerues like: \n",
  284.         3, 5);
  285.     print_str(
  286.         "1. Finding existing date (within 32 days and month and more than 12 months) \nincluding leap years;\
  287.     \n2. Suggesting file names if input was incorrect (Pretty unstable, needs \nto be modified);\
  288.     \n3. Looping for easy multiple requests.\
  289.     \nHave fun!\n",
  290.         10, 5);
  291.     smatch match;
  292.     regex date(
  293.         "(((0[1-9]|[12][0-9]|3[01])[-/.](0[13578]|1[02])|(0[1-9]|[12][0-9]|30)[-/.](0[469]|11)|(0[1-9]|1\\d|2[0-8])[-/.]02)[-/.]\\d{4}|29[-/.]02[-/.](\\d{2}(0[48]|[2468][048]|[13579][26])|([02468][048]|[1359][26])00))",
  294.         regex_constants::nosubs);
  295.     vector<string> books, dates;
  296.     files(books);
  297.     string file, read;
  298.     do
  299.     {
  300.         print_str("Write file's name: ", 15, 10);
  301.         do
  302.         {
  303.             getline(cin, file);
  304.         }
  305.         while (empty(file) && cout << "Why would you write nothing? Type again: ");
  306.         ifstream in;
  307.         check_files(books);
  308.         dates.clear();
  309.         in.open(make_path(file));
  310.         if (!in.is_open())
  311.         {
  312.             print_str("Did you mean \"", 15, 10);
  313.             cout << shorting_string(vec_search(books, make_path(file)));
  314.             print_str("\"?\n", 15, 10);
  315.             if (yes_no())
  316.                 in.open(vec_search(books, make_path(file)));
  317.         }
  318.         read.assign((istreambuf_iterator<char>(in)),
  319.                     istreambuf_iterator<char>());
  320.         string auto_dates = "";
  321.         auto_rifle(read, auto_dates);
  322.         regex_result(auto_dates, dates, date);
  323.         if (!in.is_open())
  324.             print_str("Couldn't load the file.\n", 7, 10);
  325.         else if (dates.empty())
  326.             print_str("There is no dates in this text file.\n", 15, 10);
  327.         else
  328.         {
  329.             string final_date = dates[0];
  330.             for (unsigned int i = 1; i < dates.size(); i++)
  331.             {
  332.                 int k = 0;
  333.                 int j = dates[i].length() - 4;
  334.                 while (final_date[j] == dates[i][j])
  335.                 {
  336.                     if (j == dates[i].length() - 1)
  337.                     {
  338.                         j -= 7;
  339.                     }
  340.                     if (j == dates[i].length() - 6)
  341.                     {
  342.                         j -= 4;
  343.                         k++;
  344.                         if (k == 2)
  345.                             break;
  346.                     }
  347.                     j++;
  348.                 }
  349.                 if (final_date[j] > dates[i][j])
  350.                     final_date = dates[i];
  351.             }
  352.             print_str("The earliest date is ", 15, 15);
  353.             cout << final_date << endl;
  354.         }
  355.         in.close();
  356.     }
  357.     while (yes_no("Do you want to continue?\n"));
  358. }
Advertisement
Add Comment
Please, Sign In to add comment