Bắt đầu ngayBắt đầu miễn phí

Stems from tweets

In this exercise, you will work with an array called tweets. It contains the text of the airline sentiment data collected from Twitter.

Your task is to work with this array and transform it into a list of tokens using list comprehension. After that, iterate over the list of tokens and create a stem out of each token. Remember that list comprehensions are a one-line alternative to for loops.

Bài tập này là một phần của khóa học

Sentiment Analysis in Python

Xem khóa học

Hướng dẫn bài tập

  • Import the function we used to transform strings into stems.
  • Call the Porter stemmer function you just imported.
  • Using a list comprehension, create the list tokens. It should contain all the word tokens from the tweets array.
  • Iterate over the tokens list and apply the stemming function to each item in the list.

Bài tập tương tác thực hành trực tiếp

Hãy thử làm bài tập này bằng cách hoàn thành đoạn mã mẫu này.

# Import the function to perform stemming
____
from nltk import word_tokenize

# Call the stemmer
porter = ____()

# Transform the array of tweets to tokens
tokens = [____]
# Stem the list of tokens
stemmed_tokens = [[____.____(word) for word in tweet] for tweet in tokens] 
# Print the first element of the list
print(stemmed_tokens[0])
Chỉnh sửa và Chạy Mã