spaCy pipelines

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

Azadeh Mobasher

Principal Data Scientist

spaCy pipelines

 

  • spaCy แปลงข้อความเป็น token ก่อน เพื่อสร้างออบเจกต์ Doc
  • จากนั้น Doc จะถูกประมวลผลผ่านหลายขั้นตอนใน processing pipeline

 

import spacy
nlp = spacy.load("en_core_web_sm")

doc = nlp(example_text)
การประมวลผลภาษาธรรมชาติด้วย spaCy

spaCy pipelines

  • pipeline คือ ลำดับของ pipe หรือ ตัวดำเนินการบนข้อมูล
  • NER pipeline ของ spaCy:
    • Tokenization
    • การระบุ named entity
    • การจำแนกประเภท named entity  

ตัวอย่าง spaCy pipeline สำหรับ NER

print([ent.text for ent in doc.ents])
การประมวลผลภาษาธรรมชาติด้วย spaCy

การเพิ่ม pipe

 

  • sentencizer: คอมโพเนนต์ใน spaCy pipeline สำหรับการแบ่งประโยค
text = " ".join(["This is a test sentence."]*10000)

en_core_sm_nlp = spacy.load("en_core_web_sm") start_time = time.time() doc = en_core_sm_nlp(text)
print(f"Finished processing with en_core_web_sm model in {round((time.time() - start_time)/60.0 , 5)} minutes")
>>> Finished processing with en_core_web_sm model in 0.09332 minutes
การประมวลผลภาษาธรรมชาติด้วย spaCy

การเพิ่ม pipe

 

  • สร้างโมเดลเปล่าและเพิ่ม pipe sentencizer:
blank_nlp = spacy.blank("en")

blank_nlp.add_pipe("sentencizer")
start_time = time.time() doc = blank_nlp(text) print(f"Finished processing with blank model in {round((time.time() - start_time)/60.0 , 5)} minutes")
>>> Finished processing with blank model in 0.00091 minutes
การประมวลผลภาษาธรรมชาติด้วย spaCy

การวิเคราะห์คอมโพเนนต์ใน pipeline

  • nlp.analyze_pipes() วิเคราะห์ spaCy pipeline เพื่อตรวจสอบ:
    • แอตทริบิวต์ที่คอมโพเนนต์ใน pipeline กำหนด
    • คะแนนที่คอมโพเนนต์ผลิตระหว่างการฝึก
    • การมีอยู่ของแอตทริบิวต์ที่จำเป็นทั้งหมด

 

  • การตั้งค่า pretty เป็น True จะพิมพ์ผลเป็นตาราง แทนที่จะส่งคืนเฉพาะข้อมูลเชิงโครงสร้าง
import spacy

nlp = spacy.load("en_core_web_sm")
analysis = nlp.analyze_pipes(pretty=True)
การประมวลผลภาษาธรรมชาติด้วย spaCy

การวิเคราะห์คอมโพเนนต์ใน pipeline

ผลลัพธ์ของเมธอด analyze_pipes

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

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

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

Preparing Video For Download...