Word vectors และ spaCy

การประมวลผลภาษาธรรมชาติด้วย spaCy

Azadeh Mobasher

Principal Data Scientist

การแสดงภาพ word vectors

  • Word vectors ช่วยให้เข้าใจว่าคำต่างๆ จัดกลุ่ม กันอย่างไร

 

ตัวอย่าง word vector

  • Principal Component Analysis ฉายภาพ word vectors ลงในพื้นที่สองมิติ

 

แสดงภาพ word vectors

การประมวลผลภาษาธรรมชาติด้วย spaCy

การแสดงภาพ word vectors

  • นำเข้าไลบรารีที่จำเป็นและโมเดล spaCy
import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
import numpy as np

nlp = spacy.load("en_core_web_md")
  • ดึง word vectors สำหรับรายการคำที่กำหนด แล้วนำมาเรียงซ้อนกันในแนวตั้ง
words = ["wonderful", "horrible", 
           "apple", "banana", "orange", "watermelon", 
           "dog", "cat"]
word_vectors = np.vstack([nlp.vocab.vectors[nlp.vocab.strings[w]] for w in words])
การประมวลผลภาษาธรรมชาติด้วย spaCy

การแสดงภาพ word vectors

  • ดึง principal components 2 ตัวโดยใช้ PCA
pca = PCA(n_components=2)
word_vectors_transformed = pca.fit_transform(word_vectors)

 

  • แสดง scatter plot ของ vectors ที่แปลงแล้ว
plt.figure(figsize=(10, 8))
plt.scatter(word_vectors_transformed[:, 0], word_vectors_transformed[:, 1])

for word, coord in zip(words, word_vectors_transformed): x, y = coord plt.text(x, y, word, size=10) plt.show()
การประมวลผลภาษาธรรมชาติด้วย spaCy

Analogies และการดำเนินการกับเวกเตอร์

  • ความสัมพันธ์เชิงความหมายระหว่างคู่คำ
  • Word embeddings สร้าง analogies เช่น เรื่องเพศและกาล:
    • queen - woman + man = king

Analogies และการดำเนินการกับเวกเตอร์

การประมวลผลภาษาธรรมชาติด้วย spaCy

คำที่คล้ายกันใน vocabulary

  • spaCy ค้นหาคำที่มีความหมายใกล้เคียงกับคำที่กำหนด
import numpy as np
import spacy
nlp = spacy.load("en_core_web_md")

word = "covid"

most_similar_words = nlp.vocab.vectors.most_similar( np.asarray([nlp.vocab.vectors[nlp.vocab.strings[word]]]), n=5) words = [nlp.vocab.strings[w] for w in most_similar_words[0][0]] print(words)
>>> ['Covi', 'CoVid', 'Covici', 'COVID-19', 'corona']
การประมวลผลภาษาธรรมชาติด้วย spaCy

มาฝึกกันเถอะ!

การประมวลผลภาษาธรรมชาติด้วย spaCy

Preparing Video For Download...