This repository has been archived by the owner on Apr 3, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
398 lines (395 loc) · 13.6 KB
/
index.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
<!DOCTYPE HTML>
<html lang="ja">
<head>
<meta charset="utf-8">
<title>こそこそ翻訳</title>
<meta name="X-UA-Compatible" content="IE=Edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="author" content="mtripg6666tdr">
<meta name="description" content="日本語とこそこそ語の相互変換をすることができます">
<meta name="keywords" content="翻訳,ツール,こそこそ,こそこそ語,Digitaltal Playground">
<meta name="twitter:card" content="summary">
<meta name="twitter:site" content="@mtripg6666tdr">
<meta name="twitter:creator" content="@mtripg6666tdr">
<meta name="og:title" content="こそこそ翻訳">
<meta name="og:description" content="日本語とこそこそ語の相互変換をすることができます">
<link rel="stylesheet" href="lib/materialize.min.css">
<title>こそこそ翻訳</title>
<style type="text/css">
nav div.nav-wrapper span.brand-logo {
margin-left: 20px;
}
div.main_container {
width: 100%;
padding-top: 40px;
}
div.main_container div.main {
width: 80%;
margin: 1vh auto;
}
div.select-wrapper span {
color: black;
}
</style>
<script type="text/javascript">
// グローバル変数
var fromElem, toElem, originalElem, resultElem, toLabel;
// ユーティリティ
function TextToByteArray(text){
if(typeof text !== "string") throw new Error("Received param is not a string!");
const result = [];
if (text == null)
return result;
for (var i = 0; i < text.length; i++) {
const c = text.charCodeAt(i);
if (c <= 0x7f) {
result.push(c);
} else if (c <= 0x07ff) {
result.push(((c >> 6) & 0x1F) | 0xC0);
result.push((c & 0x3F) | 0x80);
} else {
result.push(((c >> 12) & 0x0F) | 0xE0);
result.push(((c >> 6) & 0x3F) | 0x80);
result.push((c & 0x3F) | 0x80);
}
}
return result;
}
function ByteArrayToText(arr) {
if(!Array.isArray(arr)) throw new Error("Received param is not an array!");
if (arr == null)
return null;
let result = "";
let i;
while (i = arr.shift()) {
if (i <= 0x7f) {
result += String.fromCharCode(i);
} else if (i <= 0xdf) {
let c = ((i&0x1f)<<6);
c += arr.shift()&0x3f;
result += String.fromCharCode(c);
} else if (i <= 0xe0) {
let c = ((arr.shift()&0x1f)<<6)|0x0800;
c += arr.shift()&0x3f;
result += String.fromCharCode(c);
} else {
let c = ((i&0x0f)<<12);
c += (arr.shift()&0x3f)<<6;
c += arr.shift() & 0x3f;
result += String.fromCharCode(c);
}
}
return result;
}
function _eget(id){
return document.getElementById(id);
}
// !!!
// 共通中間データ = Common Intermediate Data = CID
// データ様式は、配列。
// 翻訳元の文字列を通常文字と翻訳で置換されない特殊に分割した配列にし、通常文字を0と1の数値に変換したもの
// !!!
// テキストからCIDに変換
function convertTextToCID(value){
if(typeof value !== "string") throw new Error("Received param is not a string!");
return value.split("").map(function(char){
switch(char){
case "?": return "?";
case "!": return "!";
case "?": return "?";
case "!": return "!";
case "、": return "、";
case "。": return "。";
case ",": return ",";
case ".": return ".";
case "\r": return "\r";
case "\n": return "\n";
default: return TextToByteArray(char).map(function(byte){
return (256 + byte).toString(2).substr(1)
}).join("");
}
});
}
// こそこそ語(可逆)をCIDに変換
function convertKoso1ToCID(value){
if(typeof value !== "string") throw new Error("Received param is not a string!");
const raw = value.split('')
let result = [];
let current = [];
for(let i = 0; i < raw.length; i++){
switch(raw[i]){
case "こ":
current.push("0");
break;
case "そ":
current.push("1");
break;
case "?":
case "!":
case "?":
case "!":
case "、":
case "。":
case ",":
case ".":
case "\r":
case "\n":
if(current.length > 0){
result.push(current.join(""));
current = [];
}
result.push(raw[i]);
break;
default: throw new Error("Invalid character");
}
}
if(current.length > 0)
result.push(current.join(""));
return result;
}
// CIDからテキストに変換
function convertCIDToText(value){
if(!Array.isArray(value)) throw new Error("Received param is not an array!");
const result = [];
for(let i = 0; i < value.length; i++){
switch(value[i]){
case "?":
case "!":
case "?":
case "!":
case "、":
case "。":
case ",":
case ".":
case "\r":
case "\n":
result.push(value[i]);
break;
default:
while(value[i].length % 8 !== 0){
value[i] = "0" + value[i];
}
const chunkLen = value[i].length / 8;
const bytes = [];
for(let j = 0; j < chunkLen; j++){
bytes.push(parseInt([...Array(8)].map(function(_, k){
return value[i][j * 8 + k]
}).join(""), 2));
}
result.push(ByteArrayToText(bytes));
break;
}
}
return result.join("");
}
// CIDからこそこそ語(可逆)に変換
function convertCIDToKoso1(value){
if(!Array.isArray(value)) throw new Error("Received param is not an array!");
const result = [];
for(let i = 0; i < value.length; i++){
switch(value[i]){
case "?":
case "!":
case "?":
case "!":
case "、":
case "。":
case ",":
case ".":
case "\r":
case "\n":
result.push(value[i]);
break;
default:
result.push(value[i].split("").map(function(char){
switch(char){
case "0": return "こ";
case "1": return "そ";
}
}).join(""));
}
}
return result.join("");
}
// CIDからこそこそ語(非可逆)に変換
function convertCIDToKoso2(value){
if(!Array.isArray(value)) throw new Error("Received param is not an array!");
const result = [];
for(let i = 0; i < value.length; i++){
switch(value[i]){
case "?":
case "!":
case "?":
case "!":
case "、":
case "。":
case ",":
case ".":
case "\r":
case "\n":
result.push(value[i]);
break;
default:
console.log(value[i]);
result.push(value[i].split("").reduce(function(prev, current){
return Number(prev) + Number(current);
}) % 2 === 0 ? "こ" : "そ");
}
}
return result.join("");
}
// 変換
function translate(){
let result = null;
if(fromElem.value === toElem.value){
result = "翻訳先を選択してください";
}else{
try{
const cid =
fromElem.value === "ja" ? /*ja*/ convertTextToCID(originalElem.value) :
/*koso1*/ convertKoso1ToCID(originalElem.value);
result =
toElem.value === "ja" ? /*ja*/ convertCIDToText(cid) :
toElem.value === "koso1" ? /*koso1*/ convertCIDToKoso1(cid) :
/*koso2*/ convertCIDToKoso2(cid);
} catch(e){
console.error(e);
result = "変換できませんでした: " + e;
}
}
resultElem.value = result;
M.textareaAutoResize(resultElem);
toLabel.classList[result === "" ? "remove" : "add"]("active");
}
window.addEventListener("load", function(){
fromElem = _eget("s_from");
toElem = _eget("s_to");
originalElem = _eget("original_text");
resultElem = _eget("result_text");
toLabel = _eget("s_to_label");
[fromElem, toElem, originalElem].forEach(function(elem){
elem.addEventListener("change", translate);
});
originalElem.addEventListener("keyup", translate);
_eget("copy_btn").addEventListener("click", function(){
if(resultElem.value === ""){
M.toast({text: "まず翻訳をしてください", classes: "copied"});
}else{
resultElem.select();
document.execCommand("copy");
M.toast({text: "✅コピーしました", classes: "copied"});
}
});
});
["resize", "load"].forEach(function(event){
window.addEventListener(event, function(){
if(window.innerWidth < 1000) {
[
_eget("col_left"),
_eget("col_right")
].forEach(function(elem){
elem.classList.remove("s6");
elem.classList.add("s12");
});
}else{
[
_eget("col_left"),
_eget("col_right")
].forEach(function(elem){
elem.classList.remove("s12");
elem.classList.add("s6");
});
}
if(window.innerWidth < 500){
[
_eget("s_from_container"),
_eget("s_to_container")
].forEach(function(elem){
elem.classList.remove("s5");
elem.classList.add("s12");
});
const middle = _eget("s_middle");
middle.classList.remove("s2")
middle.classList.add("s12");
middle.children[0].textContent = "⇓";
_eget("s_container").style.height = "auto";
_eget("r_container").style.marginTop = "30px";
}else{
[
_eget("s_from_container"),
_eget("s_to_container")
].forEach(function(elem){
elem.classList.remove("s12");
elem.classList.add("s5");
});
const middle = _eget("s_middle");
middle.classList.remove("s12")
middle.classList.add("s2");
middle.children[0].textContent = "⇒";
_eget("s_container").style.height = "60px";
_eget("r_container").style.marginTop = "80px";
}
});
});
document.addEventListener("DOMContentLoaded", function() {
M.FormSelect.init(document.querySelectorAll('select'), {});
});
</script>
</head>
<body>
<nav>
<div class="nav-wrapper blue darken-3">
<span class="brand-logo left">こそこそ翻訳</span>
</div>
</nav>
<div class="main_container">
<div class="main" style="min-height: 75vh;">
<div class="description">
<p>日本語とこそこそ語を相互翻訳します。</p>
<p>こそこそ語(可逆)は、日本語に変換できますが、結果が長くなります。こそこそ語(非可逆)は、入力された文章に対して不変の結果となりますが、日本語に戻すことはできません。</p>
</div>
<div class="form row">
<div class="col s6" id="col_left">
<form>
<div class="input_head row" id="s_container" style="height: 60px;">
<div class="input-field col s5" id="s_from_container">
<select id="s_from">
<option value="ja">日本語</option>
<option value="koso1">こそこそ語(可逆)</option>
</select>
<label>翻訳元</label>
</div>
<div class="col s2" style="text-align: center;margin-top: 15px;" id="s_middle">
<span style="line-height: 1.5;">⇒</span>
</div>
<div class="input-field col s5" id="s_to_container">
<select id="s_to">
<option value="ja">日本語</option>
<option value="koso1" selected>こそこそ語(可逆)</option>
<option value="koso2">こそこそ語(非可逆)</option>
</select>
<label>翻訳先</label>
</div>
</div>
<div class="input-field">
<textarea id="original_text" class="materialize-textarea"></textarea>
<label for="original_text">翻訳元</label>
</div>
</form>
</div>
<div class="col s6" id="col_right">
<div style="margin-top: 80px;" id="r_container">
<div class="input-field">
<textarea id="result_text" class="materialize-textarea" readonly></textarea>
<label for="result_text" id="s_to_label">翻訳結果</label>
</div>
</div>
<button type="button" id="copy_btn" class="btn waves-effect waves-light blue darken-3">コピー</button>
</div>
</div>
</div>
<footer style="text-align: center;">Copyright © 2016-2021 mtripg6666tdr All Rights Reserved.</footer>
</div>
<script type="text/javascript" src="lib/materialize.min.js" defer></script>
</body>
</html>