開始使用免費開始

俄文推文中的 Hashtag 與提及

讓我們回到包含俄文推文的 tweets DataFrame。這個練習中,你將分別定義兩個函式 count_hashtags()count_mentions(),用來計算每則推文中的 hashtag 與提及數量,並將它們套用到 tweetscontent 特徵上。

如果你不記得了,推文文字都在 tweetscontent 特徵中。

本練習屬於課程

Python 中文本特徵工程

檢視課程

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# 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()
編輯並執行程式碼