到处找文本,却总找错地方
请记住,相关文本不一定只在推文的主 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