从推文提取词干
在本练习中,您将使用名为 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])