开始使用免费开始使用

俄语推文中的 Hashtag 和提及

让我们回到包含俄语推文的 tweets 数据框。本练习中,您将分别定义两个函数 count_hashtags()count_mentions(),并将它们应用到 tweetscontent 特征,以计算每条推文中的 hashtag 数量和提及数量。

提醒一下,推文文本保存在 tweetscontent 特征中。

本练习是课程的一部分

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()
编辑并运行代码