Read and Parse Chinese Input-Method Dictionaries

Read Chinese input-method dictionary files into a common 'R' data model. The 'Rust' backend supports Sogou, QQ Pinyin, and Baidu dictionary formats and preserves source metadata, code components, and weights. This is useful for building a custom Chinese word segmentation dictionary.


r-cidian

CRANstatus R-universeversion License:MIT AskDeepWiki

r-cidian 读取中文输入法词库,并把不同格式转换成统一的 cidian_dictionary 对象。目前支持以下格式:

格式 扩展名 来源
SCEL .scel 搜狗
QCEL .qcel QQ 拼音
QPYD .qpyd QQ 拼音
BDICT .bdict 百度
BCD .bcd 百度

底层解析由 Rust 库 cidian-rs 进行。解析结果保持词条在源文件中的顺序,不会自动规范化、排序或去重。

r-cidian 是 qinwf/cidian 的现代化替代方案。开发初衷是仿照已经删库且无法编译的原 cidian 包的功能,为 jiebaRS 提供方便的自定义词典导入功能。

安装

从 CRAN 安装

你可以从 CRAN 安装 r-cidian 的发布版本:

install.packages("cidian")

如果您使用 Linux,您可以尝试从 P3M 为不同的 Linux 发行版安装预编译的二进制文件,避免从源代码编译。

R-universe / R-multiverse

R-universe 和 R-multiverse 上提供了预编译的二进制包:

install.packages("cidian", repos = "https://yousa-mirage.r-universe.dev")
install.packages("cidian", repos = "https://community.r-multiverse.org")

从源码安装

使用 pak 从源码安装:

pak::pak("Yousa-Mirage/r-cidian")

注意: 从源码构建需要 Rust 工具链来编译 Rust 后端。

使用说明

library(cidian)

dictionary <- read_cidian("计算机科技.qcel")
summary(dictionary)
#> <summary.cidian_dictionary>
#> Format: QCEL 
#> Name: 计算机名词
#> Category: 计算机科技
#> Entries: 9,646 
#> Weighted entries: 9,646

entries <- cidian_entries(dictionary)
head(entries)
#>             word         code weight
#> 1         阿里通  a, li, tong      8
#> 2   阿姆达尔定律 a, mu, d....   9646
#> 3 阿姆斯特朗公理 a, mu, s....   9645
#> 4         阿帕网  a, pa, wang   9644
#> 5     埃尔布朗基 ai, er, ....   9643
#> 6   埃尔米特函数 ai, er, ....   9642

# cidian_metadata(dictionary) 可以获取该词典的元信息

其中,entries$code 是 list-column,每个元素都是一个字符向量。例如:

dictionary$entries$code[[1]]
#> [1] "a" "li" "tong"

write_words(dictionary, "words.txt") 可以把词典中所有词语每行一个写入文本文件,可用于为 jiebaRS 等分词工具提供自定义词典(jiebaRS 已内置了 import_cidian() 函数用于从输入法词库中导入自定义词语)。

致谢

  • 感谢 nopdan 大佬的输入法词库解析系列文章以及 nopdan/rose 蔷薇词库转换库,开发过程中大量参考了大佬的博客文章和代码实现。
  • 感谢 qinwf/cidian 项目为本包提供了开发动机。

许可证

MIT License

Reference manual

It appears you don't have a PDF plugin for this browser. You can click here to download the reference manual.

install.packages("cidian")

0.1.0 by Hao Cheng, 10 days ago


https://github.com/Yousa-Mirage/r-cidian


Report a bug at https://github.com/Yousa-Mirage/r-cidian/issues


Browse source code at https://github.com/cran/cidian


Authors: Hao Cheng [aut, cre, cph]


Documentation:   PDF Manual  


MIT + file LICENSE license


Imports cli, rlang

Suggests rmarkdown, spelling, testthat

System requirements: Cargo (Rust's package manager), rustc >= 1.85.0, xz


See at CRAN