开始使用免费开始使用

从推文提取词干

在本练习中,您将使用名为 tweets 的数组。它包含从 Twitter 收集的航空公司情感数据的文本。

您的任务是处理该数组,并用列表推导式将其转换为由词元组成的列表。随后,遍历该词元列表,并为每个词元生成词干。请记住,列表推导式是 for 循环的一种单行替代方式。

本练习是课程的一部分

Python 中的情感分析

查看课程

练习说明

  • 导入用于将字符串转换为词干的函数。
  • 调用刚刚导入的 Porter 词干提取函数。
  • 使用列表推导式创建 tokens 列表。该列表应包含来自 tweets 数组的所有词元。
  • 遍历 tokens 列表,并对列表中的每个元素应用词干提取函数。

交互式实操练习

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

# Import the function to perform stemming
____
from nltk import word_tokenize

# Call the stemmer
porter = ____()

# Transform the array of tweets to tokens
tokens = [____]
# Stem the list of tokens
stemmed_tokens = [[____.____(word) for word in tweet] for tweet in tokens] 
# Print the first element of the list
print(stemmed_tokens[0])
编辑并运行代码