ไม่ชอบเลย!
หลังจากค้นหาทวีตเชิงบวกแล้ว ตอนนี้จะทำแบบเดียวกันกับทวีตเชิงลบบ้าง โดยจะหาประโยคที่มีคำว่า hate, dislike หรือ disapprove และบันทึกชื่อ movie หรือ concert ไว้เช่นเดิม แต่ครั้งนี้จะ match คำว่า movie หรือ concert โดยไม่ capture คำเหล่านั้น
ตัวอย่างเช่น ถ้ามีประโยค: I dislike the movie Avengers a lot. ระบบจะ match และ capture คำว่า dislike จากนั้น match แต่ไม่ capture คำว่า movie แล้ว match และ capture ทุกอย่างจนถึงจุด
ลิสต์ sentiment_analysis ที่มีข้อความจากทวีตสามรายการ รวมถึงโมดูล re ถูกโหลดไว้ในเซสชันแล้ว สามารถใช้ print() เพื่อดูข้อมูลใน IPython Shell ได้
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Regular Expressions ใน Python
คำแนะนำการฝึกหัด
- เติม regular expression ให้สมบูรณ์เพื่อ capture คำว่า
hate,dislikeหรือdisapproveจากนั้น match แต่ไม่ capture คำว่าmovieหรือconcertและ match พร้อม capture ทุกอย่างที่ปรากฏจนถึง. - ค้นหาทุก match ของ regex ในแต่ละ element ของ
sentiment_analysisแล้วกำหนดค่าให้กับnegative_matches - เติมเมธอด
.format()ให้ครบเพื่อแสดงผลลัพธ์ที่อยู่ในnegative_matchesสำหรับแต่ละ element ในsentiment_analysis
แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ
ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์
# 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(____))