我正在尝试使用 spacy 创建一个短语匹配器,但我不断收到一条错误消息:TypeError:需要一个整数。我的“类”是我从数据库中选择的列中的行。我不明白为什么需要整数,因为查看文档,他们似乎在做与我所做的相同的事情,但每当我尝试运行代码时,我都会在 matcher.add 中收到错误。 任何想法将不胜感激。这是我的代码:
import pandas as pd
import spacy
from spacy.matcher import PhraseMatcher
from nltk.tokenize import word_tokenize, sent_tokenize
import nltk
data = pd.read_csv('C:/woclorev.csv')
class_name = data['Class Name'].drop_duplicates()
class_name_str = class_name.tolist()
reviews = data['Reviewtext'].astype(str)
token_rev = reviews.apply(word_tokenize)
#PhraseMatcher object
matcher = PhraseMatcher(nlp.vocab, attr='LOWER')
matcher.add('Classes', None, *class_name_str)
matches = matcher(token_rev)
这是完整的错误消息: 文件“”,第 1 行,文件“phrasematcher.pyx”,第 209 行,spacy.matcher.phrasematcher.PhraseMatcher.add 类型错误:需要一个整数
最佳答案
来自the docs
The PhraseMatcher lets you efficiently match large terminology lists. While the Matcher lets you match sequences based on lists of token descriptions, the PhraseMatcher accepts match patterns in the form of Doc objects.
如果无法看到 word_tokenize 是什么(我假设是 nltk 实现?),很难确定,但如果该函数不返回 spaCy Doc 对象,则匹配器可能会引发异常嗯>
关于python - Spacy匹配器: TypeError: an integer is required,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61945944/