Začněte nyníZačněte zdarma

Hashtagy a zmínky v ruských tweetech

Pojďme se vrátit k dataframu tweets s ruskými tweety. V tomto cvičení spočítáš počet hashtagů a zmínek v každém tweetu – definuješ dvě funkce count_hashtags() a count_mentions() a aplikuješ je na sloupec content v tweets.

Pro připomenutí: samotné tweety jsou uloženy ve sloupci content dataframu tweets.

Toto cvičení je součástí kurzu

Feature Engineering for NLP in Python

Zobrazit kurz

Interaktivní cvičení na vyzkoušení si v praxi

Vyzkoušejte si toto cvičení dokončením tohoto ukázkového kódu.

# 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()
Upravit a spustit kód