トークン分類

Pythonで学ぶ自然言語処理(NLP)

Fouad Trad

Machine Learning Engineer

テキスト分類 vs トークン分類

テキスト分類

  • 文全体やテキストペアを分類します

前の動画で示したテキスト分類とQNLIのタスクの図。

Pythonで学ぶ自然言語処理(NLP)

テキスト分類 vs トークン分類

テキスト分類

  • 文全体やテキストペアを分類します

前の動画で示したテキスト分類とQNLIのタスクの図。

トークン分類

  • 文中のトークンにラベルを付与します

文を単語ごとに分割し、各語が異なる色(異なるクラス)で示された図。

  • 固有表現抽出(NER)
  • 品詞(PoS)タグ付け
Pythonで学ぶ自然言語処理(NLP)

固有表現抽出(NER)

  • 人名、地名、組織、日付などの実体を特定します

文「Apple opened a new office in Toronto in March 2023」のNER解析。Appleは組織、Torontoは場所、March 2023は日付として認識。

  • 用途:
    • 情報検索
    • 質問応答
Pythonで学ぶ自然言語処理(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で学ぶ自然言語処理(NLP)

品詞(PoS)タグ付け

  • 各語に文法役割(名詞・動詞・形容詞)を付与します

文「The quick fox jumps over the lazy dog」の品詞タグ。"the" は限定詞、"quick" と "lazy" は形容詞、"fox" と "dogs" は名詞、"jumps" は動詞、"over" は前置詞。

  • 用途:
    • 構文解析
    • 文法誤り訂正
    • テキスト生成
Pythonで学ぶ自然言語処理(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で学ぶ自然言語処理(NLP)

Passons à la pratique !

Pythonで学ぶ自然言語処理(NLP)

Preparing Video For Download...