CommencerCommencer gratuitement

Are they bots?

The company that you are working for asked you to perform a sentiment analysis using a dataset with tweets. First of all, you need to do some cleaning and extract some information.
While printing out some text, you realize that some tweets contain user mentions. Some of these mentions follow a very strange pattern. A few examples that you notice: @robot3!, @robot5& and @robot7#

To analyze if those users are bots, you will do a proof of concept with one tweet and extract them using the .findall() method.

You write down some helpful metacharacters to help you later:

\d: digit
\w: word character
\W: non-word character
\s: whitespace

The text of one tweet was saved in the variable sentiment_analysis. You can use print(sentiment_analysis) to view it in the IPython Shell.

Cet exercice fait partie du cours

Regular Expressions in Python

Afficher le cours

Instructions

  • Import the re module.
  • Write a regex that matches the user mentions that starts with @ and follows the pattern, e.g. @robot3!.
  • Find all the matches of the pattern in the sentiment_analysis variable.

Exercice interactif pratique

Essayez cet exercice en complétant cet exemple de code.

# Import the re module
____

# Write the regex
regex = ____"____"

# Find all matches of regex
print(re.____(____, ____))
Modifier et exécuter le code