開始使用免費開始

從推文取得詞幹

在這個練習中,你會使用一個名為 tweets 的陣列。它包含從 Twitter 收集的航空公司情緒資料的文字。

你的任務是處理這個陣列,並用串列生成式將其轉換為詞元(tokens)清單。之後,迭代該詞元清單,為每個詞元建立詞幹(stem)。記住,串列生成式是 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])
編輯並執行程式碼