ロシア語ツイートのハッシュタグとメンション
ロシア語のツイートを含む tweets データフレームに戻りましょう。この演習では、2つの関数 count_hashtags() と count_mentions() を定義し、それぞれ各ツイート内のハッシュタグ数とメンション数を計算して、tweets の content 特徴量に適用します。
念のためお伝えすると、ツイート本文は tweets の content 特徴量に含まれています。
この演習はコースの一部です
Pythonで学ぶNLPの特徴量エンジニアリング
実践的なインタラクティブ演習
このサンプルコードを完成させて、この演習に挑戦してみましょう。
# 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()