Spacy - 不使用数字和字母拆分单词的标记化规则

问题描述 投票:0回答:0

我使用下面的自定义 spacy 分词器来不将像 AB-230 这样的带连字符的单词拆分为“AB”和“230”。但是,当我有像“AB-32C”这样的标记时,它会被分成“AB-32”和“C”。即,只要单个标记中有字母和数字,它就会被分成两部分。我使用

nlp.tokenizer.explain()
来找出造成这种情况的原因,这显然是因为后缀规则。但是,我不确定如何在不影响其他后缀规则的情况下删除此规则。

def custom_tokenizer(model):

    inf = list(model.Defaults.infixes)    
    #Remove the generic op between numbers or between a number and a -           
    inf.remove(r"(?<=[0-9])[+\-\*^](?=[0-9-])")    
    inf = tuple(inf)                        
     # Add the removed rule     
    infixes = inf + tuple([r"(?<=[0-9])[+*^](?=[0-9-])", r"(?<=[0-9])-(?=-)"]) 
    # Remove - between letters rule
    infixes = [x for x in infixes if '-|–|—|--|---|——|~' not in x] 
    infix_re = compile_infix_regex(infixes)

    return Tokenizer(model.vocab, prefix_search=model.tokenizer.prefix_search,
                                suffix_search=model.tokenizer.suffix_search,
                                infix_finditer=infix_re.finditer,
                                token_match=model.tokenizer.token_match,
                                rules=model.Defaults.tokenizer_exceptions)

我如何告诉 spacy 不要拆分包含字母和数字的标记?请帮助我。

python regex string nlp spacy
© www.soinside.com 2019 - 2024. All rights reserved.