Tokenclassificatie

Natural Language Processing (NLP) in Python

Fouad Trad

Machine Learning Engineer

Tekst versus tokenclassificatie

Tekstclassificatie

  • Classificeert volledige zinnen of tekstparen

Afbeelding met de taken tekstclassificatie en QNLI, getoond in eerdere video’s.

Natural Language Processing (NLP) in Python

Tekst versus tokenclassificatie

Tekstclassificatie

  • Classificeert volledige zinnen of tekstparen

Afbeelding met de taken tekstclassificatie en QNLI, getoond in eerdere video’s.

Tokenclassificatie

  • Wijst labels toe aan tokens binnen een zin

Afbeelding van een zin opgesplitst in woorden, elk met een andere kleur als klasse.

  • Named entity recognition (NER)
  • Woordsoorttagging (PoS)
Natural Language Processing (NLP) in Python

Named entity recognition (NER)

  • Herkent entiteiten zoals namen, locaties, organisaties, datums, enz.

Afbeelding met NER-analyse van de zin "Apple opened a new office in Toronto in March 2023", waarbij Apple organisatie is, Toronto locatie en March 2023 een datum.

  • Handig voor:
    • Informatieopvraging
    • Vraagbeantwoording
Natural Language Processing (NLP) in Python

NER in code

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}]
Natural Language Processing (NLP) in Python

Woordsoorttagging (PoS)

  • Wijst per woord een grammaticale rol toe (zelfstandig naamwoord, werkwoord, bijvoeglijk naamwoord)

Afbeelding met PoS-tags voor de zin "The quick fox jumps over the lazy dog", waar "the" een determiner is, "quick" en "lazy" bijvoeglijke naamwoorden zijn, "fox" en "dogs" zelfstandige naamwoorden, "jumps" een werkwoord en "over" een voorzetsel.

  • Handig voor:
    • Syntactische ontleding
    • Grammaticacorrectie
    • Tekstgeneratie
Natural Language Processing (NLP) in Python

PoS-tagging in code

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}]
Natural Language Processing (NLP) in Python

Laten we oefenen!

Natural Language Processing (NLP) in Python

Preparing Video For Download...