【问题标题】:Got Argument 'other' has incorrect type (expected spacy.tokens.token.Token, got str)得到参数“其他”的类型不正确(预期 spacy.tokens.token.Token,得到 str)
【发布时间】:2020-07-08 02:17:20
【问题描述】:

我在尝试读取 spacy 中的列表时遇到以下错误。

TypeError: Argument 'string' has wrong type (expected spacy.tokens.token.Token, got 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)

【问题讨论】:

    标签: python python-3.x spacy lemmatization


    【解决方案1】:

    问题是来自word not in stopwordswordToken 而不是字符串。 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 以加快处理速度。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-19
      相关资源
      最近更新 更多