How to create a dictionary using Gensim

Gensim is a powerful Python module widely used for natural language processing (NLP) tasks. One of its important features is the ability to generate dictionaries, which are essential in text analysis and modeling.

What is a Gensim dictionary?

A Gensim dictionary is a fundamental data structure that converts words into unique integer IDs. It acts as a lookup database for word-to-ID and ID-to-word conversions, allowing for fast textual data processing for different NLP applications.

Note: Make sure you have Gensim installed on your system. If not, you can install it using pip install gensim command on your terminal.

Let's thoroughly understand the creation of a dictionary in a step-by-step approach.

Creating a dictionary with Gensim

  1. To build a dictionary using this module, we start by importing the necessary libraries, gensim, corpora, which provides functionality for working with corporaCollections of text documents. and dictionaries. pprint is a Python-based module and is imported for pretty printing.

import gensim
from gensim import corpora
from pprint import pprint
  1. Here, we define a list called doc that contains sample documents. The documents are strings representing a text and will be used to create the dictionary.

doc = [
"Machine learning is a subfield of artificial intelligence that focuses on the development of algorithms",
"and models that allow computers to learn and make predictions based on data."
]

Now, we preprocess the text data before creating the Gensim dictionary. They are processes like tokenization, removing stop words, and converting the text into a suitable format for analysis.

  1. We tokenize the documents by splitting each document into individual words. The resulting tokens are stored in the text_tokens list.

text_tokens = [[text for text in doc.split()] for doc in doc]
  1. Finally, we create a Gensim dictionary using the Dictionary() function from the corpora module. The Dictionary() function takes the text_tokens list as input and builds a dictionary that maps words to unique integer IDs.

dict_LoS = corpora.Dictionary(text_tokens)
  1. We print the contents of the created dictionary at last.

Note: token2id It is an attribute of the Gensim dictionary object that maps words to their corresponding integer IDs.

print(dict_LoS.token2id)

By printing dict_LoS.token2id, we can see the dictionary in the format of {word: id} pairs.

The complete implementation of the Gensim dictionary can be seen below:

Code

import gensim
from gensim import corpora
from pprint import pprint
# Sample documents
doc = [
"Machine learning is a subfield of artificial intelligence that focuses on the development of algorithms",
"and models that allow computers to learn and make predictions based on data."
]
# Tokenize the documents
text_tokens = [[text for text in doc.split()] for doc in doc]
# Create a dictionary from the tokenized documents
dict_LoS = corpora.Dictionary(text_tokens)
# Print the dictionary
print(dict_LoS.token2id)

By executing this code, you will have a Gensim dictionary (dict_LoS) that maps each word to a unique integer ID based on the provided sample documents.

Output

Upon execution, the code will tokenize the sample documents, create a Gensim dictionary based on the tokenized words, and then print the dictionary showing the terms and their respective integer IDs.

The output looks something like this:

Conclusion

In conclusion, by creating dictionaries using Gensim, developers can effectively convert text data into a structured format that facilitates efficient analysis and modeling. The Gensim dictionary allows for mapping words to unique integer IDs, facilitating text transformation into numerical representations that machine learning algorithms can process. 

Free Resources

Copyright ©2026 Educative, Inc. All rights reserved