编程示例:汉字生成盲文的翻译器

1 翻译器的意义

我国有视障人士2000多万人,需要把大量的文章与书籍转换成盲文书。

2 翻译器的开发原理

根据汉语与盲文符号的对照表,以此为基础,进行汉字与盲文之间的转换。

如下的两个图片是汉语与盲文符号的对照表。

3 翻译器的开发示例代码

html 复制代码
<HTML>
<HEAD>
<TITLE> 汉字生成盲文第一版 </TITLE>
<meta charset="gb2312">
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
<script src='zh_character_to_pinyin_character_with_tone_map.js'></script>
</HEAD>

<BODY onkeypress='key_operation()'>

<p id="p1">生成盲文的方法</p>

<p>在左边的方框内,输入一段话,再点击生成盲文的按钮,在画布上就生成了盲文</p>
<textarea id='txt5' rows="5" cols="140"></textarea>
<input type='button' value='生成盲文' onclick='print_braille()'></input>

<textarea id='txt6' rows="5" cols="140"></textarea>

<canvas id="myCanvas" width="600" height="300"  style="background-color:blue;border: 10px yellow solid" ></canvas>
<p>
<input type='button' value='q' onclick='input("q")'></input>
<input type='button' value='w' onclick='input("w")'></input>
<input type='button' value='e' onclick='input("e")'></input>
<input type='button' value='r' onclick='input("r")'></input>
<input type='button' value='t' onclick='input("t")'></input>
<input type='button' value='y' onclick='input("y")'></input>
<input type='button' value='u' onclick='input("u")'></input>
<input type='button' value='i' onclick='input("i")'></input>
<input type='button' value='o' onclick='input("o")'></input>
<input type='button' value='p' onclick='input("p")'></input>
<input type='button' value='[' onclick='input("[")'></input>
<input type='button' value=']' onclick='input("]")'></input>
<input type='button' value='\' onclick='input("\")'></input>
</p>
<p>

<input type='button' value='a' onclick='input("a")'></input>
<input type='button' value='s' onclick='input("s")'></input>
<input type='button' value='d' onclick='input("d")'></input>
<input type='button' value='f' onclick='input("f")'></input>
<input type='button' value='g' onclick='input("g")'></input>
<input type='button' value='h' onclick='input("h")'></input>
<input type='button' value='j' onclick='input("j")'></input>
<input type='button' value='k' onclick='input("k")'></input>
<input type='button' value='l' onclick='input("l")'></input>
<input type='button' value=';' onclick='input(";")'></input>
<input type='button' value='' onclick='input("")'></input>
<input type='button' value='enter' onclick='input("10")'></input>
</p>
<p>  
<input type='button' value='z' onclick='input("z")'></input>
<input type='button' value='x' onclick='input("x")'></input>
<input type='button' value='c' onclick='input("c")'></input>
<input type='button' value='v' onclick='input("v")'></input>
<input type='button' value='b' onclick='input("b")'></input>
<input type='button' value='n' onclick='input("n")'></input>
<input type='button' value='m' onclick='input("m")'></input>
<input type='button' value=',' onclick='input(",")'></input>
<input type='button' value='.' onclick='input(".")'></input>
<input type='button' value='/' onclick='input("/")'></input>
</p>
<script>
var e=document.getElementById("myCanvas");
var cxt=e.getContext("2d");
cxt.fillStyle="#FF0000";
cxt.fillRect(0,0,50,5);

var first_sound=[[[[1,0],[1,0],[0,0]],'b'],
[[[1,1],[1,0],[1,0]],'p'],
[[[1,1],[0,0],[1,0]],'m'],
[[[1,1],[1,0],[0,0]],'f'],
[[[1,1],[0,1],[0,0]],'d'],
[[[0,1],[1,1],[1,0]],'t'],
[[[1,1],[0,1],[1,0]],'n'],
[[[1,0],[1,0],[1,0]],'l'],
[[[1,1],[1,1],[0,0]],'g'],
[[[1,1],[1,1],[0,0]],'j'],
[[[1,0],[0,0],[1,0]],'k'],
[[[1,0],[0,0],[1,0]],'q'],
[[[1,0],[1,1],[0,0]],'h'],
[[[1,0],[1,1],[0,0]],'x'],
[[[0,1],[0,0],[1,0]],'zh'],
[[[0,1],[0,0],[1,0]],'zhi'],
[[[1,1],[1,1],[1,0]],'ch'],
[[[1,1],[1,1],[1,0]],'chi'],
[[[1,0],[0,1],[0,1]],'sh'],
[[[1,0],[0,1],[0,1]],'shi'],
[[[0,1],[1,1],[0,0]],'r'],
[[[0,1],[1,1],[0,0]],'ri'],
[[[1,0],[0,1],[1,1]],'z'],
[[[1,0],[0,1],[1,1]],'zi'],
[[[1,1],[0,0],[0,0]],'c'],
[[[1,1],[0,0],[0,0]],'ci'],
[[[0,1],[1,0],[1,0]],'s'],
[[[0,1],[1,0],[1,0]],'si']];

var last_sound=[[[[0,0],[0,1],[1,0]],'a'],
[[[0,0],[1,0],[0,1]],'o'],
[[[0,0],[1,0],[0,1]],'e'],
[[[0,1],[1,0],[0,0]],'yi'],
[[[0,1],[1,0],[0,0]],'i'],
[[[1,0],[0,0],[1,1]],'wu'],
[[[1,0],[0,0],[1,1]],'u'],
[[[0,1],[0,0],[1,1]],'yu'],
[[[0,1],[0,0],[1,1]],'v'],
[[[1,0],[1,1],[1,0]],'er'],
[[[0,1],[1,0],[0,1]],'ai'],
[[[0,0],[1,1],[1,0]],'ao'],
[[[0,1],[1,0],[1,1]],'ei'],
[[[1,0],[1,1],[1,1]],'ou'],
[[[1,1],[1,0],[0,1]],'ya'],
[[[1,1],[1,0],[0,1]],'ia'],
[[[0,1],[0,1],[1,0]],'yao'],
[[[0,1],[0,1],[1,0]],'iao'],
[[[1,0],[0,1],[0,0]],'ye'],
[[[1,0],[0,1],[0,0]],'ie'],
[[[1,0],[1,1],[0,1]],'you'],
[[[1,0],[1,1],[0,1]],'iou'],
[[[1,1],[1,1],[1,1]],'wa'],
[[[1,1],[1,1],[1,1]],'ua'],
[[[1,1],[0,1],[1,1]],'wai'],
[[[1,1],[0,1],[1,1]],'uai'],
[[[0,1],[1,1],[0,1]],'wei'],
[[[0,1],[1,1],[0,1]],'uei'],
[[[1,0],[0,1],[1,0]],'wo'],
[[[1,0],[0,1],[1,0]],'uo'],
[[[0,1],[1,1],[1,1]],'yue'],
[[[0,1],[1,1],[1,1]],'ve'],
[[[1,0],[1,0],[1,1]],'an'],
[[[0,0],[1,0],[1,1]],'ang'],
[[[0,0],[0,1],[1,1]],'en'],
[[[0,1],[0,1],[1,1]],'eng'],
[[[1,1],[0,0],[0,1]],'yan'],
[[[1,1],[0,0],[0,1]],'ian'],
[[[1,1],[0,0],[1,1]],'yang'],
[[[1,1],[0,0],[1,1]],'iang'],
[[[1,0],[1,0],[0,1]],'yin'],
[[[1,0],[1,0],[0,1]],'in'],
[[[1,0],[0,0],[0,1]],'ying'],
[[[1,0],[0,0],[0,1]],'ing'],
[[[1,1],[1,1],[0,1]],'wan'],
[[[1,1],[1,1],[0,1]],'uan'],
[[[0,0],[1,1],[1,1]],'wang'],
[[[0,0],[1,1],[1,1]],'uang'],
[[[0,0],[1,1],[0,0]],'wen'],
[[[0,0],[1,1],[0,0]],'uen'],
[[[0,0],[1,1],[0,1]],'weng'],
[[[0,0],[1,1],[0,1]],'ong'],
[[[1,1],[1,0],[1,1]],'yuan'],
[[[1,1],[1,0],[1,1]],'van'],
[[[0,1],[0,1],[0,1]],'yun'],
[[[0,1],[0,1],[0,1]],'vn'],
[[[1,1],[0,1],[0,1]],'yong'],
[[[1,1],[0,1],[0,1]],'iong']
];

var tone_number=[32,8,2,10,0,0,0];
var number=[28,32,40,48,52,36,56,60,44,24];
var english_character=[32,40,48,52,36,56,60,44,24,28,34,42,50,54,38,58,62,46,26,30,35,43,29,51,55,39];

var meta_letters=['a','o','e','i','u','v'];

var ch_letters=[[257,225,462,224],[333,243,466,242],[275,233,283,232],[299,237,464,236],[363,250,468,249],[470,472,474,476]];

var braille_array=[];

//translate_en_letters_to_ch_letters('jiong3')      jiong3   ---->    jǐong
function translate_en_letters_to_ch_letters(str)
{
var position_A=-1;
var position_B=-1;

      for(var i=0;i<meta_letters.length;i++)
      {
            position_A=str.indexOf(meta_letters[i]);
                if(position_A>=0)
                    {if  (i==3)
                         { position_B=str.indexOf(meta_letters[i+1]);
                               if(position_B<position_A)
                                     {break;}
                               else 
                                     {position_B=position_A;
                                       i=i+1;
                                      break;}
                         }
                     else {break;}
                    }         
      }
     var re_source_letter=meta_letters[i];
    var tone=parseInt(str.charCodeAt(str.length-1),10)-48;
     if(tone>0&&tone<=9)
      {
    var re_target_letter=String.fromCharCode(ch_letters[i][tone-1]);
     str=str.replace(re_source_letter,re_target_letter);
     str=str.substring(0,str.length-1);
     }
   return str;
}
/*function  test()
{
document.getElementById("txt4").innerText=get_pinyin_code_from_quwei_code(document.getElementById("txt3").innerText);
}*/


function getChsAscii(str)
{
  var high=Math.floor(str/100);
  var low=parseInt(str%100,10);
  return high*256+low+160*257-65536;
}

function input(val)
{
   if(val>='a'&&val<='z')
     {
	 document.getElementById("txt3").innerText=document.getElementById("txt3").innerText+val;
     }
   else if (val=='10')
     {
	   document.getElementById("txt4").innerText=get_pinyin_code_from_quwei_code(document.getElementById("txt3").innerText);
	   document.getElementById("txt3").innerText='';
     }
}
var key_buffer='';
function key_operation()
{

var val=event.charCode;
   if(val>=97&&val<=122)
     {
	 key_buffer=key_buffer+event.char;
	 }
   else if (val==13)
                  {
	   document.getElementById("txt4").innerText=get_pinyin_code_from_quwei_code(key_buffer);
	   key_buffer='';
	 }
}


function getvalue(val){
 if (val=='') {return document.getElementById("txt5").value;}
 else {return val;}
}
/*
function test()
{
    var cc=getvalue(document.getElementById("txt5").innerText);
   var str='';
   var ch_str='';
   var s=0;
   var count=0;

    for(var i=0;i<pydic.length;i++){
         if(pydic.charCodeAt(i)>660){
             str+=pydic.charAt(i)+",";
         }
    }
	document.getElementById("txt6").value=str;//get_sound(cc);//
    document.getElementById("txt6").innerText=str;//result;
}*/

//get_sound_part('sheng1')----> sh eng 1
function get_sound_part(cc)
{
   var first_sound_ch="bpmfdtnlgjkqhx";
   var last_sound_ch="wyaeiou";
   var result_first='';
   var result_last='';
   var result_num='';
   var str='';
   var ch_str='';
   var s=0;
   var count=0;
  var i=0;

         if(first_sound_ch.indexOf(cc.charAt(i))!=-1){
             s=1;
			 result_first=cc.charAt(i);
         }
		 else if(cc.charAt(i)=='z'||cc.charAt(i)=='c'||cc.charAt(i)=='s')
		 {
		    if(cc.charAt(num_add(i,1))=='h')
			{ 
			  if(cc.charAt(num_add(i,2))=='i')
	        	{result_first=cc.charAt(i)+cc.charAt(num_add(i,1))+cc.charAt(num_add(i,2));s=3;}
			  else 
			    {result_first=cc.charAt(i)+cc.charAt(num_add(i,1));s=2;}
			}
			else if(cc.charAt(num_add(i,1))=='i')
			{result_first=cc.charAt(i)+cc.charAt(num_add(i,1));s=2;}
			else 
			{result_first=cc.charAt(i);s=1;}
		 }
		 else if(cc.charAt(i)=='r') 
		 {
		    if(cc.charAt(num_add(i,1))=='i')
			{result_first=cc.charAt(i)+cc.charAt(num_add(i,1));s=2;}
			else 
			{result_first=cc.charAt(i);s=1;}
		 }
		 else if(last_sound_ch.indexOf(cc.charAt(i))!=-1)
		 {result_first='';s=0;}

   for(var i=s;i<cc.length;i++)
   {
       if(cc.charCodeAt(i)>64)
	   {
	      result_last+=cc.charAt(i);
	   }
	   else 
	   {
	      result_num=cc.charAt(i);
	   }
   }

   var front=find_code(first_sound,result_first);
   var mid=find_code(last_sound,result_last);
   var last=tone_number[result_num-1];
    braille_array.push(front);
    braille_array.push(mid);
    braille_array.push(last);

   return  front+ ' ' +mid+' '+last;
}

function print_braille()
{
    var cc=getvalue(document.getElementById("txt5").innerText);
   var str='';
   var single_str='';
   var ch_str='';
   var s=0;
   var count=0;
   braille_array=[];

    for(var i=0;i<cc.length;i++){
  
         if(pydic.indexOf(cc.charAt(i))!=-1&&cc.charCodeAt(i)>200){
             s=1;
			 single_str='';
             while(pydic.charAt(pydic.indexOf(cc.charAt(i))+s)!=","){
                 single_str+=pydic.charAt(pydic.indexOf(cc.charAt(i))+s);
                 s++;
             }
             
			 str+=get_sound_part(single_str); //进行了音节分离
             str+=" ";
			 ch_str+=cc.charAt(i)+times2(' ',s-2);
			 count+=s;
         }
		 else if (cc.charCodeAt(i)==13||cc.charCodeAt(i)==10||count>70){
		   str+=cc.charAt(i)+String.fromCharCode(13)+String.fromCharCode(10)+ch_str+String.fromCharCode(13)+String.fromCharCode(10);
		   ch_str='';
		   count=0;
		 }
         else{
             str+=cc.charAt(i);
			 ch_str+=cc.charAt(i);
			 count+=1;
         }
     }
	 make_braille(braille_array);
	document.getElementById("txt6").value=str;//get_sound(cc);//
    document.getElementById("txt6").innerText=str;//result;
}

//
function make_braille(arr)
{
 // var arr=[39,25,10,39,38,2,40,49,32,52,9,0,56,11,32,34,15,10,54,27,10,37,35,32,28,35,10,0,24,32,52,61,10,44,63,10];
  var left=100;
  var top=20;
  var colnum=0;
  var rownum=0;
  for (var i=0;i<arr.length;i++)
  {  if(i>20) 
     {rownum=1;
	  colnum=i-20;}
	  else 
	  {rownum=0;
	  colnum=i;}
      print_a_character(left,top,colnum,rownum,arr[i]);
  }
   
  
}
function print_a_character(left,top,colnum,rownum,num)
{
    var res=num_to_array(num);
    for(var i=0;i<res.length;i++)
    {
      for(var j=0;j<res[i].length;j++)
	  {
	   if(res[i][j]==1)
	   {drawCircleWithNative(left+1+j*10+colnum*24,top+1+i*10+rownum*34,4,4,0,360);}
	   else 
	   {drawCircleWithNativeEmpty(left+1+j*10+colnum*24,top+1+i*10+rownum*34,4,4,0,360);}
	  }
    }
}
//get_sound('声') ---->sheng1
/*function get_sound(character)
{
   if(character=='声') 
   {return 'sheng1';}
   else {return 'yin';}
}*/

// times2('a',2)  --->'aa'
 function times2(str, num){
      return new Array(num+1).join(str);
    }
function num_add(a,b)
{
   return parseInt(a,10)+parseInt(b,10);
}

//find_code(first_sound_str,'b')---->40
function find_code(arr,str)
{  var result=0;
   for(var i=0;i<arr.length;i++)
   {
      if(arr[i][1]==str)
	  {
	     result=array_to_num(arr[i][0]);
		 break;
	  }
   }
   return result;
}
//array_to_num([[1,0],[1,0],[0,0]]) --->40
function array_to_num(arr)
{ var result=0;
   for(var i=0;i<arr.length;i++)
    {
	  for(var j=0;j<arr[i].length;j++)
	  {result=result*2+arr[i][j];
	  }
	}
	return result;
}

//num_to_array(40)---->[[1,0],[1,0],[0,0]]
function num_to_array(num)
{ var temp=0;
   var result=[[0,0],[0,0],[0,0]];
   for(var i=result.length-1;i>=0;i--)
    {
	   for(var j=result[i].length-1;j>=0;j--)
	   {
	      temp=num%2;
		  num=(num-temp)/2;
		  result[i][j]=temp;
	   }
	}
	return result;
}

function drawCircleWithNative(cx,cy,ra,rb,start,end)
{
   cxt.fillStyle='black';
   cxt.strokeStyle='pink';
   cxt.lineWidth=1;
   cxt.beginPath();
   cxt.arc(cx,cy,ra,0,Math.PI*2);
   cxt.closePath();
   cxt.fill();
   cxt.stroke();
}

function drawCircleWithNativeEmpty(cx,cy,ra,rb,start,end)
{
   cxt.fillStyle='white';
   cxt.strokeStyle='pink';
   cxt.lineWidth=1;
   cxt.beginPath();
   cxt.arc(cx,cy,ra,0,Math.PI*2);
   cxt.closePath();
   cxt.fill();
   cxt.stroke();
}

</script>
</BODY>
</HTML>

4 翻译器的界面示例与执行示例结果

相关推荐
青灯文案12 分钟前
前端 HTTP 请求由 Nginx 反向代理和 API 网关到后端服务的流程
前端·nginx·http
m0_748254887 分钟前
DataX3.0+DataX-Web部署分布式可视化ETL系统
前端·分布式·etl
ZJ_.19 分钟前
WPSJS:让 WPS 办公与 JavaScript 完美联动
开发语言·前端·javascript·vscode·ecmascript·wps
GIS开发特训营23 分钟前
Vue零基础教程|从前端框架到GIS开发系列课程(七)响应式系统介绍
前端·vue.js·前端框架·gis开发·webgis·三维gis
Cachel wood1 小时前
python round四舍五入和decimal库精确四舍五入
java·linux·前端·数据库·vue.js·python·前端框架
学代码的小前端1 小时前
0基础学前端-----CSS DAY9
前端·css
joan_851 小时前
layui表格templet图片渲染--模板字符串和字符串拼接
前端·javascript·layui
m0_748236111 小时前
Calcite Web 项目常见问题解决方案
开发语言·前端·rust
Watermelo6172 小时前
详解js柯里化原理及用法,探究柯里化在Redux Selector 的场景模拟、构建复杂的数据流管道、优化深度嵌套函数中的精妙应用
开发语言·前端·javascript·算法·数据挖掘·数据分析·ecmascript
m0_748248942 小时前
HTML5系列(11)-- Web 无障碍开发指南
前端·html·html5