TypeError:在EntityRuler中,字符串索引必须是整数

时间:2019-08-29 04:01:50

标签: python spacy

关于TypeError: string indices must be integers,有很多关于SO的问题和答案。但是,它们不适用于我的情况,因此这个问题

我使用以下EntityRuler的spaCy代码

from spacy.pipeline import EntityRuler
nlp = spacy.load('en_core_web_lg')

patterns = {"label": "BUSI", "pattern": [{'LOWER': 'store'}]}

# Initialise a ruler with a new pattern and add it to the processing pipeline
ruler = EntityRuler(nlp)
ruler.add_patterns(patterns)
nlp.add_pipe(ruler)

text = 'Store 1 is in LA but store 2 in NY.'

# Display the text with highlighted keywords on screen
svg = spacy.displacy.render(nlp(text), style='ent', jupyter=True)

但是,我遇到了这个错误

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-16-68d5c254c098> in <module>()
      9 # Initialise a ruler with new patterns and add it to the processing pipeline
     10 ruler = EntityRuler(nlp)
---> 11 ruler.add_patterns(patterns)
     12 nlp.add_pipe(ruler)
     13 

~\AppData\Local\Continuum\anaconda3\lib\site-packages\spacy\pipeline\entityruler.py in add_patterns(self, patterns)
    178         """
    179         for entry in patterns:
--> 180             label = entry["label"]
    181             if "id" in entry:
    182                 label = self._create_label(label, entry["id"])

TypeError: string indices must be integers

我简直不知道string indices指的是什么。是entry["label"]吗?您能告诉我如何解决吗?非常感谢。

1 个答案:

答案 0 :(得分:1)

我认为您的模式应该是这样的:

person