python - Spacy匹配器: TypeError: an integer is required

标签 python nlp spacy

我正在尝试使用 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/

相关文章:

python - 如何在管理中的字段后添加详细名称 - Django

python - 导入包含在 "when some of them contains "和逗号中的值的 CSV 文件

tensorflow - 恢复保存的训练变量后,保存的 tensorflow NLP 模型不会输出任何内容

python - 如何从头开始训练 gpt 2?

python - 如何规范化使用命名实体识别提取的关键字

python - 将 spacy token 向量转换为文本

python - 检查列表中的元素是否存在于python中的多个列表中

python - 处理后如何保留(jpg)文件的名称?

java - 用 Java 创建自然 DSL 的最佳工具是什么?

python - Spacy nlp = spacy.load ("en_core_web_lg")