【发布时间】:2020-12-05 22:40:25
【问题描述】:
我想尝试看看如何使用 spaCy 模式匹配来查找文本中引用的产品类别。我显然没有正确构建它。
我想将 CAT-POS-2299 标识为产品。我尝试了一些不同的变化。你将如何做到这一点,甚至可以寻找更通用的模式 CAT-???-???
也许我应该使用其他东西?
代码:
from spacy.matcher import Matcher
nlp = spacy.load("en_core_web_sm")
matcher = Matcher(nlp.vocab)
matcher.add("Product", None,
[{"LOWER": "CAT"},{"LOWER":"-"},{"LOWER":"POS"},{"LOWER":"-"},{"IS_DIGIT":True}]
)
doc = nlp(" We have a new product CAT-POS-2299 that will be available to users soon.")
matches = matcher(doc)
for match_id, start, end in matches:
string_id = nlp.vocab.strings[match_id] # Get string representation
span = doc[start:end] # The matched span
print(match_id, string_id, start, end, span.text)```
【问题讨论】: