Hashtags and mentions in Russian tweets
Let's revisit the tweets
dataframe containing the Russian tweets. In this exercise, you will compute the number of hashtags and mentions in each tweet by defining two functions count_hashtags()
and count_mentions()
respectively and applying them to the content
feature of tweets
.
In case you don't recall, the tweets are contained in the content
feature of tweets
.
Cet exercice fait partie du cours
Feature Engineering for NLP in Python
Exercice interactif pratique
Essayez cet exercice en complétant cet exemple de code.
# Function that returns numner of hashtags in a string
def count_hashtags(string):
# Split the string into words
words = string.split()
# Create a list of words that are hashtags
hashtags = [word for word in words if ____.____(____)]
# Return number of hashtags
return(len(hashtags))
# Create a feature hashtag_count and display distribution
tweets['hashtag_count'] = tweets['content'].apply(count_hashtags)
tweets['hashtag_count'].hist()
plt.title('Hashtag count distribution')
plt.show()