PostgreSQL汉字转拼音

标签

PostgreSQL , 拼音 , 汉字转拼音


背景

在有些应用中,可能会有对拼音搜索、拼音首字母搜索、中文搜索共存的需求。在PostgreSQL中如何实现这个需求呢?

关键是函数转拼音和首字母,方法很简单,将映射关系存入数据库。创建一个函数来转换。

《PostgreSQL汉字转拼音或拼音首字母的应用》

映射文件请到以上文章的末尾下载。

方法

1、创建映射表

create table pinyin (hz varchar(1), py varchar(6), zm varchar(1));  

create index idx_pinyin_hz on pinyin(hz);  

create unique index idx_pinyin_hz_py on pinyin(hz, py);

2、写入一些测试映射,仅供演示用。映射文件请到以上文章的末尾下载。

test=# insert into pinyin values ('你','ni','n');
INSERT 0 1
test=# insert into pinyin values ('好','hao','h');
INSERT 0 1

3、将字符串转换为拼音以及单字的函数

create or replace function get_hzpy(vhz text) returns text[] as $$
declare
  res text[];
  tmp_py text;
  tmp_zm text;
begin
for i in 1..length(vhz)
loop
  select py,zm into tmp_py,tmp_zm from pinyin where hz=substring(vhz, i, 1);
  if not found then
    res := array_cat(res, array[substring(vhz, i, 1)]);
  else
    res := array_cat(res, array[tmp_py, tmp_zm, substring(vhz, i, 1)]);
  end if;
end loop;
return res;
end;
$$ language plpgsql strict immutable;

4、测试该函数,输入一个字符串,返回了它的 单字和所有单字的拼音、首字母,没有的话只输出单字。

test=# select get_hzpy('你好abx呵呵, ');
                     get_hzpy
--------------------------------------------------
 {ni,n,你,hao,h,好,a,b,x,","," "}
(1 row)

测试索引加速 单字、拼音、首字母 搜索

1、创建一张测试表,包含一个字符串

create table test(id int, info text);

2、创建函数倒排索引

test=# create index idx on test using gin (get_hzpy(info));
CREATE INDEX

3、写入测试数据

test=# insert into test values (1, '你好abx呵呵, ');
INSERT 0 1

4、按 "单字、拼音、首字母" 查询测试,使用倒排索引

test=# explain select * from test where get_hzpy(info) @> array['ni'];   -- 包含ni的记录
                            QUERY PLAN
-------------------------------------------------------------------
 Bitmap Heap Scan on test  (cost=4.10..20.92 rows=26 width=36)
   Recheck Cond: (get_hzpy(info) @> '{ni}'::text[])
   ->  Bitmap Index Scan on idx  (cost=0.00..4.09 rows=26 width=0)
         Index Cond: (get_hzpy(info) @> '{ni}'::text[])
(4 rows)

5、字典有的,可以查到,字典没有的,只能通过中文查

test=# select * from test where get_hzpy(info) @> array['ni'];
 id |     info
----+---------------
  1 | 你好abx呵呵,
(1 row)  

test=# select * from test where get_hzpy(info) @> array['he'];
 id | info
----+------
(0 rows)

6、补齐字典,注意补齐字典并不影响已有的数据,因为只在数据发生变化时才会重算 get_hzpy(info) 的值,并更新索引。

下面这个例子可以清晰的表明这个意思。

test=# insert into pinyin values ('呵','he','h');
INSERT 0 1  

test=# select * from test where get_hzpy(info) @> array['he'];
 id | info
----+------
(0 rows)  

test=# update test set id=2 where id=1;
UPDATE 1  

test=# select * from test where get_hzpy(info) @> array['he'];
 id | info
----+------
(0 rows)

只有更新了info字典,才会更新索引内容。

test=# update test set info='呵呵,xi' where id=2;
UPDATE 1
test=# select * from test where get_hzpy(info) @> array['he'];
 id |  info
----+---------
  2 | 呵呵,xi
(1 row)

7、因此,建议字典越全面越好。将来如果字典更新了,需要重建一下索引,方法如下:

create index CONCURRENTLY idx_test_2 on test using gin(get_hzpy(info));
drop index idx;  
时间: 2024-08-03 07:03:48

PostgreSQL汉字转拼音的相关文章

在PostgreSQL中实现按拼音、汉字、拼音首字母搜索的例子

在PostgreSQL中实现按拼音.汉字.拼音首字母搜索的例子 作者 digoal 日期 2016-11-09 标签 PostgreSQL , 拼音 , 中文分词 , tsvector , 拼音首字母 , hmm , 词库 背景 PostgreSQL有很多特性是可以提升开发效率,提高生产力的. 在前端页面中,搜索是一个非常常用的功能,例如淘宝首页的搜索. 为了提升用户体验,用户可以按拼音首字母进行搜索,按中文单词搜索,或者按拼音的全部进行搜索. 又比如家里的电视盒子,因为没有实体键盘,按拼音首字

javascript实现汉字转拼音代码分享

  文章主要介绍了javascript实现汉字转拼音代码分享,非常的实用,从项目中分离出来的,这里分享给大家,有需要的小伙伴可以参考下. js代码 ? 1 2 3 4 5 6 7 8 function arraySearch(l1,l2){ for (var name in PinYin){ if (PinYin[name].indexOf(l1)!=-1) { return name; break; } } return false; } 核心代码: ? 1 2 3 4 5 6 7 8 9 1

[原创代码]汉字转拼音 /成语词典/简体繁体在线互转

汉字|拼音|原创|在线|拼音|原创|在线 工作过程中随手写的 汉字转拼音 ,演示地址: http://www.facesun.cn/portal/demo/hz2py.asp 成语词典[除了解释,还带拼音\出处\造句],演示地址: http://www.facesun.cn/portal/demo/cycx.asp 简体繁体在线互转,演示地址: http://www.facesun.cn/portal/demo/jft.asp 主要是工作中一些想法的实现 第一个派上了大用场第二个没啥用就是写了玩

PHP汉字转换拼音的类

  网络上类似的代码大多只能在gb2312编码下使用,下面这个类同时能在utf-8编码下将汉字转换为拼音,具体的代码和用法如下:  <?php function Pinyin($_String, $_Code='gb2312') { $_DataKey = "aaianangaobabaibanbangbaobeibenbengbibianbiaobiebinbingbobucacaicancangcaocecengcha". "chaichanchangchaoche

Java实现汉字转换为拼音

汉字|拼音|转换 本文的核心代码取自easydozer的blog:http://blog.csdn.net/easydozer/代码说明:Java实现汉字转换为拼音的GUI版本. GUI代码部分:/** * @(#)CnToSpellGUI.java * kindani * 2004-10-25?? * */ import java.awt.*; import java.awt.event.*; import javax.swing.*; import javax.swing.event.*;

PHP汉字转拼音的函数

汉字转换成拼音函数,写到URL可以做搜索引擎优化 以下是引用片段:<?php // 汉字转换成拼音 写到URL做GOOGLE优化 //调用函数名 :pinyin($str) //****************汉字转换拼音函数开始********************* $d=array(  array("a",-20319),  array("ai",-20317),  array("an",-20304),  array("

汉字转拼音缩写的函数(C#)

函数|汉字|拼音 刚刚整理好-汉字转拼音缩写的函数(C#) 在CSDN上找了一下,没有找一完整的转换函数,特在前人基础上整理了一下,接下来的项目中有可能用到.感谢bugfree(八个飞飞).        /// <summary>        /// 汉字转拼音缩写        /// Code By MuseStudio@hotmail.com        /// 2004-11-30        /// </summary>        /// <param

利用IFELanguage分隔中文语句并对汉字加注拼音

这篇文档是讲如何利用IFELanguage接口实现对中文语句的分隔,并对词语和字加注拼音的方法. 首先感谢一下Zswang(伴水)兄弟,他的无私奉献精神和对Windows的深入研究值得我们每一个人学习.每次找到好东东他总是给我一份,包括这个IFELanguage接口资料.最初的资料源自一个日本网站,源代码是用VC8写的,Zswang(伴水)将其改写为一个适用于Delphi下的版本.原C++代码经ccrun(老妖)略作修改,在BCB6下调试通过.原VC8的版本和BCB6还有Zswang(伴水)写的

ASP汉字转拼音函数的方法

 <%  'ASP汉字转拼音函数 Set d = CreateObject("Scripting.Dictionary")  d.add "a",-20319  d.add "ai",-20317  d.add "an",-20304  d.add "ang",-20295  d.add "ao",-20292  d.add "ba",-20283  d.add