開始使用免費開始

呃!不適合我!

在找出正向推文之後,你也想對負向推文做相同的事。你的計畫是找出包含單字 hate、dislike 或 disapprove 的句子。你會再次儲存 movie 或 concert 的名稱。你會取得包含單字 movie 或 concert 的推文,但這次不打算把該單字存下來。

例如,若你有句子:I dislike the movie Avengers a lot.。你需要比對並擷取 dislike。你會比對但不擷取 movie 這個字。接著,你要比對並擷取直到句點為止的任何內容。

清單 sentiment_analysis 含有 3 則推文的文字,re 模組也已載入至你的工作階段。你可以使用 print() 在 IPython Shell 檢視資料。

本練習屬於課程

Python 中的正規表示法

檢視課程

練習說明

  • 完成正規表示式,以擷取 hatedislikedisapprove 這些單字。比對但不要擷取 movieconcert 這些單字。比對並擷取直到 . 出現為止的所有內容。
  • sentiment_analysis 的每個元素中,找出該正規表示式的所有比對結果。將它們指定給 negative_matches
  • 完成 .format() 方法,逐一列印 sentiment_analysis 中各元素所對應 negative_matches 的結果。

動手互動練習

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

# Write a regex that matches sentences with the optional words
regex_negative = r"____.+?____\s____\."

for tweet in sentiment_analysis:
	# Find all matches of regex in tweet
    negative_matches = re.____(____, ____)
    
    # Complete format to print out the results
    print("Negative comments found ____".format(____))
編輯並執行程式碼