polyphone.html 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <!DOCTYPE html>
  2. <html lang="zh-cn">
  3. <head>
  4. <meta charset="utf-8"/>
  5. <title>带词库识别多音字的汉字转拼音</title>
  6. <link rel="stylesheet" type="text/css" href="simple-input-method/simple-input-method.css">
  7. <style type="text/css">
  8. body{font-family: 'Microsoft Yahei'; font-size: 16px;}
  9. * {
  10. -webkit-box-sizing: border-box;
  11. -moz-box-sizing: border-box;
  12. box-sizing: border-box;
  13. }
  14. input[type="text"] {
  15. height: 34px;
  16. padding: 6px 12px;
  17. font-size: 14px;
  18. line-height: 1.42857143;
  19. color: #555;
  20. background-color: #fff;
  21. background-image: none;
  22. border: 1px solid #ccc;
  23. border-radius: 4px;
  24. box-shadow: inset 0 1px 1px rgba(0,0,0,.075);
  25. -webkit-transition: border-color ease-in-out .15s,-webkit-box-shadow ease-in-out .15s;
  26. transition: border-color ease-in-out .15s,box-shadow ease-in-out .15s;
  27. }
  28. input[type="text"]:focus {
  29. border-color: #66afe9;
  30. outline: 0;
  31. -webkit-box-shadow: inset 0 1px 1px rgba(0,0,0,.075),0 0 8px rgba(102,175,233,.6);
  32. box-shadow: inset 0 1px 1px rgba(0,0,0,.075),0 0 8px rgba(102,175,233,.6);
  33. }
  34. h2 > span {
  35. color: #B10000;
  36. font-size: 0.8em;
  37. }
  38. #test {margin-top: 10px;}
  39. .loading-tip{color: #00960A;margin-bottom: 10px;}
  40. p {max-width: 500px;background: #EEE;padding: 10px;}
  41. </style>
  42. </head>
  43. <body>
  44. <a href="http://blog.haoji.me/pinyinjs.html" target="_blank">返回原文</a>
  45. <a href="http://github.com/sxei/pinyinjs/" target="_blank">github地址</a>
  46. <a href="http://demo.haoji.me/pinyinjs/" target="_blank">返回上一页</a>
  47. <h2>带词库可以识别多音字的汉字转拼音:</h2>
  48. <p>说明:本页面需要加载900多kb的词库文件,所以不太适合web环境,这里仅仅是演示效果而已,并且由于没有使用分词所以效果也不一定非常好。非要识别多音字的话,推荐在服务端完成(借助一些分词工具效果会更好,比如<a href="https://github.com/fxsjy/jieba">jieba</a>),然后做成接口给前端调用。</p>
  49. <div class="loading-tip">正在加载词库,文件比较大,请耐心等待加载完毕再尝试。</div>
  50. <div>
  51. <span>输出类型:</span>
  52. <label><input type="radio" name="pinyin_type" value="0" checked/>带声调拼音</label>
  53. <label><input type="radio" name="pinyin_type" value="1"/>不带声调拼音</label>
  54. <label><input type="radio" name="pinyin_type" value="2"/>拼音首字母</label>
  55. </div>
  56. <input type="text" id="test" value="小明说长大后要去看长城!" placeholder="请随便输入一些中文"/>
  57. <h3>转换结果:</h3>
  58. <div id="result"></div>
  59. <script type="text/javascript" src="dict/pinyin_dict_withtone.js"></script>
  60. <script type="text/javascript" src="dict/pinyin_dict_polyphone.js"></script>
  61. <script type="text/javascript" src="pinyinUtil.js"></script>
  62. <script type="text/javascript">
  63. function getPinyin()
  64. {
  65. var value = document.getElementById('test').value;
  66. var type = document.querySelector('[name="pinyin_type"]:checked').value;
  67. var result = '';
  68. if(value)
  69. {
  70. switch(type)
  71. {
  72. case '0': result = pinyinUtil.getPinyin(value, ' ', true, true); break;
  73. case '1': result = pinyinUtil.getPinyin(value, ' ', false, true); break;
  74. case '2': result = pinyinUtil.getFirstLetter(value, true); break;
  75. default: break;
  76. }
  77. }
  78. var html = result;
  79. if(result instanceof Array)
  80. {
  81. html = '<ol>';
  82. result.forEach(function(val)
  83. {
  84. html += '<li>'+val+'</li>';
  85. });
  86. html += '</ol>';
  87. }
  88. document.getElementById('result').innerHTML = html;
  89. }
  90. document.getElementById('test').addEventListener('input', getPinyin);
  91. document.addEventListener('change', function(e)
  92. {
  93. if(e.target.name === 'pinyin_type')
  94. {
  95. getPinyin();
  96. }
  97. });
  98. getPinyin();
  99. document.querySelector('.loading-tip').innerHTML = 'JS加载完毕,现在你可以开始打字了!';
  100. </script>
  101. </body>
  102. </html>