【问题标题】:Python Lemmatizing input list, return output listPython Lemmatizing 输入列表,返回输出列表
【发布时间】:2017-05-02 21:38:24
【问题描述】:

我有一个列表,其中包含我要进行词形还原的字符串。虽然我可以对所有字符串进行词形还原,但我很难以与输入到词形还原器的列表格式相同的列表格式返回词形化字符串。

对每个输出做一个类型,我得到一个 unicode 和 str 对象。我尝试将 unicode 转换为字符串并尝试将字符串连接到列表但没有运气。

下面是可重现的代码:

typea = ['colors', 'caresses', 'ponies', 'presumably', 'owed', 'says']
for i in xrange(0,len(typea)):
    # Lemmatize the words
    lemmatized_words = lmtzr.lemmatize(typea[i])
    print lemmatized_words

    #Output obtained: 
    color
    caress
    pony
    presumably
    owed
    say
    #Desired output
    #['color', 'caress', 'pony', 'presumably', 'owed', 'say']

【问题讨论】:

    标签: python lemmatization


    【解决方案1】:

    lmtzr.lemmatize 接受单个字符串并返回单个字符串。所以lemmatized_words 一次将是一个字符串。

    要对所有单词进行词形还原并将它们存储在一个列表中,您需要这样的东西:

    typea = ['colors', 'caresses', 'ponies', 'presumably', 'owed', 'says']
    lemmatized_words = [lmtzr.lemmatize(x) for x in typea]
    print lemmatized_words
    

    【讨论】:

      猜你喜欢
      • 2015-01-09
      • 2018-06-16
      • 2023-02-07
      • 1970-01-01
      • 2021-12-20
      • 2014-01-29
      • 2022-01-13
      • 1970-01-01
      • 2019-07-21
      相关资源
      最近更新 更多