In: Computer Science
Hello,
Please find the below code to find the NGrams.
I have used the nltk library.
import nltk from nltk import ngrams from textblob import TextBlob file=open("veg.txt","r") rd=file.read() #sentiment sentences= nltk.sent_tokenize(rd) tb=(TextBlob (t).sentiment.polarity for t in rd) tb=TextBlob(rd) print(tb.sentiment.polarity) #ner words=[] for sentence in sentences: words.append(nltk.word_tokenize(sentence)) tags=[] for word in words: tags.append(nltk.pos_tag(word)) for tag in tags: print(nltk.ne_chunk(tag)) #pos print(tb.tags) wordlist = rd.split() wordfreq = [] for w in wordlist: wordfreq.append(wordlist.count(w)) print("Word Frequency\n" + str(list(zip(wordlist, wordfreq)))) #NGrams n = 6 # Pass any integer value to find the Ngrams of a sentence sixgrams = ngrams(rd.split(), n) for grams in sixgrams: print("GRAMS : {}".format(grams))
Result:
Let me know if you have any doubts in the comments sections.
Thanks.