I want to write a function that returns the frequency of each element in the n-gram of a given text. Help please. I did this code fo counting frequency of 2-gram
code:
from nltk import FreqDist
from nltk.util import ngrams
def compute_freq():
textfile = "please write a function"
bigramfdist = FreqDist()
threeramfdist = FreqDist()
for line in textfile:
if len(line) > 1:
tokens = line.strip().split(' ')
bigrams = ngrams(tokens, 2)
bigramfdist.update(bigrams)
return bigramfdist
bigramfdist = compute_freq()