空间短语匹配器值错误模式长度(11)>=短语_matcher.max_长度(十)

2024-04-23 21:08:06 发布

您现在位置:Python中文网/ 问答频道 /正文

在使用术语列表初始化新的PhraseMatcher时,出现以下错误:

ValueError: Pattern length (11) >= phrase_matcher.max_length (10). Length can be set on initialization, up to 10.

patterns = [nlp(org) for org in fields]
        self.matcher = PhraseMatcher(nlp.vocab)
        self.matcher.add('FIELD', None, *patterns)

Tags: orgself列表nlp错误matcherlengthmax
3条回答

在spacy2.1.4版本中,上述短语匹配器的值错误已得到解决。如果您遇到这样的错误,请更新spacy版本。 参考:github issue link

您可以尝试将类定义为实体匹配器并在各种模式/字段上循环

  class EntityMatcher(object):
       name = 'entity_matcher' 

  def __init__(self, nlp, terms, label): 
      patterns = [nlp(text) for text in terms] 
      self.matcher = PhraseMatcher(nlp.vocab) 
      self.matcher.add(label, None, *patterns) 

  def __call__(self, doc): 
      matches = self.matcher(doc) 
      for match_id, start, end in matches: 
      span = Span(doc, start, end, label = match_id) 
      doc.ents = list(doc.ents) 

  return doc  

目前,单个规则的长度不能超过10个标记:

# Allowed
'one two three four five six seven eight nine ten'
# Not Allowed
'one two three four five six seven eight nine ten eleven'

您可以尝试设置更高的限制,例如:self.matcher = PhraseMatcher(nlp.vocab, max_length=20),但是spacy10当前版本中的iirc是一个硬限制。在

请参阅https://spacy.io/api/phrasematcher#init上的相关文档和位于https://github.com/explosion/spacy/blob/master/spacy/matcher.pyx#L452的源代码

相关问题 更多 >