Skip to content

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

Closed

Description

@juexZZ

你好,我有一个词典词表并希望以此词典为准对文本进行分词,结果发现词典中词被切分为了单字:
例如我的词典中有单字"计""算""机",同时也有词"计算机",却发现在加入此自定义词表后,我的文本中所有的"计算机"都被切分为三个单字,这显然不是期望的结果,粗略阅读您的代码,在加入用户自定义词典后确实是调用了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: [['计', '算', '机']]

Activity

  1. AlongWY commented on Dec 22, 2020

    @AlongWY
    Contributor

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

  2. juexZZ commented on Dec 22, 2020

    @juexZZ
    Author

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

  3. juexZZ commented on Dec 23, 2020

    @juexZZ
    Author

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

  4. added a commit that references this issue on Dec 28, 2020
  5. link4ron commented on Jan 19, 2021

    @link4ron

    初学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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions