-
Notifications
You must be signed in to change notification settings - Fork 0
/
kanji2hiragana.html
200 lines (187 loc) · 4.98 KB
/
kanji2hiragana.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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>kanji to hiragana</title>
<style>
.sentence {
width: 100%;
padding: 10px;
margin: 10px 0;
font-size: 20px;
box-sizing: border-box;
}
#result {
padding: 20px 0;
}
.flex-1 {
flex: 1;
}
.result-wrap,
.title {
display: flex;
}
textarea {
form-sizing: normal;
}
ruby {
text-decoration: underline;
margin-inline: 3px;
text-underline-offset: 3px;
}
#all-result li + li {
margin-top: 5px;
}
</style>
</head>
<body>
<h1>Kanji to Kana</h1>
<p>Enter a sentence to get the kana reading</p>
<a
href="https://github.com/lfl976/NihongoSlicer"
target="_blank"
rel="noopener noreferrer"
>github</a
>
<form>
<textarea
class="sentence"
type="text"
id="kana"
name="kana"
value="千葉放送局マスコットキャラクターのラッカ星人も登場"
></textarea>
<span
>Number of characters(<a
href="https://taku910.github.io/mecab/#nbest"
target="_blank"
>max512</a
>): </span
><span id="number"></span><br />
<button type="button" onclick="getkana()">Convert</button>
<button type="button" onclick="speak()">Speak</button>
</form>
<br />
<div class="title">
<div class="flex-1">
<h2>result</h2>
<button type="button" onclick="copy()">Copy with furigana</button>
<button type="button" onclick="copy(2)">Copy plain text</button>
<button type="button" onclick="copy(3)">Copy hiragana</button>
</div>
<div class="flex-1">
<h2 class="flex-1">convert list</h2>
</div>
<div class="flex-1">
<h2 class="flex-1">history list</h2>
</div>
</div>
<div class="result-wrap">
<div class="flex-1">
<div id="result"></div>
</div>
<div class="flex-1">
<ul id="all-result"></ul>
</div>
<div class="flex-1">
<div id="history"></div>
</div>
</div>
<script>
document.getElementById("kana").addEventListener("input", function () {
document.getElementById("number").innerHTML = this.value.length;
});
function copy(flag = 1) {
// 找到要复制的div
var originalDiv = document.querySelector("#result");
// 将div及其内部内容的outerHTML复制到剪贴板
navigator.clipboard
.writeText(
flag === 1
? originalDiv.outerHTML
: flag === 2
? originalDiv.dataset.setence
: originalDiv.dataset.hiragana
)
.then(function () {
console.log("HTML内容已成功复制到剪贴板");
})
.catch(function (err) {
console.error("复制失败:", err);
});
}
const resultDiv = document.getElementById("result");
const allResult = document.getElementById("all-result");
const historyDiv = document.getElementById("history");
function getkana() {
const kana = document.getElementById("kana").value;
if (kana) {
const params = new URLSearchParams();
params.append("text", kana);
fetch("http://127.0.0.1:5000/tokenize?" + params.toString())
.then((response) => response.json())
.then((data) => {
console.log(data);
const result = convertToRuby(data);
resultDiv.innerHTML = result;
resultDiv.dataset.setence = kana;
resultDiv.dataset.hiragana = data
.map((item) => item.reading)
.join(" ");
const li = document.createElement("li");
li.dataset.setence = kana;
li.addEventListener(
"click",
function (kana) {
speakText(kana);
}.bind(this, kana)
);
li.innerHTML = result;
allResult.appendChild(li);
const historyLi = document.createElement("li");
historyLi.textContent = kana;
historyDiv.appendChild(historyLi);
});
}
}
function isKana(char) {
// 平假名和片假名的 Unicode 范围
const hiraganaRange = /^[\u3040-\u309F]+$/;
const katakanaRange = /^[\u30A0-\u30FF]+$/;
return hiraganaRange.test(char) || katakanaRange.test(char);
}
function isPunctuation(character) {
return /^[\p{P}]+$/u.test(character);
}
function convertToRuby(data) {
let result = "";
data.forEach((item) => {
if (isKana(item.word) || isPunctuation(item.word)) {
result += item.word;
} else {
result += `<ruby>${item.word}<rp>(</rp><rt>${item.reading}</rt><rp>)</rp></ruby>`;
}
});
return result;
}
// 创建一个新的语音合成实例
const synth = window.speechSynthesis;
// 创建一个函数来朗读文本
function speakText(text, lang = "ja-JP") {
// 创建一个新的语音合成消息
const utterance = new SpeechSynthesisUtterance(text);
// 设置朗读的语言
utterance.lang = lang;
// 开始朗读文本
synth.speak(utterance);
}
function speak() {
const text = document.getElementById("kana").value;
if (text) {
speakText(text);
}
}
</script>
</body>
</html>