-
Notifications
You must be signed in to change notification settings - Fork 0
/
dictionarySearch.cpp
161 lines (149 loc) · 6.51 KB
/
dictionarySearch.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
//-----------------------------------------------------------------------
// Source : dictionarySearch.cpp
// Last modified : 31.03.2023
// Version : 10
// Description : Search word in dictionary (Rus-Lat or Lat-Rus)
//-----------------------------------------------------------------------
#include "latin.h"
using namespace std;
bool searchWord(uint lettersCutted, vector<string>& definitions, fstream& dictionaryFile, bool isLatRus) {
mainLog.print({ "== Start: searchWord with lettersCutted = ", intToStr(lettersCutted, 0) });
string word = currentRequest.word.substr(0, currentRequest.word.size() - lettersCutted),
inputLine;
while (inputLine != "DICTIONARY_END") {
getline(dictionaryFile, inputLine);
if (inputLine.find(word) != string::npos) {
definitions.push_back(inputLine);
if (isLatRus == false && inputLine[0] >= 58) {
do {
int pos = dictionaryFile.tellg();
getline(dictionaryFile, inputLine);
if (inputLine[0] < 58)
definitions.push_back(inputLine);
else
dictionaryFile.seekg(pos);
} while (inputLine[0] < 58);
}
}
}
if (definitions.size() != 0) {
mainLog.print("=== End of: searchWord, returned true");
return true;
}
else {
mainLog.print("=== End of: searchWord, returned false");
return false;
}
}
void dictionarySearch(const string DICTIONARYLR_PATH, const string DICTIONARYRL_PATH) {
mainLog.print("= Start of: dictionarySearch");
mainLog.print({ "= CR.code: ", intToStr(currentRequest.code,0) });
vector<string> definitions;
fstream dictionaryFile;
const int maxLettersCutted = 3;
system("cls");
bool isLatRus;
if (currentRequest.code == menuCode::DICTIONARYSEARCH) {
currentRequest.clear();
cout << "Äîñòóïåí ïåðåâîä ñ ëàòèíñêîãî ÿçûêà íà ðóññêèé è ñ ðóññêîãî íà ëàòèíñêèé,\n" <<
"ðåæèì áóäåò îïðåäåë¸í àâòîìàòè÷åñêè ïî ââåä¸ííîìó ñëîâó.\n" <<
"Ãëóáèíà ïîèñêà (êîëè÷åñòâî áóêâ â êîíöå ñëîâà, êîòîðûìè ìîæíî ïðåíåáðå÷ü) ïî óìîë÷àíèþ ñîñòàâëÿåò 3\n\n" <<
"Ââåäèòå ñëîâî äëÿ ïåðåâîäà ñòðî÷íûìè áóêâàìè: ";
isLatRus = currentRequest.inputWord();
if(isLatRus == true)
openFile(dictionaryFile, DICTIONARYLR_PATH, ios::in, true);
else
openFile(dictionaryFile, DICTIONARYRL_PATH, ios::in, true);
}
if (currentRequest.code == menuCode::OUT_DICTIONARYSEARCH) {
cout << "Ñëîâî äëÿ ïåðåâîäà: " << currentRequest.word << endl;
if (currentRequest.word[0] > 0) {
isLatRus = true;
openFile(dictionaryFile, DICTIONARYLR_PATH, ios::in, true);
}
else {
isLatRus = false;
openFile(dictionaryFile, DICTIONARYRL_PATH, ios::in, true);
}
}
cout << "\nÏîèñê ïî çàïðîñó: " << currentRequest.word << "...\n";
uint lettersCutted = 0;
while(lettersCutted <= maxLettersCutted) {
dictionaryFile.seekg(0, ios::beg);
if (searchWord(lettersCutted, definitions, dictionaryFile, isLatRus) == true) {
break;
}
lettersCutted++;
}
if (definitions.size() == 0) {
cout << "Ðåçóëüòàòû ïî çàïðîñó " << currentRequest.word << " íå íàéäåíû";
mainLog.print("-- Search failed");
}
else {
mainLog.print({ "-- Found ", intToStr(definitions.size(), 0), " results" });
if (lettersCutted == 0)
cout << "Ðåçóëüòàòû ïî çàïðîñó " << currentRequest.word << ":\n\n";
if (lettersCutted > 0)
cout << "Ìîæåò, âû èìåëè â âèäó " <<
currentRequest.word.substr(0, currentRequest.word.size() - lettersCutted) << "?\n\n";
for (int i = 0; i < (int)(definitions.size()); i++)
cout << i + 1 << ": " << definitions[i] << endl;
cout << "\nÅñëè õîòèòå óâåëè÷èòü ãëóáèíó ïîèñêà, ââåäèòå 1, èíà÷å - ëþáóþ äðóãóþ êëàâèøó\n";
char isContinue = _getch();
while (isContinue == '1') {
lettersCutted++;
dictionaryFile.seekg(0, ios::beg);
definitions.clear();
cout << "\nÏîèñê ïî çàïðîñó: " <<
currentRequest.word.substr(0, currentRequest.word.size() - lettersCutted) << "...\n";
searchWord(lettersCutted, definitions, dictionaryFile, isLatRus);
cout << "Ðåçóëüòàòû ïî çàïðîñó " <<
currentRequest.word.substr(0, currentRequest.word.size() - lettersCutted) << ":\n\n";
for (int i = 0; i < (int)(definitions.size()); i++)
cout << i + 1 << ": " << definitions[i] << endl;
cout << "\nÅñëè õîòèòå óâåëè÷èòü ãëóáèíó ïîèñêà, ââåäèòå 1, èíà÷å - ëþáóþ äðóãóþ êëàâèøó\n";
isContinue = _getch();
}
cout << "\nÂâåäèòå íîìåð íàèáîëåå ïîäõîäÿùåãî îïðåäåëåíèÿ, ÷òîáû ñîõðàíèòü åãî,\n" <<
"äëÿ âûõîäà áåç ñîõðàíåíèÿ - 0\n";
int tempInt = inputInt();
while (tempInt < 0 || tempInt > definitions.size()) {
cout << "Ââîä íåêîððåêòåí, ïîâòîðèòå âûáîð\n";
tempInt = inputInt();
}
if(tempInt == 0)
mainLog.print("-- User chose not to save definition");
else {
mainLog.print("-- User chose to save definition");
currentRequest.setDef(definitions[tempInt - 1]);
mainOutput.updateWordInfo(-1, currentRequest);
}
}
dictionaryFile.close();
mainLog.print("=F= dictionaryFile closed");
ConsoleOutputBlock selectFinal("\nÍàæìèòå:", 5,
{ "íîâûé çàïðîñ ïî ñëîâàðþ" }, { 1 });
if (currentRequest.forms.empty() == true)
selectFinal.addMessage("àíàëèç äàííîãî ñëîâà", 5);
selectFinal.addMessage("Ëþáàÿ äðóãàÿ êëàâèøà - âûõîä â ìåíþ", -1);
selectFinal.print();
switch (selectFinal.inputChoice()) {
case menuCode::DEFAULT: {
currentRequest.setCode(menuCode::DEFAULT);
break;
}
case menuCode::DICTIONARYSEARCH: {
currentRequest.setCode(menuCode::DICTIONARYSEARCH);
break;
}
case menuCode::OUT_FORMANALYSIS: {
currentRequest.setCode(menuCode::OUT_FORMANALYSIS);
break;
}
default: {
currentRequest.setCode(menuCode::DEFAULT);
break;
}
}
mainLog.print("= End of: dictionarySearch");
}