轉小寫(Lowercasing)
你正在分析某旅遊網站的使用者評論。這些評論常出現大小寫不一致的情況,例如 "TRAVEL" 與 "travel"。為了進行情緒分析與主題萃取,你會先把所有單字轉成小寫,接著進行斷詞(tokenize),並移除停用詞與標點符號。
本題已提供 word_tokenize() 函式與 stop_words 串列。NLTK 資源已下載完成。
本練習屬於課程
Python 的 Natural Language Processing(NLP)
練習說明
- 將提供的
review轉為小寫。 - 將
lower_text進行斷詞成為單字。 - 使用串列生成式,利用
stop_words與string.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)