Token 分類

Python 的 Natural Language Processing(NLP)

Fouad Trad

Machine Learning Engineer

文字分類 vs. Token 分類

文字分類

  • 對整個句子或成對文字進行分類

顯示先前影片中的文字分類與 QNLI 任務的圖片。

Python 的 Natural Language Processing(NLP)

文字分類 vs. Token 分類

文字分類

  • 對整個句子或成對文字進行分類

顯示先前影片中的文字分類與 QNLI 任務的圖片。

Token 分類

  • 對句子中的各個 token 指派標籤

顯示一句話被分成單詞、各自以不同顏色代表不同類別的圖片。

  • 命名實體辨識(NER)
  • 詞性(PoS)標註
Python 的 Natural Language Processing(NLP)

命名實體辨識(NER)

  • 辨識姓名、地點、組織、日期等實體

顯示句子「Apple opened a new office in Toronto in March 2023」的 NER 分析,其中 Apple 為組織、Toronto 為地點、March 2023 為日期。

  • 常見應用:
    • 資訊擷取
    • 問答系統
Python 的 Natural Language Processing(NLP)

程式中的 NER

from transformers import pipeline

ner_pipeline = pipeline(task="ner",
model="dslim/bert-base-NER",
grouped_entities=True)
ner_results = ner_pipeline("Zara Venn established NovaCore Dynamics in London.")
print(ner_results)
[{'entity_group': 'PER', 'score': np.float32(0.99840075), 'word': 'Zara Venn', 'start': 0, 'end': 9}, 
 {'entity_group': 'ORG', 'score': np.float32(0.99875560), 'word': 'NovaCore Dynamics', 'start': 21, 'end': 38}, 
 {'entity_group': 'LOC', 'score': np.float32(0.99960726), 'word': 'London', 'start': 42, 'end': 48}]
Python 的 Natural Language Processing(NLP)

詞性(PoS)標註

  • 為每個單詞指派文法角色(名詞、動詞、形容詞)

顯示句子「The quick fox jumps over the lazy dog」的詞性標註,其中「the」為限定詞,「quick」與「lazy」為形容詞,「fox」與「dogs」為名詞,「jumps」為動詞,「over」為介系詞。

  • 常見應用:
    • 句法剖析
    • 文法更正
    • 文字生成
Python 的 Natural Language Processing(NLP)

程式中的 PoS 標註

pos_pipeline = pipeline(task="token-classification",

model="vblagoje/bert-english-uncased-finetuned-pos",
grouped_entities=True)
pos_results = pos_pipeline("Zara Venn established NovaCore Dynamics in London.") print(pos_results)
[{'entity_group': 'PROPN', 'score': np.float32(0.9982983), 'word': 'zara venn', 'start': 0, 'end': 9},  
 {'entity_group': 'VERB', 'score': np.float32(0.99940944), 'word': 'established', 'start': 10, 'end': 21},  
 {'entity_group': 'PROPN', 'score': np.float32(0.99455726), 'word': 'novacore dynamics', 'start': 22, 'end': 39},  
 {'entity_group': 'ADP', 'score': np.float32(0.99935526), 'word': 'in', 'start': 40, 'end': 42},  
 {'entity_group': 'PROPN', 'score': np.float32(0.99847955), 'word': 'london', 'start': 43, 'end': 49}]
Python 的 Natural Language Processing(NLP)

一起來練習吧!

Python 的 Natural Language Processing(NLP)

Preparing Video For Download...