เริ่มต้นใช้งานเริ่มต้นใช้งานได้ฟรี

ทำความสะอาดข้อมูล TED Talks ใน DataFrame

ในแบบฝึกหัดนี้ เราจะกลับมาดู TED Talks จากบทแรกอีกครั้ง คุณได้รับ DataFrame ชื่อ ted ซึ่งประกอบด้วย TED Talks จำนวน 5 รายการ โดยมีหน้าที่ทำความสะอาดข้อมูลเหล่านี้ด้วยเทคนิคที่ได้เรียนไปก่อนหน้า โดยเขียนฟังก์ชัน preprocess แล้วนำไปใช้กับฟีเจอร์ transcript ของ DataFrame

รายการ stopwords พร้อมใช้งานในตัวแปร stopwords

แบบฝึกหัดนี้เป็นส่วนหนึ่งของหลักสูตร

Feature Engineering for NLP in Python

ดูคอร์ส

คำแนะนำการฝึกหัด

  • สร้าง Doc object จาก text โดยไม่ต้องสนใจอาร์กิวเมนต์ disable ในตอนนี้
  • สร้าง lemmas โดยใช้ list comprehension ร่วมกับแอตทริบิวต์ lemma_
  • กรองอักขระที่ไม่ใช่ตัวอักษรออกโดยใช้ isalpha() ในเงื่อนไข if

แบบฝึกหัดเชิงโต้ตอบแบบลงมือทำ

ลองทำแบบฝึกหัดนี้โดยเติมโค้ดตัวอย่างนี้ให้สมบูรณ์

# 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'])
แก้ไขและรันโค้ด