【问题标题】:Get the synonyms out of a dataframe从数据框中获取同义词
【发布时间】:2019-02-28 16:20:16
【问题描述】:

我有一个由 {question, answer} 组成的数据集,用于聊天机器人训练,我用 pandas 加载了它。 我正在尝试使用 wordnet.synsets 为每个问题中的每个单词获取一袋同义词。我在这样做时遇到了一些困难,这是我尝试过的尝试。

import pandas  as pd`
import nltk.corpus
from nltk.corpus import stopwords, wordnet
from nltk.tokenize import word_tokenize
from nltk.stem import PorterStemmer, WordNetLemmatizer
df =pd.read_csv('healthtapQAs++.csv')
df['question']=df['question'].str.pad(width= i,side= 'left')
df['unpunctuated'] = df['question'].str.replace(r'[^\w\s]+', '')
df['tokenized'] = df['unpunctuated'].apply(word_tokenize) 
df['synonyms'] = df['tokenized'].apply(lambda x: [wordnet.synsets(y) for y 
in x])
df['synonyms_beta'] = df['synonyms'].apply( lambda x:[(y[0].name()) for y in 
x])`

这是我不断收到的错误类型

>   df['synonyms_beta'] = df['synonyms'].apply( lambda x:[(y[0].name()) for y in x])

IndexError: list index out of range

【问题讨论】:

  • y 之一没有零元素 - 意味着y 之一是空的。
  • 我明白了,有没有办法绕过它?

标签: python pandas dataframe wordnet synset


【解决方案1】:

你可以试试:

df['synonyms_beta'] = df['synonyms'].apply( lambda x:[(y[0].name()) if len(y) >0 else "no_syn" for y in x])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-16
    • 1970-01-01
    相关资源
    最近更新 更多