-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtranslate.html
305 lines (236 loc) · 7.17 KB
/
translate.html
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
<!DOCTYPE html>
<html>
<head>
<title>Translate</title>
<meta charset="utf-8" />
<style>
body {
font-family: Tahoma, Geneva, sans-serif;
}
table td {
white-space: nowrap;
}
#chinese_1 {
font-family: "Heiti TC Light", Tahoma, Geneva, sans-serif;
color:black;
font-size: 30px;
display:inline;
}
#chinese_2 {
font-family: "Heiti TC Light", Tahoma, Geneva, sans-serif;
color:red;
font-size: 30px;
display:inline;
}
#chinese_3 {
font-family: "Heiti TC Light", Tahoma, Geneva, sans-serif;
color:green;
font-size: 30px;
display:inline;
}
#chinese_4 {
font-family: "Heiti TC Light", Tahoma, Geneva, sans-serif;
color:blue;
font-size: 30px;
display:inline;
}
#pinyin_1 {
font-family: "Heiti TC Light", Tahoma, Geneva, sans-serif;
color:black;
display:inline;
}
#pinyin_2 {
color:red;
display:inline;
}
#pinyin_3 {
color:green;
display:inline;
}
#pinyin_4 {
color:blue;
display:inline;
}
</style>
</head>
<body>
<table>
<tr>
<td>
Chinese input
</td>
</tr>
<tr>
<td>
<textarea id="inputTextArea" rows="10" cols="50" >中文
老師</textarea>
</td>
<td style="width:100px;height:5px;background-color:#BBBBBB;text-align:center;" onclick="translateText();">
Translate
</td>
</tr>
<tr>
<td>
English input
</td>
</tr>
<tr>
<td>
<textarea id="englishTextArea" rows="10" cols="50" >Chinese
teacher</textarea>
</td>
</tr>
<tr>
<td>
HTML output
</td>
</tr>
<tr>
<td>
<div id="outputHtml"></div>
</td>
</td>
<tr>
<td>
Text output
</td>
</tr>
<tr>
<td>
<textarea id="outputTextArea" rows="10" cols="50"></textarea>
</td>
</tr>
</table>
</body>
<script>
var pinyinDefinitionsToneData;
function loadPinyinDefinitionsTones(filename)
{
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == XMLHttpRequest.DONE ) {
if (xmlhttp.status == 200)
{
pinyinDefinitionsToneData = xmlhttp.responseText;
} // end if it loaded the KML
else if (xmlhttp.status == 400) {
alert('There was an error 400');
}
else {
alert('something else other than 200 was returned');
}
}
};
xmlhttp.open("GET", filename, true);
xmlhttp.send();
} // end loadGenealogies
function textBetween(thisText, startString, endString)
{
var start_pos = 0;
if (startString != 'start')
{
start_pos = thisText.indexOf(startString);
// If the text does not contain the start string, return a blank string
if (start_pos < 0)
{
return '';
}
// Skip the first startString characters
start_pos = start_pos + startString.length;
}
var end_pos = thisText.length;
if (endString != 'end')
{
end_pos = thisText.indexOf(endString,start_pos);
}
// If the text does not have the end string after the start string, return the whole string after the start
if (end_pos < start_pos)
{
end_pos = thisText.length;
}
var newText = thisText.substring(start_pos,end_pos);
return newText;
} // end textBetween
// contains - Does thisText contain searchString?
function contains(thisText, searchString)
{
if (thisText == null)
{
return false;
}
return thisText.indexOf(searchString) != -1;
} // end function contains
function translateText()
{
var inputText = document.getElementById("inputTextArea").value;
var englishText = document.getElementById("englishTextArea").value;
var outputText = "";
var outputHtml = "";
var inputArray = inputText.split("\n");
var englishArray = englishText.split("\n");
var numberLines = inputArray.length;
for (var currentLine = 0; currentLine < numberLines; currentLine++)
{
var thisLine = inputArray[currentLine];
var englishLine = englishArray[currentLine];
var chineseHtml = "";
var pinyinHtml = "";
var translatedHtml = "";
var chineseText = "";
var pinyinText = "";
var translatedText = "";
var numberInput = thisLine.length;
for (var currentInput = 0; currentInput < numberInput; currentInput++)
{
var thisCharacter = thisLine[currentInput];
if (thisCharacter == " ")
{
chineseHtml = chineseHtml + "</td><td>";
pinyinHtml = pinyinHtml + "</td><td>";
translatedHtml = translatedHtml + "</td><td>";
chineseText = chineseText + " ";
pinyinText = pinyinText + " ";
translatedText = translatedText + " ";
} else {
if (contains(pinyinDefinitionsToneData, "\n" + thisCharacter + " ") == true)
{
var thisCharacterLine = textBetween(pinyinDefinitionsToneData, "\n" + thisCharacter + " ", "\n");
var thisCharacterArray = thisCharacterLine.split(" ");
var thisPinyin = thisCharacterArray[0];
var thisDefinition = thisCharacterArray[1];
var thisTone = thisCharacterArray[2];
//var thisPinyin = textBetween(thisCharacterLine, "start", " ");
//var thisDefinitionTone = textBetween(thisCharacterLine, " ", "end");
//var thisDefinition = textBetween(thisDefinitionTone, "start", " ");
//var thisTone = textBetween(thisDefinitionTone, " ", "end");
chineseHtml = chineseHtml + "<div id=\"chinese_" + thisTone + "\">" + thisCharacter + "</div>";
pinyinHtml = pinyinHtml + "<div id=\"pinyin_" + thisTone + "\">" + thisPinyin + "</div> ";
translatedHtml = translatedHtml + " " + thisDefinition;
chineseText = chineseText + thisCharacter;
pinyinText = pinyinText + thisPinyin + " ";
translatedText = translatedText + thisDefinition + " ";
//alert(thisTone);
} else {
chineseHtml = chineseHtml + thisCharacter;
pinyinHtml = pinyinHtml + thisCharacter;
translatedHtml = translatedHtml + thisCharacter;
chineseText = chineseText + thisCharacter;
pinyinText = pinyinText + thisCharacter;
translatedText = translatedText + thisCharacter;
} // end if the dictionary contains the character already
} // end if this character is a space
} // end for every character
outputText = outputText + chineseText + "\n" + pinyinText + "\n" + translatedText + "\n" + englishLine + "\n" + "\n";
outputHtml = outputHtml + "<p><table>";
outputHtml = outputHtml + "<tr><td>" + chineseHtml + "</td></tr>";
outputHtml = outputHtml + "<tr><td>" + pinyinHtml + "</td></tr>";
outputHtml = outputHtml + "<tr><td>" + translatedHtml + "</td></tr>";
outputHtml = outputHtml + "</table><p>";
outputHtml = outputHtml + englishLine;
} // end for every line
document.getElementById("outputHtml").innerHTML = outputHtml;
document.getElementById("outputTextArea").value = outputText;
} // end function translate
loadPinyinDefinitionsTones("PinyinDefinitionsTones.txt");
</script>
</html>