開始使用免費開始

清理資料框中的 TED 演講

在這個練習中,我們會重溫第 1 章的 TED Talks。你拿到了一個包含 5 段 TED 演講的資料框 ted。你的任務是撰寫函式 preprocess,並將它套用到資料框的 transcript 特徵上,依照前面介紹的技巧來清理這些演講內容。

停用詞清單已提供為 stopwords

本練習屬於課程

Python 中文本特徵工程

檢視課程

練習說明

  • text 產生 Doc 物件。先忽略 disable 這個引數。
  • 使用串列生成式與 lemma_ 屬性來產生詞形還原結果(lemmas)。
  • 在 if 條件式中使用 isalpha() 移除非英文字母的字元。

動手互動練習

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

# Function to preprocess text
def preprocess(text):
  	# Create Doc object
    doc = nlp(____, disable=['ner', 'parser'])
    # Generate lemmas
    lemmas = [token.____ for token in doc]
    # Remove stopwords and non-alphabetic characters
    a_lemmas = [lemma for lemma in lemmas 
            if lemma.____ and lemma not in stopwords]
    
    return ' '.join(a_lemmas)
  
# Apply preprocess to ted['transcript']
ted['transcript'] = ted['transcript'].apply(____)
print(ted['transcript'])
編輯並執行程式碼