使 Spacy 分词器不在 / 上拆分
nlp 213
原文标题 :Make Spacy tokenizer not split on /
如何修改英文分词器以防止在'/'
字符上拆分分词?
例如,以下字符串应该是一个标记:
import spacy
nlp = spacy.load('en_core_web_md')
doc = nlp("12/AB/568793")
for t in doc:
print(f"[{t.pos_} {t.text}]")
# produces
#[NUM 12]
#[SYM /]
#[ADJ AB/568793]
回复
我来回复-
Dave 评论
该方法是从 Spacy 文档中删除“修改现有规则集”中的规则的一种变体:
nlp = spacy.load('en_core_web_md') infixes = nlp.Defaults.infixes assert(len([x for x in infixes if '/' in x])==1) # there seems to just be one rule that splits on /'s # remove that rule; then modify the tokenizer infixes = [x for x in infixes if '/' not in x] nlp.tokenizer.infix_finditer = spacy.util.compile_infix_regex(infixes).finditer
2年前