开始使用免费开始使用

清洗数据框中的 TED 演讲

在本练习中,我们将回到第 1 章的 TED 演讲数据。给您提供了包含 5 个 TED 演讲的数据框 ted。您的任务是编写函数 preprocess,并将其应用到数据框的 transcript 特征上,使用前面介绍的技术来清洗这些演讲文本。

停用词列表已作为 stopwords 提供。

本练习是课程的一部分

Python 中的 NLP 特征工程

查看课程

练习说明

  • text 生成 Doc 对象。暂时忽略 disable 参数。
  • 使用列表推导配合 lemma_ 属性生成词元的词形还原形式。
  • 在 if 条件中使用 isalpha() 去除非字母字符。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# Function to preprocess text
def preprocess(text):
  	# Create Doc object
    doc = nlp(____, disable=['ner', 'parser'])
    # Generate lemmas
    lemmas = [token.____ for token in doc]
    # Remove stopwords and non-alphabetic characters
    a_lemmas = [lemma for lemma in lemmas 
            if lemma.____ and lemma not in stopwords]
    
    return ' '.join(a_lemmas)
  
# Apply preprocess to ted['transcript']
ted['transcript'] = ted['transcript'].apply(____)
print(ted['transcript'])
编辑并运行代码