UNIGRAM
from collections import Counter as c
Run to view results
sentance = "Apple Cherry Lemon Lemon Hero"
corpus = list(sentance.split())
print(corpus)
Run to view results
unigram_count = c(corpus)
print(unigram_count['Lemon'])
Run to view results
BIGRAM
from nltk import bigrams as b
from nltk.probability import FreqDist
Run to view results
sentance = "I love icecream bit I love big dance too"
bigram_tokens = list(b(sentance.split()))
# print(bigram_tokens)
freqdist = FreqDist(bigram_tokens)
print(freqdist[('I' ,'love')])
Run to view results
print(bigram_tokens)
Run to view results
Trigram
from nltk import trigrams as t
from nltk.probability import FreqDist
Run to view results
sentance = "I love icecream bit I love icecream sandwich too"
trigram_tokens = list(t(sentance.split()))
# print(trigram_tokens)
freqdist = FreqDist(trigram_tokens)
print(freqdist[('I' ,'love' , 'icecream')])
Run to view results
print(trigram_tokens)
Run to view results
N- gram
from nltk import ngrams as ng
from nltk.probability import FreqDist as fd
Run to view results
sent = input("")
n = len(list(sent.split()))
ngram_tokens = list(ng(sent.split(),n))
Fred = fd(ngram_tokens)
print(Fred[("I","Love","icecream")])
Run to view results