liveBook · Manning

Vocabulary

As explained in Natural Language Processing in Action, Second Edition

A lexicon is a collection or index of words and other lexical sequences known to a language-processing system.

A vocabulary, or lexicon, is the set of tokens a language-processing system recognizes. Think of it as an index: each entry gives the system a place to record information about a word or another sequence. A corpus is the collection of documents from which such an index can be built. section 1.5.3 section 2.1

Tokens need not be whole words. They can include punctuation, characters, or pieces of words. Subword vocabularies can represent a new word using familiar pieces; if all its characters are already in the vocabulary, a subword tokenizer need not use an out-of-vocabulary token for it. section 2.1 section 2.2.1

To build a vocabulary for several documents, tokenize each one and take the union of their unique tokens. Choices such as lowercasing affect which entries are distinct: case folding combines capitalized and lowercase forms, reducing the vocabulary size. section 3.2

Listing 3.1 Short documents about bias
>>> docs = [nlp(s) for s in bias_intro.split('\n')
...         if s.strip()]                    #1
>>> counts = []
>>> for doc in docs:
...     counts.append(Counter([
...         t.text.lower() for t in doc]))    #2
>>> df = pd.DataFrame(counts)
>>> df = df.fillna(0).astype(int)    #3
>>> len(df)
16
>>> df.head()
  algorithmic bias describes  systematic  ... between  same service
0           1    1         1           1  ...       0     0       0
1           0    1         0           0  ...       0     0       0
2           1    1         0           0  ...       0     0       0
3           1    1         0           1  ...       0     0       0
4           0    1         0           0  ...       0     0       0

This Python example tokenizes paragraphs about algorithmic bias, lowercases their tokens, and builds a DataFrame of document counts. Each column is a vocabulary entry. section 3.2

A bag-of-words vector gives each vocabulary entry a position holding a count or presence indicator. Its length is the vocabulary size, often written |V|. The order can be arbitrary, but it must be consistent across documents so that corresponding positions represent the same tokens. section 2.5.2 section 3.2.3

Vocabulary choices determine what a pipeline can distinguish. An out-of-vocabulary token can collect excluded words, but their individual identities are lost. Keeping related forms such as “swim” and “swimming” separate can also make it harder to find all articles about swimming; treating tokens independently loses information about their sequences. section 3.1 section 2.3

How Natural Language Processing in Action, Second Edition covers it

How deep it goes
Explains token choices, corpus-wide vocabulary construction, vector dimensions, and information lost through exclusion or separate word forms.
Examples
Builds token counts for paragraphs about algorithmic bias and inspects a corpus vocabulary of 246 entries.
What you can do after
Apply vocabulary construction using spaCy tokenization, Python Counter, and pandas document-count vectors.
How it connects
Connects vocabulary to tokenization, subword tokenization, bag-of-words vectors, and out-of-vocabulary tokens.

Also explained in

Compare how 13 books explain vocabulary →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Natural Language Processing in Action, Second Edition. The section links point to the parts of the book it draws on.