开始使用免费开始使用

产品评论的频率分析

您现在可以使用更大的 TechZone 产品评论数据集。与之前一样,您已经完成预处理,并将评论转换为 BoW 表示 X。接下来,您的任务是分析词频,并找出数据集中最常见的术语。

为便于分析,我们提供了一个名为 get_top_ten() 的辅助函数。它接收一个单词列表及其对应计数,并返回出现频率最高的 10 个单词及其计数。

本练习是课程的一部分

Python 中的自然语言处理(NLP)

查看课程

交互式实操练习

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

def preprocess(text):
    text = text.lower()
    tokens = word_tokenize(text)
    tokens = [word for word in tokens if word not in string.punctuation]
    return " ".join(tokens)
  
cleaned_reviews = [preprocess(review) for review in product_reviews]
X = vectorizer.fit_transform(cleaned_reviews)

# Get word counts
word_counts = np.____(X.____, axis=0)
# Get words
words = vectorizer.____

top_words_with_stopwords, top_counts_with_stopwords = get_top_ten(words, word_counts)
print(top_words_with_stopwords, top_counts_with_stopwords)
编辑并运行代码