Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

自定义核心词库 #227

Open
tansongchen opened this issue Apr 24, 2024 · 7 comments
Open

自定义核心词库 #227

tansongchen opened this issue Apr 24, 2024 · 7 comments
Labels
question Further information is requested

Comments

@tansongchen
Copy link

功能描述

现在可以通过 addDict 来增加新的词到读音的映射,但是无法覆盖内置的所有词

背景是我在做一个带调拼音的输入法,希望让词中汉字的读音与单字一致,只出现《通用规范汉字字典》里出现的音(也即不考虑非助词的轻声),如果不改动源码,目前似乎实现不了

提炼几个功能点

  • 希望有自定义核心词库的 API
  • 如果需要改代码才能实现,希望能对内部实现机制有一些介绍
@zh-lx
Copy link
Owner

zh-lx commented Apr 25, 2024

addDict 是用来让用户自定义词典的 api,在拼音转换时,会根据通过 addDict 添加的词的权重来定义优先级。
如果你要强制定义所有单字的读音,可以使用 customPinyin api,命中 customPinyin 的字词都会优先走这个 api 定义的读音

可以看下这个 api 是否能够解决你的问题

@zh-lx zh-lx added the question Further information is requested label Apr 25, 2024
@tansongchen
Copy link
Author

tansongchen commented Apr 25, 2024

这个似乎也是在现有的基础上加,不是替换?

我的需求不光是单字,我是打算把「孩子」这种词都注成 hai2 zi3,不标轻声,词音和单字音一样

@zh-lx
Copy link
Owner

zh-lx commented Apr 25, 2024

这个似乎也是在现有的基础上加,不是替换?

我的需求不光是单字,我是打算把「孩子」这种词都注成 hai2 zi3,不标轻声,词音和单字音一样

是覆盖,比如说 [孩子],你添加了 子: 'zǐ',所有文本中的子的转换结果都是

@yisibl
Copy link
Collaborator

yisibl commented Apr 25, 2024

我的需求不光是单字,我是打算把「孩子」这种词都注成 hai2 zi3,不标轻声,词音和单字音一样

这个目前你只能把所有的词库中的轻声用 customPinyin 覆盖一遍,理想情况下,我们需要一个选项来控制是否开启轻声。

@yisibl
Copy link
Collaborator

yisibl commented Apr 25, 2024

另外,《通用规范汉字字典》是指商务印书馆出版的这本吗?

请注意,在「通用规范汉字表」中,我们目前还缺少一些字,将在 #228 补充完整。

@tansongchen
Copy link
Author

tansongchen commented Apr 28, 2024

目前使用 customPinyin 可以成功自定义二字以上的词。但是,对于一字词,好像没有办法用 customPinyin 来实现所有的需求。例如,

describe("add custom pinyin", () => {
  it("should not interfere with predefined words", () => {
    customPinyin({ : "yín", : "xíng háng héng" });
    expect(pinyin("银行")).toEqual("yín háng");
  });
});
image

我的本意是把整个通规字典导入一遍,以保证程序注音的结果中没匹配到词的单字给注的音一定是字典中的音,但是导致本来成词的也跟着这个表走了


如果感觉这里的行为不太好自定义,那我也可以等 #228 做好之后再搞

@zh-lx
Copy link
Owner

zh-lx commented Apr 29, 2024

目前使用 customPinyin 可以成功自定义二字以上的词。但是,对于一字词,好像没有办法用 customPinyin 来实现所有的需求。例如,

describe("add custom pinyin", () => {
  it("should not interfere with predefined words", () => {
    customPinyin({ : "yín", : "xíng háng héng" });
    expect(pinyin("银行")).toEqual("yín háng");
  });
});
image 我的本意是把整个通规字典导入一遍,以保证程序注音的结果中没匹配到词的单字给注的音一定是字典中的音,但是导致本来成词的也跟着这个表走了

如果感觉这里的行为不太好自定义,那我也可以等 #228 做好之后再搞

那我理解你只需要对于内置字典中所缺少的字导入一遍。你可以在通规字典导入时,对每个字先使用 pinyin api 转换下,如果转换结果和字本身相同,意味着内置字典缺少这个字。对于这些缺少的字再使用 customPinyin 导入

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants