如何在 Postgres 文本搜索中使用 Ispell 字典?

Nei*_*gan 5 postgresql windows full-text-search

Postgres 可以在文本搜索中使用 Ispell 兼容的字典,但不提供所需的文件。

Nei*_*gan 7

此示例使用加拿大英语词典,但您也可以与其他人一起尝试。

这些是 Windows 所需的步骤:

  1. 打开http://src.chromium.org/svn/trunk/deps/third_party/hunspell_dictionaries/en_CA.dic
  2. 选择所有文本,复制它,然后将其粘贴到 Text Mechanic:http : //textmechanic.co/Sort-Text-Lines.html。在末尾添加换行符。
  3. 打开http://src.chromium.org/svn/trunk/deps/third_party/hunspell_dictionaries/en_CA.dic_delta
  4. 选择所有文本,复制它,然后将其粘贴到 Text Mechanic 中先前粘贴的文本下方。
  5. 滚动到顶部,选中并剪切第一行(应该是五位数),去掉换行符。
  6. 单击按字母顺序排列的按钮,然后等待文本排序。
  7. 选择所有文本并将其复制到剪贴板
  8. 以管理员身份打开 Windows 记事本
  9. 将步骤 7 中的文本粘贴到记事本中
  10. 将文件另存为en_ca.dict(使用 UTF-8 编码)到 Postgres 文本搜索文件夹。我的是 C:\Program Files\PostgreSQL\9.3\share\tsearch_data 。
  11. 打开http://src.chromium.org/svn/trunk/deps/third_party/hunspell_dictionaries/en_CA.aff,全选,复制并粘贴到记事本。将文件作为en_ca.affix保存到 Postgres 文本搜索文件夹。

在 PgAdmin 中,运行以下 SQL:

create text search dictionary ispell_en_ca (
  template  =   ispell,
  dictfile  =   en_ca,
  afffile   =   en_ca,
  stopwords =   english
);

--make sure it works:
select * from ts_lexize('ispell_en_ca', 'colours');

/* 
result:
ts_lexize
text[]
{coloured,colour}
*/
Run Code Online (Sandbox Code Playgroud)

您需要创建一个新的文本搜索配置才能使用字典。


小智 7

我编写了以下脚本来在运行 PostgreSQL 9.4 的 Ubuntu 14.04 上安装 en_us 字典。在大多数情况下,它应该很容易修改。

#!/bin/bash
cd /usr/share/postgresql/9.4/tsearch_data

wget http://src.chromium.org/svn/trunk/deps/third_party/hunspell_dictionaries/en_US.dic
wget http://src.chromium.org/svn/trunk/deps/third_party/hunspell_dictionaries/en_US.dic_delta
wget http://src.chromium.org/svn/trunk/deps/third_party/hunspell_dictionaries/en_US.aff -O en_us.affix

# Remove first line
sed -i 1d en_US.dic

# Concat the dic and dic_delta, sort alphabetically and remove the leading blank line (leaves the ending newline intact)
cat en_US.dic en_US.dic_delta | sort > en_us.dict
sed -i 1d en_us.dict

# Set permissions
chown -R postgres:postgres *

sudo -u postgres psql -c "CREATE TEXT SEARCH DICTIONARY ispell_en_us (template  = ispell, dictfile = en_us, afffile = en_us, stopwords = english);"

# Clean up source files
rm en_US*
Run Code Online (Sandbox Code Playgroud)