text.transform

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 (which 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][test]

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

  • pytest -sv tests/test_text_transform.py::test_tokenize [source]
  • pytest -sv tests/test_text_transform.py::test_tokenize_handles_empty_lines [source]
  • pytest -sv tests/test_text_transform.py::test_tokenize_ignores_extraneous_space [source]

To run tests please refer to this guide.

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

This class will process texts by applying 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]

[source]

defaults.text_post_rules = [replace_all_caps, deal_caps]

[source]

and

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

[source]

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][test]

process_text(t:str, tok:BaseTokenizer) → List[str] No tests found for process_text. To contribute a test please refer to this guide and this discussion.

Process one text t with tokenizer tok.

process_all[source][test]

process_all(texts:StrList) → List[List[str]] Tests found for process_all:

Some other tests where process_all is used:

  • pytest -sv tests/test_text_transform.py::test_tokenize [source]
  • pytest -sv tests/test_text_transform.py::test_tokenize_handles_empty_lines [source]
  • pytest -sv tests/test_text_transform.py::test_tokenize_ignores_extraneous_space [source]

To run tests please refer to this guide.

Process a list of texts.

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

  1. path = untar_data(URLs.IMDB_SAMPLE)
  2. path
  1. PosixPath('/home/ubuntu/.fastai/data/imdb_sample')
  1. df = pd.read_csv(path/'texts.csv', header=None)
  2. example_text = df.iloc[2][1]; example_text
  1. '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.'
  1. tokenizer = Tokenizer()
  2. tok = SpacyTokenizer('en')
  3. ' '.join(tokenizer.process_text(example_text, tok))
  1. '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 . nn 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 . nn 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 . nn 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 . nn 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 " . nn 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:

  1. df = pd.read_csv(path/'texts.csv', header=None)
  2. texts = df[1].values
  3. tokenizer = Tokenizer()
  4. tokens = tokenizer.process_all(texts)
  5. ' '.join(tokens[2])
  1. '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 . nn 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 . nn 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 . nn 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 . nn 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 " . nn 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][test]

BaseTokenizer(lang:str) No tests found for BaseTokenizer. To contribute a test please refer to this guide and this discussion.

Basic class for a tokenizer function.

tokenizer[source][test]

tokenizer(t:str) → List[str] Tests found for tokenizer:

Some other tests where tokenizer is used:

  • pytest -sv tests/test_text_transform.py::test_tokenize [source]
  • pytest -sv tests/test_text_transform.py::test_tokenize_handles_empty_lines [source]
  • pytest -sv tests/test_text_transform.py::test_tokenize_ignores_extraneous_space [source]

To run tests please refer to this guide.

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

add_special_cases[source][test]

add_special_cases(toks:StrList) No tests found for add_special_cases. To contribute a test please refer to this guide and this discussion.

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][test]

SpacyTokenizer(lang:str) :: BaseTokenizer No tests found for SpacyTokenizer. To contribute a test please refer to this guide and this discussion.

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][test]

deal_caps(x:StrList) → StrList Tests found for deal_caps:

  • pytest -sv tests/test_text_transform.py::test_rules [source]

To run tests please refer to this guide.

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

fix_html[source][test]

fix_html(x:str) → str Tests found for fix_html:

  • pytest -sv tests/test_text_transform.py::test_rules [source]

To run tests please refer to this guide.

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…

  1. fix_html("Some HTML&nbsp;text<br />")
  1. 'Some HTML& textn'

replace_all_caps[source][test]

replace_all_caps(x:StrList) → StrList Tests found for replace_all_caps:

  • pytest -sv tests/test_text_transform.py::test_rules [source]

To run tests please refer to this guide.

Replace tokens in ALL CAPS in x by their lower version and add TK_UP before.

replace_rep[source][test]

replace_rep(t:str) → str Tests found for replace_rep:

  • pytest -sv tests/test_text_transform.py::test_rules [source]

To run tests please refer to this guide.

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 occurrences and char the character.

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

replace_wrep[source][test]

replace_wrep(t:str) → str Tests found for replace_wrep:

  • pytest -sv tests/test_text_transform.py::test_rules [source]

To run tests please refer to this guide.

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 occurrences and w the word repeated.

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

rm_useless_spaces[source][test]

rm_useless_spaces(t:str) → str Tests found for rm_useless_spaces:

  • pytest -sv tests/test_text_transform.py::test_rules [source]

To run tests please refer to this guide.

Remove multiple spaces in t.

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

spec_add_spaces[source][test]

spec_add_spaces(t:str) → str Tests found for spec_add_spaces:

  • pytest -sv tests/test_text_transform.py::test_rules [source]

To run tests please refer to this guide.

Add spaces around / and # in t.

  1. spec_add_spaces('I #like to #put #hashtags #everywhere!')
  1. '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][test]

Vocab(itos:StrList) Tests found for Vocab:

Some other tests where Vocab is used:

  • pytest -sv tests/test_text_transform.py::test_numericalize_and_textify [source]

To run tests please refer to this guide.

Contain the correspondence between numbers and tokens and numericalize.

itos contains the id to token correspondence.

create[source][test]

create(tokens:Tokens, max_vocab:int, min_freq:int) → Vocab Tests found for create:

Some other tests where create is used:

  • pytest -sv tests/test_text_transform.py::test_numericalize_and_textify [source]

To run tests please refer to this guide.

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][test]

numericalize(t:StrList) → List[int] Tests found for numericalize:

  • pytest -sv tests/test_text_transform.py::test_numericalize_and_textify [source]

To run tests please refer to this guide.

Convert a list of tokens t to their ids.

textify[source][test]

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

  • pytest -sv tests/test_text_transform.py::test_numericalize_and_textify [source]

To run tests please refer to this guide.

Convert a list of nums to their tokens.

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

Company logo

©2021 fast.ai. All rights reserved.
Site last generated: Jan 5, 2021