-
Notifications
You must be signed in to change notification settings - Fork 0
/
translation.h
262 lines (239 loc) · 8.21 KB
/
translation.h
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#include <iostream>
#include <fstream>
#include <unordered_map>
#include <vector>
#include <cstring>
#include <sstream>
#include "utilities.h"
struct Entry
{
std::string traditional;
std::string simplified;
std::string pinyin;
std::vector<std::string> glosses;
};
struct Dictionary
{
// The dictionary
std::unordered_map<std::string, Entry> entries;
// Some meta-data to speed up translation
int longest_word = 0;
};
void print_dict(Dictionary dic)
{
// Print the meta-data
printf("Longest word: %d\n", dic.longest_word);
// Print the elements
for (const auto &elem : dic.entries)
{
printf("%s\n", elem.first.c_str());
printf("\tSimp: %s\n", elem.second.simplified.c_str());
printf("\tTrad: %s\n", elem.second.traditional.c_str());
printf("\tPin: %s\n", elem.second.pinyin.c_str());
for (const auto &entry : elem.second.glosses)
{
printf("\t\tdef: %s\n", entry.c_str());
}
}
}
// https://stackoverflow.com/questions/4214314/get-a-substring-of-a-char
char *translate(Dictionary *dic, char *content, Dictionary *used_words)
{
int search_size;
char *output = (char *)calloc(1, sizeof(char));
if (strlen(content) < (*dic).longest_word)
{
search_size = strlen(content);
}
else
{
search_size = (*dic).longest_word;
}
// printf("search_size %d\n",search_size); //DEBUG
while (search_size >= 1)
{
char search_buf[search_size + 1];
for (int i = 0; i <= strlen(content) - search_size; i++)
{
memcpy(search_buf, &content[i], search_size);
search_buf[search_size] = '\0';
if ((*dic).entries.count(search_buf))
{
used_words->entries[search_buf] = (*dic).entries[search_buf];
// printf("MATCH: \'%s\'\n",search_buf); // DEBUG
char front_str[i + 1] = "";
memcpy(front_str, &content[0], i);
front_str[i] = '\0';
// printf("front_str: \'%s\'\n",front_str); // DEBUG
char rear_str[strlen(content) - i - search_size + 1] = "";
memcpy(rear_str, &content[i + search_size], strlen(content) - i - search_size);
rear_str[strlen(content) - i - search_size] = '\0';
// printf("rear_str: \'%s\'\n",rear_str); // DEBUG
char *temp = translate(dic, front_str, used_words);
strcat_safe(&output, temp);
free(temp);
strcat_safe(&output, (char *)"<span>");
strcat_safe(&output, search_buf);
strcat_safe(&output, (char *)"</span>");
temp = translate(dic, rear_str, used_words);
strcat_safe(&output, temp);
free(temp);
// printf("Output: \'%s\'\n",output);
return output;
}
}
search_size--;
}
strcpy_safe(&output, content);
return output;
}
std::string escape_string(std::string &s)
{
std::string result;
for (char c : s)
{
switch (c)
{
case '\\':
result += "\\\\";
break;
case '"':
result += "\\\"";
break;
default:
result += c;
break;
}
}
return result;
}
// https://vividchinese.com/where-do-the-tone-marks-go/
std::string pinyinify(std::string s)
{
char pinyin_tones[5][5][3] = {
{"ā", "á", "ǎ", "à", "a"},
{"ē", "é", "ě", "è", "e"},
{"ī", "í", "ǐ", "ì", "i"},
{"ō", "ó", "ǒ", "ò", "o"},
{"ū", "ú", "ǔ", "ù", "u"}};
std::istringstream iss(s);
std::string out;
std::string word;
// printf("\'%s\'\n",s.c_str());
bool first = true;
while (std::getline(iss, word, ' '))
{
bool applied = false;
if(!first)
{
out.append(" ");
}
first = false;
// “a” or “e” always gets the tone mark. (There is no Hanyu Pinyin that contains both a and e)
if(word.find('a') != std::string::npos && word.back() < 58 && word.length() > 1)
{
word.replace(word.find('a'),1,pinyin_tones[0][word.back()-49]);
applied = true;
}
// “a” or “e” always gets the tone mark. (There is no Hanyu Pinyin that contains both a and e)
else if(word.find('e') != std::string::npos && word.back() < 58 && word.length() > 1)
{
word.replace(word.find('e'),1,pinyin_tones[1][word.back()-49]);
applied = true;
}
// If you see “ou” or “uo,” then “o” gets the mark.
else if(word.find("ou") != std::string::npos && word.back() < 58 && word.length() > 1)
{
word.replace(word.find("ou"),1,pinyin_tones[3][word.back()-49]);
applied = true;
}
// If you see “ou” or “uo,” then “o” gets the mark.
else if(word.find("uo") != std::string::npos && word.back() < 58 && word.length() > 1)
{
word.replace(word.find("uo")+1,1,pinyin_tones[3][word.back()-49]);
applied = true;
}
// The final vowel gets the tone mark in all other cases.
else if(word.find_last_of("aeiou") != std::string::npos && word.back() < 58 && word.length() > 1)
{
int letter = 0;
switch (word.at(word.find_last_of("aeiou")))
{
case 'a':
letter = 0;
break;
case 'e':
letter = 1;
break;
case 'i':
letter = 2;
break;
case 'o':
letter = 3;
break;
case 'u':
letter = 4;
break;
}
word.replace(word.find_last_of("aeiou"),1,pinyin_tones[letter][word.back()-49]);
applied = true;
}
if(applied)
{
word.pop_back();
}
out.append(word);
}
// printf("\'%s\'\n",out.c_str());
return out;
}
void load_dict(Dictionary *dic, char *file_name)
{
std::ifstream file(file_name);
std::string line;
printf("Loading dictionary.\n");
while (std::getline(file, line))
{
// Skip comments
if (line[0] == '#')
continue;
// Escape \ and "
line = escape_string(line);
// Find pinyin start and end indices
size_t pinyin_start = line.find("[") + 1;
size_t pinyin_end = line.find("]");
size_t glosses_start = line.find("/", pinyin_end) + 1;
size_t glosses_end = line.length() - 1;
// Extract traditional, simplified, and pinyin
std::string traditional = line.substr(0, line.find(" "));
std::string simplified = line.substr(line.find(" ") + 1, pinyin_start - line.find(" ") - 3);
std::string pinyin = line.substr(pinyin_start, pinyin_end - pinyin_start);
// Cleanup pinyin
pinyin = pinyinify(pinyin);
// printf("%s\n",pinyin.c_str());
// Extract glosses
std::vector<std::string> glosses;
std::string glosses_str = line.substr(glosses_start, glosses_end - glosses_start);
std::istringstream iss(glosses_str);
std::string gloss;
while (std::getline(iss, gloss, '/'))
{
glosses.push_back(gloss);
}
// Update longest word
if ((*dic).longest_word < traditional.length())
{
(*dic).longest_word = traditional.length();
// printf("New longest word: %s %ld\n",traditional.c_str(),traditional.length()); // DEBUG
}
// Add entry to map
Entry entry;
entry.traditional = traditional;
entry.simplified = simplified;
entry.pinyin = pinyin;
entry.glosses = glosses;
(*dic).entries[traditional] = entry;
(*dic).entries[simplified] = entry;
}
printf("Dictionary loaded.\n");
}