Love it!
You are still working on the Twitter sentiment analysis project. First, you want to identify positive tweets about movies and concerts.
You plan to find all the sentences that contain the words love, like, or enjoy and capture that word. You will limit the tweets by focusing on those that contain the words movie or concert by keeping the word in another group. You will also save the movie or concert name.
For example, if you have the sentence: I love the movie Avengers. You match and capture love. You need to match and capture movie. Afterwards, you match and capture anything until the dot.
The list sentiment_analysis containing the text of three tweets and the re module are loaded in your session. You can use print() to view the data in the IPython Shell.
Diese Übung ist Teil des Kurses
Regular Expressions in Python
Anleitung zur Übung
- Complete the regular expression to capture the words
loveorlikeorenjoy. Match and capture the wordsmovieorconcert. Match and capture anything appearing until the.. - Find all matches of the regex in each element of
sentiment_analysis. Assign them topositive_matches. - Complete the
.format()method to print out the results contained inpositive_matchesfor each element insentiment_analysis.
Interaktive Übung
Vervollständige den Beispielcode, um diese Übung erfolgreich abzuschließen.
# 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(____))