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

用户自定义词典中的词在分词时被切分的问题 #457

Closed
juexZZ opened this issue Dec 21, 2020 · 4 comments
Closed

用户自定义词典中的词在分词时被切分的问题 #457

juexZZ opened this issue Dec 21, 2020 · 4 comments

Comments

@juexZZ
Copy link

juexZZ commented Dec 21, 2020

你好,我有一个词典词表并希望以此词典为准对文本进行分词,结果发现词典中词被切分为了单字:
例如我的词典中有单字"计""算""机",同时也有词"计算机",却发现在加入此自定义词表后,我的文本中所有的"计算机"都被切分为三个单字,这显然不是期望的结果,粗略阅读您的代码,在加入用户自定义词典后确实是调用了trie树前向最大匹配,不知是哪里有问题?是否可以通过某些设定来改善?版本4.0.9,例子:

# default setting:
toks, _ = ltp.seg(["计算机"])
print(toks)
# output: [['计算机']]
# add user defined vocab
ltp.add_words(words=xianhan_words, max_window=4)
toks, _ = ltp.seg(["计算机"])
print(toks)
# output: [['计', '算', '机']]
@AlongWY
Copy link
Contributor

AlongWY commented Dec 22, 2020

这个问题会在新版本中修复,目前您可以修改下代码

@juexZZ
Copy link
Author

juexZZ commented Dec 22, 2020

了解。如何修改?您是指我按照这个新的commit (12c6361) 去改一下我这边对应的代码就好了是吗?谢谢

@AlongWY AlongWY closed this as completed Dec 22, 2020
@juexZZ
Copy link
Author

juexZZ commented Dec 23, 2020

A follow-up issue.
按照您的commit修改代码后,我发现max_forward_matching碰到某些样例,尤其是包含拼音字符的,会卡住,推测是修改后的while循环没有妥善退出。您可以试一下:
落日luòhuā
授命 : [shòumìng]下命令
看看是不是会遇到这样的情况。

AlongWY added a commit that referenced this issue Dec 28, 2020
2. 更新4个UD模型
@link4ron
Copy link

初学LTP,但我经过跟踪代码分析,建议maximum_forward_matching.py的maximum_forward_matching函数以下代码做以下修改。修改后可以按照自定义字典进行匹配。但有无其他影响尚不知道。
if candidate:
# start += candidate[1] #原代码,按照字典匹配出词位置后,后续开始匹配位置却直接加上上一个词的最后一字的位置,个人觉得应该从上一个词的最后一个字+1开始匹配新词。
start = candidate[1] + 1 #修改后的代码
maximum_matching_pos.append(candidate)

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

No branches or pull requests

3 participants