微信语音红包小程序开发如何提高精准度 红包小程…

2018-06-22 05:34:01来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

  公司最近开发的一个微信语音红包,就是前些时间比较火的包你说红包小程序。如何提高识别的精准度呢。

 在说精准度之前,先大概说下整个语音识别的开发流程。前面我有文章已经说到过了。具体我就不谈了。一笔带过。

  先是通过小程序前端调动语音录制功能拿到客户说的语音,比如mp3格式,然后通过百度的语音识别算法,转为文字。具体看百度语音识别的接口。地址http://yuyin.baidu.com/docs/asr/188

   返回的格式如下:

  1. // 成功返回
  2. {
  3. "err_no": 0,
  4. "err_msg": "success.",
  5. "corpus_no": "15984125203285346378",
  6. "sn": "481D633F-73BA-726F-49EF-8659ACCC2F3D",
  7. "result": ["北京天气"]
  8. }

  json格式的,取到他的文字来,然后和你的语音红包文字进行匹配,匹配对了,就可以抢到这个语音红包了。

  好了,问题来了,如果只用中文进行匹配,就会出现,比如我们说话的时候,我们打一个字,比如lianhe 会出现很多个词组,比如联合 连和 联和  莲河  那么具体是哪个呢?

   我们之前开发的第一个版本,我们就遇到这个坑了,当时一个客户是要连和,但是呢,百度识别一直给你识别的是联合。这就让人抓狂了。客户一直抢不到红包。但是我说是说对了呀。那怎么办呢?产品经理将问题提过来了。技术员,看看你怎么解决了?

   嗯,临危受命,我们想想技术怎么来解决吧。于是想到的方法就是两种:

1、将文字转为拼音,匹配这两个的拼音是否一致。

2、拼音可能会遇到南方人和北方人的区别,比如是否有后鼻音的问题。  beijing  和beijin   没有后鼻音的。那么就将拼音作为一个单元,比如一个词组,假设有10个字母,我们设置概率,只要80%的准确率就算答对了。

 

  使用上面这两个方法成功将这个问题解决了。所以重点就是文字怎么转为拼音了。大家可以去百度搜有相关的类库函数,直接调用就可以了。

  我这里也分享一个我自己在用的。大家需要可以直接复制使用。还不错哦。也是我百度搜的,别人写的一个类库。

 

  1 /**
  2  *+------------------------------------------------------
  3  *    PHP 汉字转拼音
  4  *+------------------------------------------------------
  5  *    使用方法:
  6  *      $py = new PinYin();
  7  *      echo $py->getAllPY("输出汉字所有拼音"); //shuchuhanzisuoyoupinyin
  8  *      echo $py->getFirstPY("输出汉字首拼音"); //schzspy
  9  *+------------------------------------------------------
 10  */
 11 class PinYin {
 12     private $pylist = array(
 13     'a'=>-20319,'ai'=>-20317,'an'=>-20304,'ang'=>-20295,'ao'=>-20292,
 14     'ba'=>-20283,'bai'=>-20265,'ban'=>-20257,'bang'=>-20242,'bao'=>-20230,'bei'=>-20051,'ben'=>-20036,'beng'=>-20032,'bi'=>-20026,'bian'=>-20002,'biao'=>-19990,'bie'=>-19986,'bin'=>-19982,'bing'=>-19976,'bo'=>-19805,'bu'=>-19784,
 15     'ca'=>-19775,'cai'=>-19774,'can'=>-19763,'cang'=>-19756,'cao'=>-19751,'ce'=>-19746,'ceng'=>-19741,'cha'=>-19739,'chai'=>-19728,'chan'=>-19725,'chang'=>-19715,'chao'=>-19540,'che'=>-19531,'chen'=>-19525,'cheng'=>-19515,'chi'=>-19500,'chong'=>-19484,'chou'=>-19479,'chu'=>-19467,'chuai'=>-19289,'chuan'=>-19288,'chuang'=>-19281,'chui'=>-19275,'chun'=>-19270,'chuo'=>-19263,'ci'=>-19261,'cong'=>-19249,'cou'=>-19243,'cu'=>-19242,'cuan'=>-19238,'cui'=>-19235,'cun'=>-19227,'cuo'=>-19224,
 16     'da'=>-19218,'dai'=>-19212,'dan'=>-19038,'dang'=>-19023,'dao'=>-19018,'de'=>-19006,'deng'=>-19003,'di'=>-18996,'dian'=>-18977,'diao'=>-18961,'die'=>-18952,'ding'=>-18783,'diu'=>-18774,'dong'=>-18773,'dou'=>-18763,'du'=>-18756,'duan'=>-18741,'dui'=>-18735,'dun'=>-18731,'duo'=>-18722,
 17     'e'=>-18710,'en'=>-18697,'er'=>-18696,
 18     'fa'=>-18526,'fan'=>-18518,'fang'=>-18501,'fei'=>-18490,'fen'=>-18478,'feng'=>-18463,'fo'=>-18448,'fou'=>-18447,'fu'=>-18446,
 19     'ga'=>-18239,'gai'=>-18237,'gan'=>-18231,'gang'=>-18220,'gao'=>-18211,'ge'=>-18201,'gei'=>-18184,'gen'=>-18183,'geng'=>-18181,'gong'=>-18012,'gou'=>-17997,'gu'=>-17988,'gua'=>-17970,'guai'=>-17964,'guan'=>-17961,'guang'=>-17950,'gui'=>-17947,
 20 'gun'=>-17931,'guo'=>-17928,
 21     'ha'=>-17922,'hai'=>-17759,'han'=>-17752,'hang'=>-17733,'hao'=>-17730,'he'=>-17721,'hei'=>-17703,'hen'=>-17701,'heng'=>-17697,'hong'=>-17692,'hou'=>-17683,'hu'=>-17676,'hua'=>-17496,'huai'=>-17487,'huan'=>-17482,'huang'=>-17468,'hui'=>-17454,
 22 'hun'=>-17433,'huo'=>-17427,
 23     'ji'=>-17417,'jia'=>-17202,'jian'=>-17185,'jiang'=>-16983,'jiao'=>-16970,'jie'=>-16942,'jin'=>-16915,'jing'=>-16733,'jiong'=>-16708,'jiu'=>-16706,'ju'=>-16689,'juan'=>-16664,'jue'=>-16657,'jun'=>-16647,
 24     'ka'=>-16474,'kai'=>-16470,'kan'=>-16465,'kang'=>-16459,'kao'=>-16452,'ke'=>-16448,'ken'=>-16433,'keng'=>-16429,'kong'=>-16427,'kou'=>-16423,'ku'=>-16419,'kua'=>-16412,'kuai'=>-16407,'kuan'=>-16403,'kuang'=>-16401,'kui'=>-16393,'kun'=>-16220,'kuo'=>-16216,
 25     'la'=>-16212,'lai'=>-16205,'lan'=>-16202,'lang'=>-16187,'lao'=>-16180,'le'=>-16171,'lei'=>-16169,'leng'=>-16158,'li'=>-16155,'lia'=>-15959,'lian'=>-15958,'liang'=>-15944,'liao'=>-15933,'lie'=>-15920,'lin'=>-15915,'ling'=>-15903,'liu'=>-15889,
 26 'long'=>-15878,'lou'=>-15707,'lu'=>-15701,'lv'=>-15681,'luan'=>-15667,'lue'=>-15661,'lun'=>-15659,'luo'=>-15652,
 27     'ma'=>-15640,'mai'=>-15631,'man'=>-15625,'mang'=>-15454,'mao'=>-15448,'me'=>-15436,'mei'=>-15435,'men'=>-15419,'meng'=>-15416,'mi'=>-15408,'mian'=>-15394,'miao'=>-15385,'mie'=>-15377,'min'=>-15375,'ming'=>-15369,'miu'=>-15363,'mo'=>-15362,'mou'=>-15183,'mu'=>-15180,
 28     'na'=>-15165,'nai'=>-15158,'nan'=>-15153,'nang'=>-15150,'nao'=>-15149,'ne'=>-15144,'nei'=>-15143,'nen'=>-15141,'neng'=>-15140,'ni'=>-15139,'nian'=>-15128,'niang'=>-15121,'niao'=>-15119,'nie'=>-15117,'nin'=>-15110,'ning'=>-15109,'niu'=>-14941,
 29 'nong'=>-14937,'nu'=>-14933,'nv'=>-14930,'nuan'=>-14929,'nue'=>-14928,'nuo'=>-14926,
 30     'o'=>-14922,'ou'=>-14921,
 31     'pa'=>-14914,'pai'=>-14908,'pan'=>-14902,'pang'=>-14894,'pao'=>-14889,'pei'=>-14882,'pen'=>-14873,'peng'=>-14871,'pi'=>-14857,'pian'=>-14678,'piao'=>-14674,'pie'=>-14670,'pin'=>-14668,'ping'=>-14663,'po'=>-14654,'pu'=>-14645,
 32     'qi'=>-14630,'qia'=>-14594,'qian'=>-14429,'qiang'=>-14407,'qiao'=>-14399,'qie'=>-14384,'qin'=>-14379,'qing'=>-14368,'qiong'=>-14355,'qiu'=>-14353,'qu'=>-14345,'quan'=>-14170,'que'=>-14159,'qun'=>-14151,
 33     'ran'=>-14149,'rang'=>-14145,'rao'=>-14140,'re'=>-14137,'ren'=>-14135,'reng'=>-14125,'ri'=>-14123,'rong'=>-14122,'rou'=>-14112,'ru'=>-14109,'ruan'=>-14099,'rui'=>-14097,'run'=>-14094,'ruo'=>-14092,
 34     'sa'=>-14090,'sai'=>-14087,'san'=>-14083,'sang'=>-13917,'sao'=>-13914,'se'=>-13910,'sen'=>-13907,'seng'=>-13906,'sha'=>-13905,'shai'=>-13896,'shan'=>-13894,'shang'=>-13878,'shao'=>-13870,'she'=>-13859,'shen'=>-13847,'sheng'=>-13831,'shi'=>-13658,'shou'=>-13611,'shu'=>-13601,'shua'=>-13406,'shuai'=>-13404,'shuan'=>-13400,'shuang'=>-13398,'shui'=>-13395,'shun'=>-13391,'shuo'=>-13387,'si'=>-13383,'song'=>-13367,'sou'=>-13359,'su'=>-13356,'suan'=>-13343,'sui'=>-13340,'sun'=>-13329,'suo'=>-13326,
 35     'ta'=>-13318,'tai'=>-13147,'tan'=>-13138,'tang'=>-13120,'tao'=>-13107,'te'=>-13096,'teng'=>-13095,'ti'=>-13091,'tian'=>-13076,'tiao'=>-13068,'tie'=>-13063,'ting'=>-13060,'tong'=>-12888,'tou'=>-12875,'tu'=>-12871,'tuan'=>-12860,'tui'=>-12858,'tun'=>-12852,'tuo'=>-12849,
 36     'wa'=>-12838,'wai'=>-12831,'wan'=>-12829,'wang'=>-12812,'wei'=>-12802,'wen'=>-12607,'weng'=>-12597,'wo'=>-12594,'wu'=>-12585,
 37     'xi'=>-12556,'xia'=>-12359,'xian'=>-12346,'xiang'=>-12320,'xiao'=>-12300,'xie'=>-12120,'xin'=>-12099,'xing'=>-12089,'xiong'=>-12074,'xiu'=>-12067,'xu'=>-12058,'xuan'=>-12039,'xue'=>-11867,'xun'=>-11861,
 38     'ya'=>-11847,'yan'=>-11831,'yang'=>-11798,'yao'=>-11781,'ye'=>-11604,'yi'=>-11589,'yin'=>-11536,'ying'=>-11358,'yo'=>-11340,'yong'=>-11339,'you'=>-11324,'yu'=>-11303,'yuan'=>-11097,'yue'=>-11077,'yun'=>-11067,
 39     'za'=>-11055,'zai'=>-11052,'zan'=>-11045,'zang'=>-11041,'zao'=>-11038,'ze'=>-11024,'zei'=>-11020,'zen'=>-11019,'zeng'=>-11018,'zha'=>-11014,'zhai'=>-10838,'zhan'=>-10832,'zhang'=>-10815,'zhao'=>-10800,'zhe'=>-10790,'zhen'=>-10780,'zheng'=>-10764,'zhi'=>-10587,'zhong'=>-10544,'zhou'=>-10533,'zhu'=>-10519,'zhua'=>-10331,'zhuai'=>-10329,'zhuan'=>-10328,'zhuang'=>-10322,'zhui'=>-10315,'zhun'=>-10309,'zhuo'=>-10307,'zi'=>-10296,'zong'=>-10281,'zou'=>-10274,'zu'=>-10270,'zuan'=>-10262,
 40 'zui'=>-10260,'zun'=>-10256,'zuo'=>-10254
 41     );
 42     
 43     
 44     /**
 45      *+---------------------------------------------------------------------------------
 46      *    获取全部拼音
 47      *+---------------------------------------------------------------------------------
 48      *    @return string 
 49      *+---------------------------------------------------------------------------------
 50      */
 51     public function getAllPY($chinese, $delimiter = '', $length = 0,$charset='utf-8') {
 52         if($charset != 'gb2312') $chinese = $this->_U2_Utf8_Gb($chinese);
 53        $py = $this->zh_to_pys($chinese, $delimiter);
 54        if($length) {
 55            $py = substr($py, 0, $length);
 56        }
 57        return $py;
 58     }
 59     
 60     /**
 61      *+--------------------------------------------------------------------------------
 62      *    获取拼音首字母
 63      *+--------------------------------------------------------------------------------
 64      *    @return string
 65      *+--------------------------------------------------------------------------------
 66      */
 67     public function getFirstPY($chinese,$charset='utf-8'){
 68       if($charset != 'gb2312') $chinese = $this->_U2_Utf8_Gb($chinese);
 69        $result = '' ;
 70        for ($i=0; $i<strlen($chinese); $i++) {
 71            $p = ord(substr($chinese,$i,1));
 72            if ($p>160) {
 73               $q = ord(substr($chinese,++$i,1));
 74               $p = $p*256 + $q - 65536;
 75            }
 76            $result .= substr($this->zh_to_py($p),0,1);
 77        }
 78        return $result ;
 79     }
 80   
 81       public function _U2_Utf8_Gb($_C){
 82             $_String = '';
 83             if($_C < 0x80) $_String .= $_C;
 84             elseif($_C < 0x800)
 85             {
 86             $_String .= chr(0xC0 | $_C>>6);
 87             $_String .= chr(0x80 | $_C & 0x3F);
 88             }elseif($_C < 0x10000){
 89             $_String .= chr(0xE0 | $_C>>12);
 90             $_String .= chr(0x80 | $_C>>6 & 0x3F);
 91             $_String .= chr(0x80 | $_C & 0x3F);
 92             } elseif($_C < 0x200000) {
 93             $_String .= chr(0xF0 | $_C>>18);
 94             $_String .= chr(0x80 | $_C>>12 & 0x3F);
 95             $_String .= chr(0x80 | $_C>>6 & 0x3F);
 96             $_String .= chr(0x80 | $_C & 0x3F);
 97             }
 98             return iconv('UTF-8', 'GB2312', $_String);
 99     }
100  
101     private function zh_to_py($num, $blank = '') {
102        if($num>0 && $num<160 ) {
103            return chr($num);
104        } elseif ($num<-20319||$num>-10247) {
105            return $blank;
106        } else {
107            foreach ($this->pylist as $py => $code) {
108               if($code > $num) break;
109               $result = $py;
110            }
111            return $result;
112        }
113     }
114       
115     private function zh_to_pys($chinese, $delimiter = ' ', $first=0){
116        $result = array();
117        for($i=0; $i<strlen($chinese); $i++) {
118            $p = ord(substr($chinese,$i,1));
119            if($p>160) {
120               $q = ord(substr($chinese,++$i,1));
121               $p = $p*256 + $q - 65536;
122            }
123            $result[] = $this->zh_to_py($p);
124            if ($first) {
125               return $result[0];
126            }
127        }
128        return implode($delimiter, $result);
129     }
130 }

 

 

 

 

 

     好了,今天就给大家分享到这里了。  我,秋峰,phper,目前自创业,做项目系统开发,php后端加小程序前端结合  今天就给大家分享到这里,希望对大家有所帮助。欢迎交流  大家相互学习 共同提高 我的 微信号:qiufeng2999

 

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:PHP二维数组排序

下一篇:php字符串反转