시작하기무료로 시작하기

러시아어 트윗의 해시태그와 멘션

러시아어 트윗이 담긴 tweets 데이터프레임을 다시 살펴보겠습니다. 이 연습에서는 각 트윗의 해시태그와 멘션 개수를 계산합니다. 이를 위해 두 함수를 각각 count_hashtags()count_mentions()로 정의하고, tweetscontent 특성에 적용하세요.

기억이 잘 나지 않는다면, 트윗 본문은 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()
코드 편집 및 실행