開始使用免費開始

轉小寫(Lowercasing)

你正在分析某旅遊網站的使用者評論。這些評論常出現大小寫不一致的情況,例如 "TRAVEL""travel"。為了進行情緒分析與主題萃取,你會先把所有單字轉成小寫,接著進行斷詞(tokenize),並移除停用詞與標點符號。

本題已提供 word_tokenize() 函式與 stop_words 串列。NLTK 資源已下載完成。

本練習屬於課程

Python 的 Natural Language Processing(NLP)

檢視課程

練習說明

  • 將提供的 review 轉為小寫。
  • lower_text 進行斷詞成為單字。
  • 使用串列生成式,利用 stop_wordsstring.punctuation 的串列來移除停用詞與標點符號。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

review = "I have been FLYING a lot lately and the Flights just keep getting DELAYED. Honestly, traveling for WORK gets exhausting with endless delays, but every trip teaches you something new!"

# Lowercase the review
lower_text = ____

# Tokenize the lower_text into words
tokens = ____

# Remove stop words and punctuation
clean_tokens = [____ if word ____ and word ____]

print(clean_tokens)
編輯並執行程式碼