NLP data processing; tokenizes text and creates vocab indexes

NLP Preprocessing

text.tranform contains the functions that deal behind the scenes with the two main tasks when preparing texts for modelling: tokenization and numericalization.

Tokenization splits the raw texts into tokens (wich can be words, or punctuation signs...). The most basic way to do this would be to separate according to spaces, but it's possible to be more subtle; for instance, the contractions like "isn't" or "don't" should be split in ["is","n't"] or ["do","n't"]. By default fastai will use the powerful spacy tokenizer.

Numericalization is easier as it just consists in attributing a unique id to each token and mapping each of those tokens to their respective ids.

Tokenization

Introduction

This step is actually divided in two phases: first, we apply a certain list of rules to the raw texts as preprocessing, then we use the tokenizer to split them in lists of tokens. Combining together those rules, the tok_funcand the lang to process the texts is the role of the Tokenizer class.

class Tokenizer[source]

Tokenizer(`tok_func`:Callable=`'SpacyTokenizer'`, `lang`:str=`'en'`, `pre_rules`:ListRules=`None`, `post_rules`:ListRules=`None`, `special_cases`:StrList=`None`, `n_cpus`:int=`None`)

Put together rules and a tokenizer function to tokenize text with multiprocessing.

This class will process texts by appling them the pre_rules, tokenizing them with tok_func(lang) and then applying the post_rules. special_cases are a list of tokens passed as special to the tokenizer and n_cpus is the number of cpus to use for multi-processing (by default, half the cpus available). We don't directly pass a tokenizer for multi-processing purposes: each process needs to initiate a tokenizer of its own. The rules and special_cases default to:

defaults.text_pre_rules = [fix_html, replace_rep, replace_wrep, spec_add_spaces, rm_useless_spaces]

defaults.text_post_rules = [replace_all_caps, deal_caps]

and

defaults.text_spec_tok = [UNK,PAD,BOS,FLD,TK_MAJ,TK_UP,TK_REP,TK_WREP]

The rules are all listed below, here is the meaning of the special tokens:

  • UNK (xxunk) is for an unknown word (one that isn't present in the current vocabulary)
  • PAD (xxpad) is the token used for padding, if we need to regroup several texts of different lengths in a batch
  • BOS (xxbos) represents the beginning of a text in your dataset
  • FLD (xxfld) is used if you set mark_fields=True in your TokenizeProcessor to separate the different fields of texts (if your texts are loaded from several columns in a dataframe)
  • TK_MAJ (xxmaj) is used to indicate the next word begins with a capital in the original text
  • TK_UP (xxup) is used to indicate the next word is written in all caps in the original text
  • TK_REP (xxrep) is used to indicate the next character is repeated n times in the original text (usage xxrep n {char})
  • TK_WREP(xxwrep) is used to indicate the next word is repeated n times in the original text (usage xxwrep n {word})

process_text[source]

process_text(`t`:str, `tok`:BaseTokenizer) → List[str]

Process one text t with tokenizer tok.

process_all[source]

process_all(`texts`:StrList) → List[List[str]]

Process a list of texts.

For an example, we're going to grab some IMDB reviews.

path = untar_data(URLs.IMDB_SAMPLE)
path
PosixPath('/home/ubuntu/.fastai/data/imdb_sample')
df = pd.read_csv(path/'texts.csv', header=None)
example_text = df.iloc[2][1]; example_text
'This is a extremely well-made film. The acting, script and camera-work are all first-rate. The music is good, too, though it is mostly early in the film, when things are still relatively cheery. There are no really superstars in the cast, though several faces will be familiar. The entire cast does an excellent job with the script.<br /><br />But it is hard to watch, because there is no good end to a situation like the one presented. It is now fashionable to blame the British for setting Hindus and Muslims against each other, and then cruelly separating them into two countries. There is some merit in this view, but it\'s also true that no one forced Hindus and Muslims in the region to mistreat each other as they did around the time of partition. It seems more likely that the British simply saw the tensions between the religions and were clever enough to exploit them to their own ends.<br /><br />The result is that there is much cruelty and inhumanity in the situation and this is very unpleasant to remember and to see on the screen. But it is never painted as a black-and-white case. There is baseness and nobility on both sides, and also the hope for change in the younger generation.<br /><br />There is redemption of a sort, in the end, when Puro has to make a hard choice between a man who has ruined her life, but also truly loved her, and her family which has disowned her, then later come looking for her. But by that point, she has no option that is without great pain for her.<br /><br />This film carries the message that both Muslims and Hindus have their grave faults, and also that both can be dignified and caring people. The reality of partition makes that realisation all the more wrenching, since there can never be real reconciliation across the India/Pakistan border. In that sense, it is similar to "Mr & Mrs Iyer".<br /><br />In the end, we were glad to have seen the film, even though the resolution was heartbreaking. If the UK and US could deal with their own histories of racism with this kind of frankness, they would certainly be better off.'
tokenizer = Tokenizer()
tok = SpacyTokenizer('en')
' '.join(tokenizer.process_text(example_text, tok))
'xxmaj this is a extremely well - made film . xxmaj the acting , script and camera - work are all first - rate . xxmaj the music is good , too , though it is mostly early in the film , when things are still relatively cheery . xxmaj there are no really superstars in the cast , though several faces will be familiar . xxmaj the entire cast does an excellent job with the script . \n\n xxmaj but it is hard to watch , because there is no good end to a situation like the one presented . xxmaj it is now fashionable to blame the xxmaj british for setting xxmaj hindus and xxmaj muslims against each other , and then cruelly separating them into two countries . xxmaj there is some merit in this view , but it \'s also true that no one forced xxmaj hindus and xxmaj muslims in the region to mistreat each other as they did around the time of partition . xxmaj it seems more likely that the xxmaj british simply saw the tensions between the religions and were clever enough to exploit them to their own ends . \n\n xxmaj the result is that there is much cruelty and inhumanity in the situation and this is very unpleasant to remember and to see on the screen . xxmaj but it is never painted as a black - and - white case . xxmaj there is baseness and nobility on both sides , and also the hope for change in the younger generation . \n\n xxmaj there is redemption of a sort , in the end , when xxmaj puro has to make a hard choice between a man who has ruined her life , but also truly loved her , and her family which has disowned her , then later come looking for her . xxmaj but by that point , she has no option that is without great pain for her . \n\n xxmaj this film carries the message that both xxmaj muslims and xxmaj hindus have their grave faults , and also that both can be dignified and caring people . xxmaj the reality of partition makes that realisation all the more wrenching , since there can never be real reconciliation across the xxmaj india / xxmaj pakistan border . xxmaj in that sense , it is similar to " xxmaj mr & xxmaj mrs xxmaj iyer " . \n\n xxmaj in the end , we were glad to have seen the film , even though the resolution was heartbreaking . xxmaj if the xxup uk and xxup us could deal with their own histories of racism with this kind of frankness , they would certainly be better off .'

As explained before, the tokenizer split the text according to words/punctuations signs but in a smart manner. The rules (see below) also have modified the text a little bit. We can tokenize a list of texts directly at the same time:

df = pd.read_csv(path/'texts.csv', header=None)
texts = df[1].values
tokenizer = Tokenizer()
tokens = tokenizer.process_all(texts)
' '.join(tokens[2])
'xxmaj this is a extremely well - made film . xxmaj the acting , script and camera - work are all first - rate . xxmaj the music is good , too , though it is mostly early in the film , when things are still relatively cheery . xxmaj there are no really superstars in the cast , though several faces will be familiar . xxmaj the entire cast does an excellent job with the script . \n\n xxmaj but it is hard to watch , because there is no good end to a situation like the one presented . xxmaj it is now fashionable to blame the xxmaj british for setting xxmaj hindus and xxmaj muslims against each other , and then cruelly separating them into two countries . xxmaj there is some merit in this view , but it \'s also true that no one forced xxmaj hindus and xxmaj muslims in the region to mistreat each other as they did around the time of partition . xxmaj it seems more likely that the xxmaj british simply saw the tensions between the religions and were clever enough to exploit them to their own ends . \n\n xxmaj the result is that there is much cruelty and inhumanity in the situation and this is very unpleasant to remember and to see on the screen . xxmaj but it is never painted as a black - and - white case . xxmaj there is baseness and nobility on both sides , and also the hope for change in the younger generation . \n\n xxmaj there is redemption of a sort , in the end , when xxmaj puro has to make a hard choice between a man who has ruined her life , but also truly loved her , and her family which has disowned her , then later come looking for her . xxmaj but by that point , she has no option that is without great pain for her . \n\n xxmaj this film carries the message that both xxmaj muslims and xxmaj hindus have their grave faults , and also that both can be dignified and caring people . xxmaj the reality of partition makes that realisation all the more wrenching , since there can never be real reconciliation across the xxmaj india / xxmaj pakistan border . xxmaj in that sense , it is similar to " xxmaj mr & xxmaj mrs xxmaj iyer " . \n\n xxmaj in the end , we were glad to have seen the film , even though the resolution was heartbreaking . xxmaj if the xxup uk and xxup us could deal with their own histories of racism with this kind of frankness , they would certainly be better off .'

Customize the tokenizer

The tok_func must return an instance of BaseTokenizer:

class BaseTokenizer[source]

BaseTokenizer(`lang`:str)

Basic class for a tokenizer function.

tokenizer[source]

tokenizer(`t`:str) → List[str]

Take a text t and returns the list of its tokens.

add_special_cases[source]

add_special_cases(`toks`:StrList)

Record a list of special tokens toks.

The fastai library uses spacy tokenizers as its default. The following class wraps it as BaseTokenizer.

class SpacyTokenizer[source]

SpacyTokenizer(`lang`:str) :: BaseTokenizer

Wrapper around a spacy tokenizer to make it a BaseTokenizer.

If you want to use your custom tokenizer, just subclass the BaseTokenizer and override its tokenizer and add_spec_cases functions.

Rules

Rules are just functions that take a string and return the modified string. This allows you to customize the list of default_pre_rules or default_post_rules as you please. Those are:

deal_caps[source]

deal_caps(`x`:StrList) → StrList

In t, every word is lower-casse. If a word begins with a capital, we put a token TK_MAJ in front of it.

fix_html[source]

fix_html(`x`:str) → str

This rules replaces a bunch of HTML characters or norms in plain text ones. For instance <br /> are replaced by \n, &nbsp; by spaces etc...

fix_html("Some HTML&nbsp;text<br />")
'Some HTML& text\n'

replace_all_caps[source]

replace_all_caps(`x`:StrList) → StrList

Add TK_UP for words in all caps in x.

replace_rep[source]

replace_rep(`t`:str) → str

Whenever a character is repeated more than three times in t, we replace the whole thing by 'TK_REP n char' where n is the number of occurences and char the character.

replace_rep("I'm so excited!!!!!!!!")
"I'm so excited xxrep 8 ! "

replace_wrep[source]

replace_wrep(`t`:str) → str

Whenever a word is repeated more than four times in t, we replace the whole thing by 'TK_WREP n w' where n is the number of occurences and w the word repeated.

replace_wrep("I've never ever ever ever ever ever ever ever done this.")
"I've never  xxwrep 7 ever  done this."

rm_useless_spaces[source]

rm_useless_spaces(`t`:str) → str

Remove multiple spaces in t.

rm_useless_spaces("Inconsistent   use  of     spaces.")
'Inconsistent use of spaces.'

spec_add_spaces[source]

spec_add_spaces(`t`:str) → str

Add spaces around / and # in t.

spec_add_spaces('I #like to #put #hashtags #everywhere!')
'I  # like to  # put  # hashtags  # everywhere!'

Numericalization

To convert our set of tokens to unique ids (and be able to have them go through embeddings), we use the following class:

class Vocab[source]

Vocab(`itos`:StrList)

Contain the correspondance between numbers and tokens and numericalize.

itos contains the id to token correspondance.

create[source]

create(`tokens`:Tokens, `max_vocab`:int, `min_freq`:int) → Vocab

Create a vocabulary from a set of tokens.

Only keeps max_vocab tokens, and only if they appear at least min_freq times, set the rest to UNK.

numericalize[source]

numericalize(`t`:StrList) → List[int]

Convert a list of tokens t to their ids.

textify[source]

textify(`nums`:Collection[int], `sep`=`' '`) → List[str]

Convert a list of nums to their tokens.

vocab = Vocab.create(tokens, max_vocab=1000, min_freq=2)
vocab.numericalize(tokens[2])[:10]
[4, 20, 15, 12, 623, 89, 23, 115, 31, 10]