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

简介: 标签 PostgreSQL , Hunspell , 分词 , 复数还原 , 字典 背景 在英语中,名词通常都有复数,表示多个;形容词,过去式,动词等。 有large, larger, largest, stories, eating, did, doing, hacked这样的。 这可

标签

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数据库、数据库生态工具、云原生智能化数据库管控平台,为阿里巴巴经济体以及各个行业的企业客户和开发者提供从公共云到混合云再到私有云的完整解决方案,提供基于云基础设施进行数据从处理、到存储、再到计算与分析的一体化解决方案。本节课带你了解阿里云数据库产品家族及特性。
目录
相关文章
|
5天前
|
数据采集 人工智能 安全
|
14天前
|
云安全 监控 安全
|
6天前
|
自然语言处理 API
万相 Wan2.6 全新升级发布!人人都能当导演的时代来了
通义万相2.6全新升级,支持文生图、图生视频、文生视频,打造电影级创作体验。智能分镜、角色扮演、音画同步,让创意一键成片,大众也能轻松制作高质量短视频。
1180 152
|
19天前
|
机器学习/深度学习 人工智能 自然语言处理
Z-Image:冲击体验上限的下一代图像生成模型
通义实验室推出全新文生图模型Z-Image,以6B参数实现“快、稳、轻、准”突破。Turbo版本仅需8步亚秒级生成,支持16GB显存设备,中英双语理解与文字渲染尤为出色,真实感和美学表现媲美国际顶尖模型,被誉为“最值得关注的开源生图模型之一”。
1837 9
|
11天前
|
人工智能 自然语言处理 API
一句话生成拓扑图!AI+Draw.io 封神开源组合,工具让你的效率爆炸
一句话生成拓扑图!next-ai-draw-io 结合 AI 与 Draw.io,通过自然语言秒出架构图,支持私有部署、免费大模型接口,彻底解放生产力,绘图效率直接爆炸。
747 152
|
8天前
|
SQL 自然语言处理 调度
Agent Skills 的一次工程实践
**本文采用 Agent Skills 实现整体智能体**,开发框架采用 AgentScope,模型使用 **qwen3-max**。Agent Skills 是 Anthropic 新推出的一种有别于mcp server的一种开发方式,用于为 AI **引入可共享的专业技能**。经验封装到**可发现、可复用的能力单元**中,每个技能以文件夹形式存在,包含特定任务的指导性说明(SKILL.md 文件)、脚本代码和资源等 。大模型可以根据需要动态加载这些技能,从而扩展自身的功能。目前不少国内外的一些框架也开始支持此种的开发方式,详细介绍如下。
549 5
|
13天前
|
人工智能 安全 前端开发
AgentScope Java v1.0 发布,让 Java 开发者轻松构建企业级 Agentic 应用
AgentScope 重磅发布 Java 版本,拥抱企业开发主流技术栈。
689 14

相关产品

  • 云原生数据库 PolarDB
  • 云数据库 RDS PostgreSQL 版