แฮชแท็กและการกล่าวถึงในทวีตภาษารัสเซีย
กลับมาดู dataframe tweets ที่เก็บทวีตภาษารัสเซียกันอีกครั้ง ในแบบฝึกหัดนี้ จะได้คำนวณจำนวนแฮชแท็กและการกล่าวถึง (mention) ในแต่ละทวีต โดยนิยามฟังก์ชัน count_hashtags() และ count_mentions() ตามลำดับ แล้วนำไปใช้กับฟีเจอร์ content ของ tweets
ขอให้นึกถึงว่าทวีตทั้งหมดเก็บอยู่ในฟีเจอร์ content ของ tweets
แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร
Feature Engineering for NLP in 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()