如何解决数据库分词的拼写纠正问题 - PostgreSQL Hunspell 字典 复数形容词动词等变异还原

本文涉及的产品
云原生数据库 PolarDB MySQL 版,Serverless 5000PCU 100GB
简介: 标签 PostgreSQL , Hunspell , 分词 , 复数还原 , 字典 背景 在英语中,名词通常都有复数,表示多个;形容词,过去式,动词等。 有large, larger, largest, stories, eating, did, doing, hacked这样的。 这可能会给分词带来一定的困扰,例如我们来看看PG默认的ts config怎么处理它的。 比如engli

标签

PostgreSQL , Hunspell , 分词 , 复数还原 , 字典


背景

在英语中,名词通常都有复数,表示多个;形容词,过去式,动词等。 有large, larger, largest, stories, eating, did, doing, hacked这样的。

这可能会给分词带来一定的困扰,例如我们来看看PG默认的ts config怎么处理它的。

比如english tsconfig是这么处理的

postgres=# SELECT * FROM ts_debug('english', 'larger');
   alias   |   description   | token  |  dictionaries  |  dictionary  | lexemes  
-----------+-----------------+--------+----------------+--------------+----------
 asciiword | Word, all ASCII | larger | {english_stem} | english_stem | {larger}
(1 row)

postgres=# SELECT * FROM ts_debug('english', 'large');
   alias   |   description   | token |  dictionaries  |  dictionary  | lexemes 
-----------+-----------------+-------+----------------+--------------+---------
 asciiword | Word, all ASCII | large | {english_stem} | english_stem | {larg}
(1 row)

postgres=# SELECT * FROM ts_debug('english', 'largest');
   alias   |   description   |  token  |  dictionaries  |  dictionary  |  lexemes  
-----------+-----------------+---------+----------------+--------------+-----------
 asciiword | Word, all ASCII | largest | {english_stem} | english_stem | {largest}
(1 row)

postgres=# SELECT * FROM ts_debug('english', 'stories');
   alias   |   description   |  token  |  dictionaries  |  dictionary  | lexemes 
-----------+-----------------+---------+----------------+--------------+---------
 asciiword | Word, all ASCII | stories | {english_stem} | english_stem | {stori}
(1 row)

很显然,它没有很好的处理这几个词, large, larger, largest, stories。

默认的parser支持的token类型

postgres=# select * from ts_token_type('default');
 tokid |      alias      |               description                
-------+-----------------+------------------------------------------
     1 | asciiword       | Word, all ASCII
     2 | word            | Word, all letters
     3 | numword         | Word, letters and digits
     4 | email           | Email address
     5 | url             | URL
     6 | host            | Host
     7 | sfloat          | Scientific notation
     8 | version         | Version number
     9 | hword_numpart   | Hyphenated word part, letters and digits
    10 | hword_part      | Hyphenated word part, all letters
    11 | hword_asciipart | Hyphenated word part, all ASCII
    12 | blank           | Space symbols
    13 | tag             | XML tag
    14 | protocol        | Protocol head
    15 | numhword        | Hyphenated word, letters and digits
    16 | asciihword      | Hyphenated word, all ASCII
    17 | hword           | Hyphenated word, all letters
    18 | url_path        | URL path
    19 | file            | File or path name
    20 | float           | Decimal notation
    21 | int             | Signed integer
    22 | uint            | Unsigned integer
    23 | entity          | XML entity
(23 rows)

实际上从PostgreSQL 9.6开始,就支持了拼写的纠正字典,参考

https://www.postgresql.org/docs/9.6/static/textsearch-dictionaries.html#TEXTSEARCH-ISPELL-DICTIONARY

通过affix, dict文件进行纠正。

例子

The .affix file of Ispell has the following structure:

prefixes
flag *A:
    .           >   RE      # As in enter > reenter
suffixes
flag T:
    E           >   ST      # As in late > latest
    [^AEIOU]Y   >   -Y,IEST # As in dirty > dirtiest
    [AEIOU]Y    >   EST     # As in gray > grayest
    [^EY]       >   EST     # As in small > smallest
And the .dict file has the following structure:

lapse/ADGRS
lard/DGRS
large/PRTY
lark/MRS

postgrespro开源了一个插件,实现了一些国家语言的fix , 可以用来处理这类拼写纠正。

Hunspell Dictionaries

https://github.com/postgrespro/hunspell_dicts

git clone https://github.com/postgrespro/hunspell_dicts
cd hunspell_dicts
ll
total 28K
drwxr-xr-x 5 digoal users 4.0K Dec  6 19:53 hunspell_de_de
drwxr-xr-x 5 digoal users 4.0K Dec  6 19:53 hunspell_en_us
drwxr-xr-x 5 digoal users 4.0K Dec  6 19:53 hunspell_fr
drwxr-xr-x 5 digoal users 4.0K Dec  6 19:53 hunspell_nl_nl
drwxr-xr-x 5 digoal users 4.0K Dec  6 19:53 hunspell_nn_no
drwxr-xr-x 5 digoal users 4.0K Dec  6 19:53 hunspell_ru_ru
-rw-r--r-- 1 digoal users 1.3K Dec  6 19:53 README.md

cd hunspell_en_us
ll
total 560K
-rw-r--r-- 1 dege.zzz users 3.1K Dec  6 19:53 en_us.affix -- 纠正拼写的语法
-rw-r--r-- 1 dege.zzz users 531K Dec  6 19:53 en_us.dict  -- 纠正字典
drwxr-xr-x 2 dege.zzz users 4.0K Dec  6 19:53 expected
-rw-r--r-- 1 dege.zzz users  804 Dec  6 19:53 hunspell_en_us--1.0.sql
-rw-r--r-- 1 dege.zzz users  150 Dec  6 19:53 hunspell_en_us.control
drwxr-xr-x 2 dege.zzz users 4.0K Dec  6 19:53 license
-rw-r--r-- 1 dege.zzz users  370 Dec  6 19:53 Makefile
drwxr-xr-x 2 dege.zzz users 4.0K Dec  6 19:53 sql

make USE_PGXS=1 install

目前支持的几个字典如下

Module Dictionary Configuration
hunspell_de_de german_hunspell german_hunspell
hunspell_en_us english_hunspell english_hunspell
hunspell_fr french_hunspell french_hunspell
hunspell_nl_nl dutch_hunspell dutch_hunspell
hunspell_nn_no norwegian_hunspell norwegian_hunspell
hunspell_ru_ru russian_hunspell russian_hunspell

通过模块安装这些字典

psql

CREATE EXTENSION hunspell_en_us;

postgres=# select * from pg_ts_config;
     cfgname      | cfgnamespace | cfgowner | cfgparser 
------------------+--------------+----------+-----------
 simple           |           11 |       10 |      3722
 danish           |           11 |       10 |      3722
 dutch            |           11 |       10 |      3722
 english          |           11 |       10 |      3722
 finnish          |           11 |       10 |      3722
 french           |           11 |       10 |      3722
 german           |           11 |       10 |      3722
 hungarian        |           11 |       10 |      3722
 italian          |           11 |       10 |      3722
 norwegian        |           11 |       10 |      3722
 portuguese       |           11 |       10 |      3722
 romanian         |           11 |       10 |      3722
 russian          |           11 |       10 |      3722
 spanish          |           11 |       10 |      3722
 swedish          |           11 |       10 |      3722
 turkish          |           11 |       10 |      3722
 english_hunspell |         2200 |       10 |      3722  -- 新增
(17 rows)

解决复数,形容词问题

postgres=# SELECT * FROM ts_debug('english_hunspell', 'stories');
   alias   |   description   |  token  |          dictionaries           |    dictionary    | lexemes 
-----------+-----------------+---------+---------------------------------+------------------+---------
 asciiword | Word, all ASCII | stories | {english_hunspell,english_stem} | english_hunspell | {story}
(1 row)

postgres=# SELECT * FROM ts_debug('english_hunspell', 'large');
   alias   |   description   | token |          dictionaries           |    dictionary    | lexemes 
-----------+-----------------+-------+---------------------------------+------------------+---------
 asciiword | Word, all ASCII | large | {english_hunspell,english_stem} | english_hunspell | {large}
(1 row)

postgres=# SELECT * FROM ts_debug('english_hunspell', 'larger');
   alias   |   description   | token  |          dictionaries           |    dictionary    | lexemes 
-----------+-----------------+--------+---------------------------------+------------------+---------
 asciiword | Word, all ASCII | larger | {english_hunspell,english_stem} | english_hunspell | {large}
(1 row)

postgres=# SELECT * FROM ts_debug('english_hunspell', 'largest');
   alias   |   description   |  token  |          dictionaries           |    dictionary    | lexemes 
-----------+-----------------+---------+---------------------------------+------------------+---------
 asciiword | Word, all ASCII | largest | {english_hunspell,english_stem} | english_hunspell | {large}
(1 row)

一个小的插件,反映的是PostgreSQL社区生态,以及PG社区圈子热衷贡献的精神。还有很多很多这样的例子,在程序实现要花不少时间的问题,可能在PG圈就能找到插件帮你解决。快来用PG吧。

相关实践学习
使用PolarDB和ECS搭建门户网站
本场景主要介绍基于PolarDB和ECS实现搭建门户网站。
阿里云数据库产品家族及特性
阿里云智能数据库产品团队一直致力于不断健全产品体系,提升产品性能,打磨产品功能,从而帮助客户实现更加极致的弹性能力、具备更强的扩展能力、并利用云设施进一步降低企业成本。以云原生+分布式为核心技术抓手,打造以自研的在线事务型(OLTP)数据库Polar DB和在线分析型(OLAP)数据库Analytic DB为代表的新一代企业级云原生数据库产品体系, 结合NoSQL数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
相关文章
|
1天前
|
SQL 关系型数据库 数据库
关系型数据库选择合适的数据库管理系统
【5月更文挑战第5天】关系型数据库选择合适的数据库管理系统
234 2
关系型数据库选择合适的数据库管理系统
|
1天前
|
关系型数据库 MySQL BI
关系型数据库选择合适的数据库管理系统
【5月更文挑战第4天】关系型数据库选择合适的数据库管理系统
175 4
关系型数据库选择合适的数据库管理系统
|
1天前
|
存储 Cloud Native 关系型数据库
PolarDB-X 是面向超高并发、海量存储和复杂查询场景设计的云原生分布式数据库系统
【5月更文挑战第14天】PolarDB-X 是面向超高并发、海量存储和复杂查询场景设计的云原生分布式数据库系统
23 2
|
1天前
|
Cloud Native 关系型数据库 分布式数据库
PolarDB是阿里云自主研发的关系型云原生数据库
【5月更文挑战第14天】PolarDB是阿里云自主研发的关系型云原生数据库
28 3
|
1天前
|
存储 关系型数据库 MySQL
Percona XtraBackup是否支持PostgreSQL数据库备份?
【5月更文挑战第13天】Percona XtraBackup是否支持PostgreSQL数据库备份?
47 1
|
1天前
|
负载均衡 关系型数据库 MySQL
关系型数据库的安装和配置数据库节点
【5月更文挑战第5天】关系型数据库的安装和配置数据库节点
125 3
关系型数据库的安装和配置数据库节点
|
1天前
|
SQL 存储 关系型数据库
性能诊断工具DBdoctor如何快速纳管数据库PolarDB-X
DBdoctor是一款基于eBPF技术的数据库性能诊断工具,已通过阿里云PolarDB分布式版(V2.3)认证。PolarDB-X是阿里云的高性能云原生分布式数据库,采用Shared-nothing和存储计算分离架构,支持高可用、水平扩展和低成本存储。PolarDB-X V2.3.0在读写混合场景下对比开源MySQL有30-40%的性能提升。DBdoctor能按MySQL方式纳管PolarDB-X的DN节点,提供性能洞察和诊断。用户可通过指定步骤安装PolarDB-X和DBdoctor,实现数据库的管理和性能监控。
114 0
|
1天前
|
Cloud Native 关系型数据库 分布式数据库
数据库性能诊断工具DBdoctor通过阿里云PolarDB产品生态集成认证
DBdoctor(V3.1.0)成功通过阿里云PolarDB分布式版(V2.3)集成认证,展现优秀兼容性和稳定性。此工具是聚好看科技的内核级数据库性能诊断产品,运用eBPF技术诊断SQL执行,提供智能巡检、根因分析和优化建议。最新版V3.1.1增加了对PolarDB-X和OceanBase的支持,以及基于cost的索引诊断功能。PolarDB-X是阿里巴巴的高性能云原生分布式数据库,兼容MySQL生态。用户可通过提供的下载地址、在线试用链接和部署指南体验DBdoctor。
|
1天前
|
存储 关系型数据库 分布式数据库
数据库索引回表困难?揭秘PolarDB存储引擎优化技术
PolarDB分布式版存储引擎采用CSM方案均衡资源开销与可用性。
数据库索引回表困难?揭秘PolarDB存储引擎优化技术
|
1天前
|
关系型数据库 Java 数据库
docker部署postgresql数据库和整合springboot连接数据源
docker部署postgresql数据库和整合springboot连接数据源
18 0