Advertisement
TShiva

Translator

Jul 26th, 2016
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //GUI
  2.  
  3. #include <Windows.h>
  4. #include "Damerau_Levenstein.h"
  5. #include "Dictionary.h"
  6.  
  7. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM );
  8. #define ID_BUTTON1 1
  9. #define ID_TEXTBOX 2
  10. #define ID_BUTTON2 3
  11. #define ID_LISTBOX 4
  12. #define ID_BUTTON_SEARCH 5
  13.  
  14. int showWindow()
  15. {
  16.     LPCWSTR szClassName = L"MyWindow";
  17.  
  18.     MSG msg;
  19.  
  20.     WNDCLASSEX wcex;
  21.     memset(&wcex, 0, sizeof(wcex));
  22.     wcex.cbSize = sizeof(WNDCLASSEX);
  23.     wcex.style = CS_HREDRAW | CS_VREDRAW;
  24.     wcex.lpfnWndProc = WndProc;
  25.     wcex.cbClsExtra = 0;
  26.     wcex.cbWndExtra = 0;
  27.     wcex.hInstance = NULL;
  28.     wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
  29.     wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW - 2);
  30.     wcex.lpszMenuName = NULL;
  31.     wcex.lpszClassName = szClassName;
  32.     wcex.hIconSm = LoadIcon(wcex.hInstance,  MAKEINTRESOURCE(IDI_APPLICATION));
  33.     int res = RegisterClassEx(&wcex);
  34.  
  35.     HWND hMainWnd = CreateWindow(szClassName, // имя класса
  36.         L"Полноценная оконная процедура", // имя окна (то что сверху)
  37.         WS_OVERLAPPED| WS_CAPTION| WS_SYSMENU| WS_MINIMIZEBOX, // режимы отображения окошка
  38.         100, // положение окна по оси х (по умолчанию)
  39.         100, // позиция окна по оси у (раз дефолт в х, то писать не нужно)
  40.         295, // ширина окошка (по умолчанию)
  41.         370, // высота окна (раз дефолт в ширине, то писать не нужно)
  42.         NULL, // дескриптор родительского окошка (у нас нет род. окон)
  43.         NULL, // дескриптор меню (у нас его нет)
  44.         NULL, // .... экземпляра приложения
  45.         NULL); // ничего не передаём из WndProc
  46.  
  47.     ShowWindow(hMainWnd, SW_SHOWNORMAL);
  48.  
  49.     while (GetMessage(&msg, NULL, 0, 0))
  50.     {
  51.         TranslateMessage(&msg);// Функция трансляции кодов нажатой клавиши
  52.         DispatchMessage(&msg); // Посылает сообщение функции WndProc()
  53.     }
  54.  
  55.     return 0;
  56. }
  57.  
  58. HWND hEdit;
  59.  
  60. LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wparam, LPARAM lparam)
  61. {
  62.     switch (Message)
  63.     {
  64.  
  65.     case WM_CREATE:
  66.         CreateWindow(L"button", L"Направление перевода", WS_CHILD | BS_PUSHBUTTON | WS_VISIBLE, 5, 30, 270, 35, hwnd, (HMENU)ID_BUTTON1, NULL, NULL);
  67.         hEdit = CreateWindowEx(WS_EX_STATICEDGE, L"edit", NULL, WS_CHILD | WS_VISIBLE | ES_MULTILINE | ES_AUTOVSCROLL, 5, 10, 270, 25, hwnd, (HMENU)ID_TEXTBOX, NULL, NULL);
  68.         CreateWindow(L"button", L"Перевести", WS_CHILD | BS_PUSHBUTTON | WS_VISIBLE, 5, 290, 270, 35, hwnd, (HMENU)ID_BUTTON2, NULL, NULL);
  69.         CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("listbox"), L"", WS_CHILD | WS_VISIBLE | WS_VSCROLL | ES_AUTOVSCROLL, 5, 70, 270, 220, hwnd, (HMENU)ID_LISTBOX, NULL, NULL);
  70.  
  71.         return 0;
  72.     case WM_COMMAND:
  73.     {
  74.         int Id = LOWORD(wparam);
  75.         int Event = HIWORD(wparam);
  76.         switch (Id)
  77.         {
  78.             case ID_TEXTBOX:
  79.             {
  80.                 switch (Event)
  81.                 {
  82.                     case EN_CHANGE:
  83.                     {
  84.                         int length_of_textbox = GetWindowTextLength(hEdit);
  85.                         TCHAR* buf = new TCHAR[length_of_textbox + 1];
  86.                         GetWindowText(hEdit, buf, length_of_textbox + 1);
  87.  
  88.                         break;
  89.                     }
  90.                 }
  91.                 break;
  92.             }
  93.  
  94.             case ID_LISTBOX:
  95.             {
  96.                 switch (Event)
  97.                 {
  98.                     case LBN_DBLCLK:
  99.                     {
  100.                         //MessageBox(hwnd, )
  101.                     }
  102.                 }
  103.             }
  104.             case ID_BUTTON1:
  105.             case ID_BUTTON2:
  106.         }
  107.         return 0;
  108.     }
  109.  
  110.  
  111.         case WM_DESTROY:
  112.         PostQuitMessage(0);
  113.         break;
  114.     }
  115.  
  116.     return DefWindowProc(hwnd, Message, wparam, lparam);
  117. }
  118.  
  119. int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
  120. {
  121.     return showWindow();
  122.  
  123. }
  124.  
  125.  
  126. //Logic
  127.  
  128. #include <Windows.h>
  129. #include <iostream>
  130. #include <map>
  131. #include <vector>
  132. #include <fstream>
  133. #include <string>
  134. #include <memory>
  135. #include "Damerau_Levenstein.h"
  136.  
  137. namespace Dictionary
  138. {
  139.     typedef std::map<std::wstring, std::wstring> dictionary_t;
  140.     typedef std::shared_ptr<dictionary_t> dict_ptr;//умный поинтер
  141.     ////////////////////////////////////////////////////////
  142.     enum TranslateDirection
  143.     {
  144.         ENG2RU,
  145.         RU2ENG
  146.     };
  147.  
  148.     class IParser
  149.     {
  150.     public:
  151.         virtual std::vector<dict_ptr> parse() = 0;
  152.     };
  153.     class IDictionary
  154.     {
  155.     public:
  156.         virtual bool load() = 0;
  157.         virtual std::vector<std::wstring> find(std::wstring, int, TranslateDirection) = 0;
  158.         virtual std::wstring translate(std::wstring str, TranslateDirection transDir) = 0;
  159.         virtual ~IDictionary() = 0;
  160.     };
  161.  
  162.     ////////////////////////////////////////////////////////
  163.  
  164.     class Parser : IParser
  165.     {
  166.     private:
  167.         std::wstring m_filename;
  168.  
  169.     public:
  170.         Parser(std::wstring filename) : m_filename(filename) {}
  171.  
  172.         std::vector<dict_ptr> parse()
  173.         {
  174.             dict_ptr rus_dict = std::make_shared<dictionary_t>();
  175.             dict_ptr eng_dict = std::make_shared<dictionary_t>();
  176.             std::vector<dict_ptr> dicts;
  177.  
  178.             std::wifstream file;
  179.             file.open(m_filename);
  180.             if (!file.is_open())
  181.                 std::cout << "Файл не может быть открыт";
  182.             else{
  183.                 std::wstring wstring;
  184.                 while (std::getline(file, wstring))
  185.                 {
  186.                     size_t sign = 0; // итератор на конец слова
  187.                     sign = wstring.find('<', 0);
  188.                     eng_dict->operator[](wstring.substr(0, sign)) = wstring.substr(sign + 2, wstring.size());
  189.                     rus_dict->operator[](wstring.substr(sign + 2, wstring.size())) = wstring.substr(0, sign);
  190.                 }
  191.                 dicts.push_back(rus_dict);
  192.                 dicts.push_back(eng_dict);
  193.             }
  194.             return dicts;
  195.         }
  196.  
  197.     };
  198.  
  199.     class Dictionary : IDictionary
  200.     {
  201.         dict_ptr m_ru2engDic;
  202.         dict_ptr m_eng2ruDic;
  203.     public:
  204.  
  205.         Dictionary() {}
  206.  
  207.         bool load(IParser &parser)
  208.         {
  209.             std::vector<dict_ptr> vec = parser.parse();
  210.             auto dict = std::move(vec.at(0));
  211.             m_ru2engDic = std::move(vec.at(0));
  212.             m_eng2ruDic = std::move(vec.at(1));
  213.         };
  214.  
  215.         std::vector<std::wstring> find(std::wstring word, int d, TranslateDirection transDir) override // проверяет есть ли это в родительском классе
  216.         {
  217.             std::vector<std::wstring> words;
  218.             if (transDir == RU2ENG)
  219.             {
  220.                 for (auto i : *m_ru2engDic)
  221.                 {
  222.                     if (Damerau_Levenstein(word, i.first) <= d)
  223.                     {
  224.                         words.push_back(i.first);
  225.                     }
  226.                 }
  227.             }
  228.             else
  229.             {
  230.                 for (auto i : *m_eng2ruDic)
  231.                 {
  232.                     if (Damerau_Levenstein(word, i.first) <= d)
  233.                     {
  234.                         words.push_back(i.first);
  235.                     }
  236.                 }
  237.             }
  238.             return words;
  239.         }
  240.  
  241.         std::wstring translate(std::wstring str, TranslateDirection transDir)
  242.         {
  243.             if (transDir == TranslateDirection::ENG2RU)
  244.             {
  245.                 m_eng2ruDic->at(str);
  246.             }
  247.             else
  248.             {
  249.                 m_ru2engDic->at(str);
  250.             }
  251.             return std::wstring();
  252.         };
  253.     };
  254. }
  255.  
  256. // Damerau Levenstein
  257.  
  258. #include "Damerau_Levenstein.h"
  259. #include <windows.h>
  260.  
  261. size_t Damerau_Levenstein(const std::wstring& str1, const std::wstring& str2)
  262. {
  263.     const size_t m = str1.length(), n = str2.length();
  264.     if (m == 0) {
  265.         return n;
  266.     }
  267.     if (n == 0) {
  268.         return m;
  269.     }
  270.     std::vector<std::vector<size_t>> matrix(m + 1);
  271.  
  272.     for (size_t i = 0; i <= m; ++i) {
  273.         matrix[i].resize(n + 1);
  274.         matrix[i][0] = i;
  275.     }
  276.     for (size_t i = 0; i <= n; ++i) {
  277.         matrix[0][i] = i;
  278.     }
  279.  
  280.     size_t up_elem, left_elem, diag_elem, cost;
  281.  
  282.     for (size_t i = 1; i <= m; ++i) {
  283.         for (size_t j = 1; j <= n; ++j) {
  284.             diag_elem = matrix[i - 1][j - 1];
  285.             left_elem = matrix[i][j - 1];
  286.             up_elem = matrix[i - 1][j];
  287.             cost = str1[i - 1] == str2[j - 1] ? 0 : 1;
  288.             matrix[i][j] = min(min(up_elem + 1, left_elem + 1), diag_elem + cost);
  289.             if (i > 1 & j > 1 & str1[i] == str2[j - 1] & str1[i - 1] == str2[j])
  290.                 matrix[i][j] = min(matrix[i][j], matrix[i - 2][j - 2] + 1);
  291.         }
  292.     }
  293.     return matrix[m][n];
  294. }
  295.  
  296. //.h алгоритма
  297. #pragma once
  298. #include "string.h"
  299. #include <vector>
  300. size_t Damerau_Levenstein(const std::wstring& str1, const std::wstring& str2);
  301.  
  302. // Controller
  303.  
  304. #include "Dictionary.h"
  305.  
  306. using namespace Dictionary;
  307.  
  308. class Controller{
  309.     std::wstring cur_translated_str;
  310.     IDictionary *m_dic;
  311.  
  312. public:
  313.     void init();
  314.     std::wstring getTranslatedWord();
  315.     void setTranslatedWord(std::wstring);
  316.     void translate();
  317.     void setTranslateCallBack(std::wstring (CallBack)());
  318.     void setFilterCallBack(std::vector<std::wstring>(CallBack));
  319. };
  320.  
  321. //.h часть
  322.  
  323. #include "Controller.h"
  324.  
  325.  
  326. std::wstring Controller::getTranslatedWord(){}
  327.  
  328. void Controller::setTranslatedWord(std::wstring word){
  329.     cur_translated_str = word;
  330. }
  331.  
  332. void Controller::translate(){
  333.  
  334. }
  335.  
  336. void Controller::setTranslateCallBack(std::wstring(CallBack)())
  337.  
  338. void Controller::setFilterCallBack(std::vector<std::wstring>(CallBack))
  339.  
  340. //Ещё не реализовано
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement