到處找,卻都不是重點所在
記得,重點文字不一定只在推文的主要 text 欄位。也可能在 extended_tweet、retweeted_status,或 quoted_status。我們需要檢查所有這些欄位,才能確保把所有相關文字都納入。這件事會常常用到,所以我們要寫一個函式來處理。
前兩行會檢查主要的 text 欄位或 extended_tweet 是否包含該文字。其餘的部分需要你來補上。
本練習屬於課程
使用 Python 分析社群媒體資料
練習說明
完成 check_word_in_tweet 函式,請依下列步驟進行:
- 檢查欄位
quoted_status-text是否包含該字詞。 - 檢查欄位
quoted_status-extended_tweet-full_text是否包含該字詞。 - 檢查欄位
retweeted_status-text是否包含該字詞。 - 檢查欄位
retweeted_status-extended_tweet-full_text是否包含該字詞。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
def check_word_in_tweet(word, data):
"""Checks if a word is in a Twitter dataset's text.
Checks text and extended tweet (140+ character tweets) for tweets,
retweets and quoted tweets.
Returns a logical pandas Series.
"""
contains_column = data['text'].str.contains(word, case = False)
contains_column |= data['extended_tweet-full_text'].str.contains(word, case = False)
contains_column |= data[____].str.contains(word, case = False)
contains_column |= data[____].____.____(____, case = False)
contains_column |= data[____].____.____(____, ____)
contains_column |= ____[____].____.____(____, ____)
return contains_column