标签

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;  

PostgreSQL汉字转拼音相关推荐

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

    在PostgreSQL中实现按拼音.汉字.拼音首字母搜索的例子 作者 digoal 日期 2016-11-09 标签 PostgreSQL , 拼音 , 中文分词 , tsvector , 拼音首字母 ...

  2. PostgreSQL的汉字转拼音

    为什么80%的码农都做不了架构师?>>>    线上产品升级需要导入一批3W多的用户数据,问题卡在了用户的汉字拼音码上,Excel也能实现大部分汉字的拼音转换,但还有很多生僻字无法转 ...

  3. PostgreSQL的中文拼音排序

    为什么80%的码农都做不了架构师?>>>    前一段时间开发人员咨询,说postgresql里面想根据一个字段做中文的拼音排序,但是不得其解.So,Take a Loooook. ...

  4. Java pinyin4j 汉字转拼音包括——多音字

    Java汉字转拼音(包括多音字) 有个需求需要把汉字转拼音,我的小伙伴推荐用Unicode官方的包:下载有些慢. 实际中用了Java工具包:pinyin4j解决 可以转汉字,多音字,多音字的地方要求不 ...

  5. mysql汉字转拼音函数

    -- 创建汉字拼音对照临时表 CREATE TABLE IF NOT EXISTS `t_base_pinyin` (`pin_yin_` varchar(255) CHARACTER SET gbk ...

  6. 昵称到拼音php,php 汉字转换拼音程序_PHP教程

    php 汉字转换拼音程序 $p=ord(substr($str,$i,1)); if($p>160){ $q=ord(substr($str,++$i,1)); $p=$p*256+$q-655 ...

  7. 文件名批量汉字转拼音+核磁共振影像数据处理

    文件名批量汉字转拼音+核磁共振影像数据处理 讲解视频内容请移步Bilibili: https://space.bilibili.com/542601735 入群讨论请加v hochzeitstorte ...

  8. mysql简拼_mysql实现汉字换拼音,及汉字转简拼

    我们实现汉字转拼音,需要结束代码包来完成,但是有个问题 ,针对多音字,会把排列组合的所有方式 全部打印出来. 比如 地区名,都是不需要多音字的,只有一个拼音发音是正确的. 下边 我们来实现 汉字转拼音 ...

  9. java 汉字转拼音_推荐一款前端汉字转拼音组件工具

    推荐一个前端的汉字转拼音组件 hotoo/pinyin, 支持在 Node 和 Web 浏览器环境运行. github网址:https://github.com/hotoo/pinyin 特性 根据词 ...

最新文章

  1. C++基本序列式容器 vector (一)
  2. [WP7开发入门]在Windows 2003,XP上安装Windows Phone 7开发工具
  3. php柱形图 数据sql,ThinkPHP 5.1 读取数据库中的图片
  4. 前端学习(2608):vuex的介绍
  5. Java 面试题(4)—— 多线程
  6. 5-7激活层-BN层-FC层-损失层
  7. 解决“(1146, “Table ‘mydb.django_session‘ doesn‘t exist“)”报错的方法
  8. springboot2 war页面放在那_Spring Boot2版的权限管理系统
  9. 软件 Bug 引发的致命事故,程序员责任何在?| 技术头条
  10. Bailian2965 玛雅历【日期计算】
  11. 【基础知识】【中缀转逆波兰(后缀)表达式】
  12. 一卡通(M1卡)破解过程记录——获取扇区密钥
  13. 静态文件html中加入php的Url,YII中URL伪静态加前缀.html的方法
  14. 用excel制作门店流水日常
  15. PVE直通Intel核显虚拟机配置ffmpeg-qsv硬件加速
  16. 12道JS基础简答题
  17. FineCMS安装教程
  18. 中国宠物经济蛋糕静候分割
  19. linux 内存容量换算,Hi3516A开发--内存换算
  20. 融会贯通面对对象编程思想

热门文章

  1. CSS实现DIV块的阴影效果
  2. CSS的文本属性阴影效果
  3. 窄带高清赋能体育赛事,世界杯高清直播背后的技术变革
  4. Weakly Supervised Semantic Segmentation with Boundary Exploration
  5. 【解决笔记本电脑声音音量低的方法】
  6. 二叉树——推荐一些神奇的网站
  7. android 乐固渠道打包,安卓腾讯乐固(legutools)多渠道打包(友盟)
  8. python绘制函数曲线
  9. 使用father打包发布前端工具库
  10. matplotlib入门基础(五)轴坐标范围 轴刻度比例 轴刻度