首页 > 解决方案 > Spacy NLP 示例:无法将标记转换为小写,抛出错误

问题描述

from collections import defaultdict
item_ratings = defaultdict(list)

for idx, review in data.iterrows():

    doc = nlp(review.text)

    matches = matcher(doc)
    
    found_items = set(doc[match[1]:match[2]].lower_ for match in matches)

    
for item in found_items:
        
    item_ratings[item].append(review.stars)

错误:

AttributeError: 'spacy.tokens.span.Span' object has no attribute 'lower_'

我需要标记为小写,我该怎么做?

标签: spacyspacy-3

解决方案


found_items = set(doc[match[1]:match[2]].text.lower() for match in matches)

就像错误所说的那样,Span对象没有lower_. 您可以使用textthen that' 只是一个字符串,以便您可以将其小写。


推荐阅读