【发布时间】:2018-06-24 17:45:00
【问题描述】:
我想在 Spacy 中包含连字符,例如:long-term、self-自尊、 等作为单个标记。在查看 StackOverflow 上的一些类似帖子后,Github、documentation 和 elsewhere,我还编写了一个自定义标记器,如下所示:
import re
from spacy.tokenizer import Tokenizer
prefix_re = re.compile(r'''^[\[\("']''')
suffix_re = re.compile(r'''[\]\)"']$''')
infix_re = re.compile(r'''[.\,\?\:\;\...\‘\’\`\“\”\"\'~]''')
def custom_tokenizer(nlp):
return Tokenizer(nlp.vocab, prefix_search=prefix_re.search,
suffix_search=suffix_re.search,
infix_finditer=infix_re.finditer,
token_match=None)
nlp = spacy.load('en_core_web_lg')
nlp.tokenizer = custom_tokenizer(nlp)
doc = nlp(u'Note: Since the fourteenth century the practice of “medicine” has become a profession; and more importantly, it\'s a male-dominated profession.')
[token.text for token in doc]
所以对于这句话: '注:自十四世纪以来,“行医”已成为一种职业;更重要的是,这是一个男性主导的职业。'
现在,合并自定义 Spacy Tokenizer 后的令牌是:
'Note', ':', 'Since', 'the', 'teenth', '世纪', 'the', 'practice', 'of', ''药', '”', '有', ';', '成为', 'a', '专业', ',', '和', '更多', '重要', ',', “它是”, 'a','男性主导','职业','。'
此前,此更改之前的标记是:
'Note', ':', 'Since', 'the', 'teenth', 'century', 'the', 'practice', 'of', '“', '医学', '”', 'has', 'become', 'a', 'professional', ';', 'and', 'more', '重要', ',', '它', "的", 'a', '男', ' -', '主宰', '职业', '.'
而且,预期的令牌应该是:
'Note', ':', 'Since', 'the', 'teenth', 'century', 'the', 'practice', 'of', '“', '医学', '”', 'has', 'become', 'a', 'professional', ';', 'and', 'more', 'importantly', ',', 'it', "的", 'a', '男性主导', '职业','。'
总结:正如大家所见...
- 包括连字符和除双引号和撇号以外的其他标点符号...
- ...但是现在,撇号和双引号没有早期或预期的行为。
- 我已经为中缀的正则表达式编译尝试了不同的排列和组合,但没有解决这个问题的进展。
【问题讨论】:
-
需要明确的是,“medicine” 总是用尾随双引号分开标记(错误地,前后都有):'“medicine' , '”'.你也想解决这个问题。
标签: regex nlp tokenize spacy linguistics