forked from sxei/pinyinjs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
polyphone.html
106 lines (102 loc) · 3.76 KB
/
polyphone.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
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="utf-8"/>
<title>带词库识别多音字的汉字转拼音</title>
<link rel="stylesheet" type="text/css" href="simple-input-method/simple-input-method.css">
<style type="text/css">
body{font-family: 'Microsoft Yahei'; font-size: 16px;}
* {
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
}
input[type="text"] {
height: 34px;
padding: 6px 12px;
font-size: 14px;
line-height: 1.42857143;
color: #555;
background-color: #fff;
background-image: none;
border: 1px solid #ccc;
border-radius: 4px;
box-shadow: inset 0 1px 1px rgba(0,0,0,.075);
-webkit-transition: border-color ease-in-out .15s,-webkit-box-shadow ease-in-out .15s;
transition: border-color ease-in-out .15s,box-shadow ease-in-out .15s;
}
input[type="text"]:focus {
border-color: #66afe9;
outline: 0;
-webkit-box-shadow: inset 0 1px 1px rgba(0,0,0,.075),0 0 8px rgba(102,175,233,.6);
box-shadow: inset 0 1px 1px rgba(0,0,0,.075),0 0 8px rgba(102,175,233,.6);
}
h2 > span {
color: #B10000;
font-size: 0.8em;
}
#test {margin-top: 10px;}
.loading-tip{color: #00960A;margin-bottom: 10px;}
p {max-width: 500px;background: #EEE;padding: 10px;}
</style>
</head>
<body>
<a href="http://blog.liuxianan.com/pinyinjs.html" target="_blank">返回原文</a>
<a href="http://github.com/liuxianan/pinyinjs/" target="_blank">github地址</a>
<a href="http://demo.liuxianan.com/pinyinjs/" target="_blank">返回上一页</a>
<h2>带词库可以识别多音字的汉字转拼音:</h2>
<p>说明:本页面需要加载900多kb的词库文件,所以不太适合web环境,这里仅仅是演示效果而已,并且由于没有使用分词所以效果也不一定非常好。非要识别多音字的话,推荐在服务端完成(借助一些分词工具效果会更好,比如<a href="https://github.com/fxsjy/jieba">jieba</a>),然后做成接口给前端调用。</p>
<div class="loading-tip">正在加载词库,文件比较大,请耐心等待加载完毕再尝试。</div>
<div>
<span>输出类型:</span>
<label><input type="radio" name="pinyin_type" value="0" checked/>带声调拼音</label>
<label><input type="radio" name="pinyin_type" value="1"/>不带声调拼音</label>
<label><input type="radio" name="pinyin_type" value="2"/>拼音首字母</label>
</div>
<input type="text" id="test" value="小明说长大后要去看长城!" placeholder="请随便输入一些中文"/>
<h3>转换结果:</h3>
<div id="result"></div>
<script type="text/javascript" src="dict/pinyin_dict_withtone.js"></script>
<script type="text/javascript" src="dict/pinyin_dict_polyphone.js"></script>
<script type="text/javascript" src="pinyinUtil.js"></script>
<script type="text/javascript">
function getPinyin()
{
var value = document.getElementById('test').value;
var type = document.querySelector('[name="pinyin_type"]:checked').value;
var result = '';
if(value)
{
switch(type)
{
case '0': result = pinyinUtil.getPinyin(value, ' ', true, true); break;
case '1': result = pinyinUtil.getPinyin(value, ' ', false, true); break;
case '2': result = pinyinUtil.getFirstLetter(value, true); break;
default: break;
}
}
var html = result;
if(result instanceof Array)
{
html = '<ol>';
result.forEach(function(val)
{
html += '<li>'+val+'</li>';
});
html += '</ol>';
}
document.getElementById('result').innerHTML = html;
}
document.getElementById('test').addEventListener('input', getPinyin);
document.addEventListener('change', function(e)
{
if(e.target.name === 'pinyin_type')
{
getPinyin();
}
});
getPinyin();
document.querySelector('.loading-tip').innerHTML = 'JS加载完毕,现在你可以开始打字了!';
</script>
</body>
</html>