python - 得到参数 'other' 的类型不正确(预期 spacy.tokens.token.Token,得到 str)

标签 python python-3.x spacy lemmatization

我在尝试阅读 spacy 中的列表时遇到以下错误。
类型错误:参数“字符串”的类型不正确(预期 spacy.tokens.token.Token,得到 str)
这是下面的代码

f= "MotsVides.txt"
file= open(f, 'r', encoding='utf-8')
stopwords = [line.rstrip() for line in file]

# stopwords =['alors', 'au', 'aucun', 'aussi', 'autre', 'avant', 'avec', 'avoir', 'bon', 'car', 'ce', 'cela', 'ces', 'ceux', 'chaque', 'ci', 'comme', 'comment', 'ça', 'dans', 'des', 'du', 'dedans', 'dehors', 'depuis', 'deux', 'devrait', 'doit', 'donc', 'dos', 'droite', 'début', 'elle', 'elles', 'en', 'encore', 'essai', 'est', 'et', 'eu', 'étaient', 'état', 'étions', 'été', 'être', 'fait', 'faites', 'fois', 'font', 'force', 'haut', 'hors', 'ici', 'il', 'ils', 's', 'juste', 'la', 'le', 'les', 'leur', 'là\t ma', 'maintenant', 'mais', 'mes', 'mine', 'moins', 'mon', 'mot', 'même', 'ni', 'nommés', 'notre', 'nous', 'nouveaux', 'ou', 'où', 'par', 'parce', 'parole', 'pas', 'personnes', 'peut', 'peu', 'pièce', 'plupart', 'pour', 'pourquoi', 'quand', 'que', 'quel', 'quelle', 'quelles', 'quels', 'qui\t sa', 'sans', 'ses', 'seulement', 'si', 'sien', 'son', 'sont', 'sous', 'soyez', 'sujet', 'sur', 'ta', 'tandis', 'tellement', 'tels', 'tes', 'ton', 'tous', 'tout', 'trop', 'très', 'tu', 'valeur', 'voie', 'voient', 'vont', 'votre', 'vous', 'vu']


def spacy_process(texte):
    for lt in texte:
        mytokens = nlp(lt)
        print(mytokens)
        mytokens2 = [word.lemma_.lower().strip() for word in mytokens if word.pos_ != "PUNCT" and word not in stopwords]
    
    print(type(mytokens2))
    
a = ['je suis la bonne personne et droit à la caricature.', 'Je suis la bonne personne et droit à la caricature.']
spacy_process(a)

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-133-03cc18018278> in <module>
     33 
     34 a = ['je suis la bonne personne et droit à la caricature.', 'Je suis la bonne personne et droit à la caricature.']
---> 35 spacy_process(a)

<ipython-input-133-03cc18018278> in spacy_process(texte)
     28         mytokens = nlp(lt)
     29         print(mytokens)
---> 30         mytokens2 = [word.lemma_.lower().strip() for word in mytokens if word.pos_ != "PUNCT" and word not in stopwords]
     31 
     32     print(type(mytokens2))

<ipython-input-133-03cc18018278> in <listcomp>(.0)
     28         mytokens = nlp(lt)
     29         print(mytokens)
---> 30         mytokens2 = [word.lemma_.lower().strip() for word in mytokens if word.pos_ != "PUNCT" and word not in stopwords]
     31 
     32     print(type(mytokens2))

TypeError: Argument 'other' has incorrect typ (expected spacy.tokens.token.Token, got str)

最佳答案

问题是word来自 word not in stopwordsToken不是字符串。 Python 正在提示,因为它试图在字符串列表和 Token 之间进行搜索和比较。不起作用的类。
你想用 spacy word.text获取字符串,而不是 word .
下面的代码应该工作...

import spacy
stopwords = ['alors', 'au', 'aucun', 'aussi', 'autre'] # truncated for simplicity

nlp = spacy.load('en')
def spacy_process(texte):
    for lt in texte:
        mytokens = nlp(lt)
        mytokens2 = [word.lemma_.lower().strip() for word in mytokens if word.pos_ != "PUNCT" and word.text not in stopwords]
    print(mytokens2)

a = ['je suis la bonne personne et droit à la caricature.', 'Je suis la bonne personne et droit à la caricature.']
spacy_process(a)
顺便说一句......检查列表中的值相当慢。您应该将列表转换为 set加快速度。

关于python - 得到参数 'other' 的类型不正确(预期 spacy.tokens.token.Token,得到 str),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62786554/

相关文章:

python - typeerror 'builtin_function_or_method' 对象没有属性 '__getitem__'

python - 如何使用strptime转换微秒部分的7位时间戳字符串?

string - 用字符串变量中的一个新行替换多个空行

python - 我无法在 EMR PySpark 笔记本中安装 spacy 模型

python - 痕迹未显现

python - 从 Python 使用 OpenCV TLD

python-3.x - 来自 python 二叉树的 python 列表

Python SpaCy Regex 不提取包含单词的标记

python - Spacy - nlp.pipe() 返回生成器

Python 在列表中的元组中查找最大值、平均值、最小值和相加值