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












