การรู้จำชื่อเฉพาะในข้อความที่ถอดเสียงแล้ว

Spoken Language Processing ด้วย Python

Daniel Bourke

Machine Learning Engineer/YouTube Creator

การติดตั้ง spaCy

# Install spaCy
$ pip install spacy
# Download spaCy language model
$ python -m spacy download en_core_web_sm
Spoken Language Processing ด้วย Python

การใช้งาน spaCy

import spacy

# Load spaCy language model nlp = spacy.load("en_core_web_sm")
# Create a spaCy doc
text = ("I'd like to talk about a smartphone I ordered on July 31st "
        "from your Sydney store, my order number is 4093829. I spoke "
        "to one of your customer service team, Georgia, yesterday.")
doc = nlp(text)
Spoken Language Processing ด้วย Python

โทเคนของ spaCy

# Show different tokens and positions
for token in doc:
  print(token.text, token.idx)
I 0
'd 1
like 4
to 9
talk 12
about 17
a 23
smartphone 25...
Spoken Language Processing ด้วย Python

ประโยคของ spaCy

# Show sentences in doc
for sentence in doc.sents:
  print(sentence)
I'd like to talk about a smartphone I ordered on July 31st from your Sydney store, 
my order number is 4093829.
I spoke to one of your customer service team, Georgia, yesterday.
Spoken Language Processing ด้วย Python

เอนทิตีที่มีชื่อเฉพาะของ spaCy

เอนทิตีที่มีชื่อเฉพาะที่มีอยู่ในตัวของ spaCy ได้แก่:

  • PERSON บุคคล รวมถึงตัวละครสมมติ
  • ORG บริษัท หน่วยงาน สถาบัน เป็นต้น
  • GPE ประเทศ เมือง รัฐ
  • PRODUCT วัตถุ ยานพาหนะ อาหาร เป็นต้น (ไม่รวมบริการ)
  • DATE วันที่หรือช่วงเวลา ทั้งแบบสัมบูรณ์และสัมพัทธ์
  • TIME เวลาที่สั้นกว่าหนึ่งวัน
  • MONEY มูลค่าเงิน รวมถึงหน่วยเงิน
  • CARDINAL ตัวเลขที่ไม่จัดอยู่ในประเภทอื่น
Spoken Language Processing ด้วย Python

เอนทิตีที่มีชื่อเฉพาะของ spaCy

# Find named entities in doc
for entity in doc.ents:
  print(entity.text, entity.label_)
July 31st DATE
Sydney GPE
4093829 CARDINAL
one CARDINAL
Georgia GPE
yesterday DATE
Spoken Language Processing ด้วย Python

เอนทิตีที่มีชื่อเฉพาะแบบกำหนดเอง

# Check spaCy pipeline
print(nlp.pipe_names)
['tok2vec', 'tagger', 'parser',
 'attribute_ruler', 'lemmatizer', 'ner']
Spoken Language Processing ด้วย Python

การเปลี่ยนไปป์ไลน์

# Add entity_ruler to the pipeline, before ner
ruler = nlp.add_pipe("entity_ruler", before="ner")
# Add token pattern to ruler
ruler.add_patterns([{"label":"PRODUCT", "pattern": "smartphone"}])
# Check updated pipeline
nlp.pipe_names
Spoken Language Processing ด้วย Python

การเปลี่ยนไปป์ไลน์

['tok2vec', 'tagger', 'parser', 'attribute_ruler',
 'lemmatizer', 'entity_ruler', 'ner']
Spoken Language Processing ด้วย Python

ทดสอบไปป์ไลน์ใหม่

# Recreate the doc with the updated pipeline
doc = nlp(text)
# Test new entity rule
for entity in doc.ents:
  print(entity.text, entity.label_)
smartphone PRODUCT
July 31st DATE
Sydney GPE
4093829 CARDINAL
one CARDINAL
Georgia GPE
yesterday DATE
Spoken Language Processing ด้วย Python

มาลุยฝึกใช้งาน spaCy กันเถอะ!

Spoken Language Processing ด้วย Python

Preparing Video For Download...