5  Word Tokenization

5.1 Word tokenization with LatinCy

# Imports & setup

import spacy
from pprint import pprint
nlp = spacy.load('la_core_web_sm')
text = "Haec narrantur a poetis de Perseo. Perseus filius erat Iovis, maximi deorum. Avus eius Acrisius appellabatur."
doc = nlp(text)
print(doc)
Haec narrantur a poetis de Perseo. Perseus filius erat Iouis, maximi deorum. Auus eius Acrisius appellabatur.

Word tokenization is the task of splitting a text into words (and wordlike units like punctuation, numbers, etc.). For the LatinCy models, tokenization is the fundamental pipeline component on which all other components depend. SpaCy uses non-destructive, “canonical” tokenization, i.e. non-destructive, in that the original text can be untokenized, so to speak, based on Token annotations and canonical in that indices are assigned to each token during this process and these indices are used to refer to the tokens in other annotations. (Tokens can be separated or merged, but this requires the user to actively undo and redefine the tokenization output.) LatinCy uses a modified version of the default spaCy tokenizer that recognizes and splits enlitic -que using a rules-based process. (NB: It is in the LatinCy development plan to move enclitic splitting to a separate post-tokenization component.)

The spaCy Doc object is an iterable and tokens are the iteration unit.

tokens = [item for item in doc]
print(tokens)
[Haec, narrantur, a, poetis, de, Perseo, ., Perseus, filius, erat, Iouis, ,, maximi, deorum, ., Auus, eius, Acrisius, appellabatur, .]
token = tokens[0]
print(type(token))
<class 'spacy.tokens.token.Token'>

The text content of a Token object can be retrieved with the text attribute.

for i, token in enumerate(tokens, 1):
    print(f'{i}: {token.text}')
1: Haec
2: narrantur
3: a
4: poetis
5: de
6: Perseo
7: .
8: Perseus
9: filius
10: erat
11: Iouis
12: ,
13: maximi
14: deorum
15: .
16: Auus
17: eius
18: Acrisius
19: appellabatur
20: .

Note again that the token itself is a spaCy Token object and that the text attribute returns a Python string even though their representations in the Jupyter Notebook look the same.

token = tokens[0]
print(f'{type(token)} -> {token}')
print(f'{type(token.text)} -> {token.text}')
<class 'spacy.tokens.token.Token'> -> Haec
<class 'str'> -> Haec

5.1.2 Normalization at the tokenization stage

In order to start the pipeline processing with text data that is as close as possible to the data used for training all of the downstream components, the LatinCy models include a certain amount of normalization at the tokenization stage. These four normalization functions run at tokenization: 1. macron removal; 2. accent removal; 3. ligature separation; and 4. space adjustment (i.e. converting all whitespace to a single space and stripping whitespace from the left and right side of the input text).

text = " Hæc     peritè\n\nnarrāntur\t\ta poētīs dē Perseō Acrisiōque."
doc = nlp(text)

print("Before normalization:")
print(text, "\n")

print("After normalization:")
print(doc.text)
Before normalization:
 Hæc     peritè

narrāntur       a poētīs dē Perseō Acrisiōque. 

After normalization:
Haec perite narrantur a poetis de Perseo Acrisioque.

References

SLP Chapter 2, Section 2.8 “Rule-based tokenization” link