# Bigram frequency without word order in python

When working with text data, it is often useful to analyze the frequency of bigrams, which are pairs of consecutive words. However, in some cases, we may want to ignore the order of the words in the bigrams and treat them as unordered pairs. In this article, we will explore three different ways to calculate the frequency of unordered bigrams in Python.

## Option 1: Using a Counter

One simple way to calculate the frequency of unordered bigrams is to use the Counter class from the collections module. The Counter class is a specialized dictionary that allows us to count the occurrences of elements in a list or any other iterable.

``````from collections import Counter

def calculate_bigram_frequency(text):
words = text.split()
bigrams = [(words[i], words[i+1]) for i in range(len(words)-1)]
unordered_bigrams = [tuple(sorted(bigram)) for bigram in bigrams]
frequency = Counter(unordered_bigrams)
return frequency

text = "I love to code in Python"
frequency = calculate_bigram_frequency(text)
print(frequency)``````

In this code, we first split the input text into a list of words. Then, we generate a list of all bigrams by iterating over the words and creating pairs of consecutive words. Next, we convert each bigram into an unordered pair by sorting the words alphabetically. Finally, we use the Counter class to count the occurrences of each unordered bigram.

## Option 2: Using a DefaultDict

Another approach to calculate the frequency of unordered bigrams is to use a defaultdict from the collections module. A defaultdict is a dictionary subclass that provides a default value for missing keys.

``````from collections import defaultdict

def calculate_bigram_frequency(text):
words = text.split()
bigrams = [(words[i], words[i+1]) for i in range(len(words)-1)]
unordered_bigrams = [tuple(sorted(bigram)) for bigram in bigrams]
frequency = defaultdict(int)
for bigram in unordered_bigrams:
frequency[bigram] += 1
return frequency

text = "I love to code in Python"
frequency = calculate_bigram_frequency(text)
print(frequency)``````

In this code, we follow a similar approach as in Option 1, but instead of using the Counter class, we use a defaultdict with a default value of 0. We then iterate over the unordered bigrams and increment their frequency in the defaultdict.

## Option 3: Using a Dictionary

A third option to calculate the frequency of unordered bigrams is to use a regular dictionary. This approach is similar to Option 2, but instead of using a defaultdict, we manually handle the case when a bigram is not yet present in the dictionary.

``````def calculate_bigram_frequency(text):
words = text.split()
bigrams = [(words[i], words[i+1]) for i in range(len(words)-1)]
unordered_bigrams = [tuple(sorted(bigram)) for bigram in bigrams]
frequency = {}
for bigram in unordered_bigrams:
if bigram not in frequency:
frequency[bigram] = 0
frequency[bigram] += 1
return frequency

text = "I love to code in Python"
frequency = calculate_bigram_frequency(text)
print(frequency)``````

In this code, we create an empty dictionary to store the frequencies of the unordered bigrams. We then iterate over the unordered bigrams and manually handle the case when a bigram is not yet present in the dictionary.

After exploring these three options, it is clear that Option 1, which uses the Counter class, is the most concise and efficient solution. The Counter class provides a simple way to count the occurrences of elements, making it the best choice for calculating the frequency of unordered bigrams in Python.

Rate this post