[开源] morph-query:英语词素/形态切分查询工具 (Python API / CLI)

在背单词或者进行语言学研究时,我们经常需要拆解单词的前缀、后缀、词根、派生词缀和屈折变化。虽然有诸如 UniMorph
(umLabeller) 和 CityLex 这样优秀的公开形态学(Morphology)标注数据集,但它们通常以几十兆甚至上百兆的 raw TSV
文本形式存在,直接检索和在代码中调用非常不便。

为了解决这个问题,我写了一个轻量级的本地查询工具 morph-query(已发布到 PyPI,代码在 GitHub
开源)。它将这些数据集整理并索引化为一个 ~50MB 的高性能 SQLite 数据库,并提供了开箱即用的命令行工具(CLI)和 Python
API。

• GitHub 仓库:https://github.com/connoryang331/morph-query
• PyPI 地址: pip install morph-query

──────

:rocket: 核心特性

  1. 多维度词素查询:支持按前缀 (Prefix)、后缀 (Suffix)、词根 (Root)、派生后缀 (Derivational Suffix) 或屈折后缀
    (Inflectional Suffix) 检索单词。
  2. 毫秒级响应:底层采用精心设计的 SQLite 关系表和多重索引,子查询及批量查询均在 1ms 内完成。
  3. 极简安装:为了避免 PyPI 安装包过大,我们将 50MB 的数据库托管在 GitHub Release。用户在首次运行 mq 命令或初始化
    Python 实例时,程序会自动下载并解压至本地,无需手动配置。
  4. 同时支持 CLI 和库调用:既可以在终端快速排查词素,也可以作为 Python 库集成到大家的词典制作脚本中。
    ──────

:laptop: 使用演示

1. 终端命令行 (CLI)

安装后直接在终端运行 mq 命令:

# 查询包含特定词根的单词
mq query --root dict

# 查询特定的形态切分 (如前缀、词根、后缀)
mq query --prefix con --root struct --suffix ion

输出样式类似下面这样(支持彩色高亮排版):

Word: construction
----------------------------------------
Data Source: umLabeller
- Morpheme Division: con-struct-ion
- Prefix: con
- Root: struct
- Derivational Suffix: ion
- Inflectional Suffix: (none)

2. Python API 调用

from morph_query import MQ

mq = MQ()

# 批量查询单词的词素切分
results = mq.query_words(["construction", "deconstruct"])
for word, info in results.items():
    print(word, info)

大哥,你这有点坑人啊


程序:
Failed to download morph-query database from https://github.com/connoryang331/morph-query/releases/download/v0.1.0/morph_query.db.zip. Error: HTTP Error 404: Not Found
实际:
https://github.com/connoryang331/morphoneme/releases/download/v0.1.0/morphoneme.db.zip


程序:
Please ensure you have internet access, or download it manually and place it at C:\Users\Administrator\.morph_query\morph_query.db
REAME.MD
如果未找到,程序会检查 ~/.morphoneme/morphoneme.db 路径。
实际:
morphoneme.db.zip解压出来为morphoneme.db,而程序在找morph_query.db

帖子:
mq query --root dict
实际:
mq query --root dict
Unknown: query

..............