开始使用免费开始使用

太喜欢了!

您仍在进行 Twitter 情感分析项目。首先,您想找出关于电影和音乐会的正向推文。

您的计划是:找到所有包含单词 love、like 或 enjoy 的句子,并捕获该单词。您还将把包含单词 movie 或 concert 的推文筛选出来,并把这个单词放在另一个分组中。同时,保存电影或音乐会的名称。

例如,若句子为:I love the movie Avengers. 您需要匹配并捕获 love。您还需要匹配并捕获 movie。之后,再匹配并捕获直到句号为止的所有内容。

列表 sentiment_analysis(包含 3 条推文文本)以及 re 模块已加载到您的会话中。您可以使用 print() 在 IPython Shell 中查看数据。

本练习是课程的一部分

Python 中的正则表达式

查看课程

练习说明

  • 完成正则表达式以捕获 lovelikeenjoy;匹配并捕获 movieconcert;并匹配、捕获在 . 之前出现的任意内容。
  • sentiment_analysis 的每个元素中查找该正则的所有匹配项,并将其赋给 positive_matches
  • 完成 .format() 方法,打印 sentiment_analysis 中每个元素对应的 positive_matches 结果。

交互式实操练习

通过完成这段示例代码来试试这个练习。

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

for tweet in sentiment_analysis:
	# Find all matches of regex in tweet
    positive_matches = re.____(____, ____)
    
    # Complete format to print out the results
    print("Positive comments found ____".format(____))
编辑并运行代码