[파이썬] Gensim Dictionary Filtering

Gensim is a popular Python library for topic modeling and document similarity analysis. It provides efficient tools for working with text data and extracting meaningful insights. One of the key functionalities of Gensim is dictionary filtering, which allows us to remove irrelevant words or tokens from a text corpus. In this blog post, we will explore how to perform dictionary filtering using Gensim in Python.

What is a Gensim Dictionary?

Before we dive into dictionary filtering, let’s first understand what a Gensim dictionary is. In Gensim, a dictionary represents a collection of words or tokens and their unique integer IDs. It acts as a mapping between words and their corresponding IDs. We can create a Gensim dictionary from a text corpus, and then use it for various text analysis tasks.

Filtering the Dictionary

Once we have created a Gensim dictionary, we may want to filter out certain words that are not relevant to our analysis. Gensim provides a convenient method called filter_extremes to achieve this. This method allows us to filter out words based on their frequencies or document counts.

Here’s an example of how to filter a Gensim dictionary by frequency:

from gensim.corpora import Dictionary

# Create a Gensim dictionary from a text corpus
corpus = [['apple', 'banana', 'orange', 'apple'],
          ['car', 'bus', 'car', 'bike'],
          ['apple', 'car', 'car'],
          ['dog', 'cat', 'dog', 'cat']]
dictionary = Dictionary(corpus)

# Filter out words that appear less than 2 times
dictionary.filter_extremes(no_below=2, no_above=1.0)

# Print the filtered dictionary
print(dictionary)

In the above example, we create a Gensim dictionary from a text corpus containing four documents. We then apply the filter_extremes method to remove words that appear less than 2 times (no_below=2) and words that appear in more than 100% of the documents (no_above=1.0). Finally, we print the filtered dictionary.

Conclusion

In this blog post, we have explored how to perform dictionary filtering using Gensim in Python. Gensim provides a powerful and efficient way to filter out irrelevant words from a text corpus, allowing us to focus on the most important words for our analysis. Being able to filter the dictionary is an important step in preparing the text data for further analysis, such as topic modeling or document similarity computation.