【发布时间】:2017-04-13 16:36:14
【问题描述】:
我想使用 python 库 spacy 来匹配文本中的标记(添加标签作为语义参考)。然后,我想使用匹配来提取标记之间的关系。我的第一个是使用 spacy 的matcher.add 和matcher.add_pattern。 matcher.add 工作正常,我可以找到令牌,我的代码到现在为止:
import spacy
nlp = spacy.load('en')
def merge_phrases(matcher, doc, i, matches):
if i != len(matches)-1:
return None
spans = [(ent_id, label, doc[start : end]) for ent_id, label, start, end in matches]
for ent_id, label, span in spans:
span.merge('NNP' if label else span.root.tag_, span.text, nlp.vocab.strings[label])
matcher = spacy.matcher.Matcher(nlp.vocab)
matcher.add(entity_key='1', label='FINANCE', attrs={}, specs=[[{spacy.attrs.ORTH: 'financial'}, {spacy.attrs.ORTH: 'instrument'}]], on_match=merge_phrases)
matcher.add(entity_key='2', label='BUYER', attrs={}, specs=[[{spacy.attrs.ORTH: 'acquirer'}]], on_match=merge_phrases)
matcher.add(entity_key='3', label='CODE', attrs={}, specs=[[{spacy.attrs.ORTH: 'Code'}]], on_match=merge_phrases)
这工作正常,它输出了非常好的结果:
doc = nlp(u'Code used to identify the acquirer of the financial instrument.')
# Output
['Code|CODE', 'used|', 'to|', 'identify|', 'the|', 'acquirer|BUYER', 'of|', 'the|', 'financial instrument|FINANCE', '.|']
我的问题是,我如何使用matcher.add_patern 来匹配令牌之间的关系,例如
matcher.add_pattern("IS_OF", [{BUYER}, {'of'}, {FINANCE}])
对于输出:
doc = nlp(u'Code used to identify the acquirer of the financial instrument.')
# Output
[acquirer of financial instrument]
我已经尝试了不同的方法来使它起作用,但显然不是,我想我对matcher.add_pattern的理解有问题。
- 请给我一些正确的方向如何做到这一点 空间?
- 是否可以在此处添加正则表达式以查找模式,如何?
- 如何添加多个具有相同标签的令牌,或以某种方式创建 相同标签的标记列表,例如。 “金融”?
我将不胜感激。
【问题讨论】:
标签: python information-extraction spacy