開始使用免費開始

產品評論的詞頻分析

你現在可以使用更大的 TechZone 產品評論資料集。和先前一樣,你已經將評論前處理並轉換為 BoW(詞袋)表示法 X。你現在的任務是分析詞頻,並找出資料集中最常見的詞彙。

為了協助分析,我們提供了一個輔助函式 get_top_ten()。它接收詞彙清單及其對應的計數,並回傳出現頻率最高的 10 個詞彙及其計數。

本練習屬於課程

Python 的 Natural Language Processing(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)
編輯並執行程式碼