Question

In: Computer Science

how to add n gram code for this program? import nltk from textblob import TextBlob from...

how to add n gram code for this program?

import nltk
from textblob import TextBlob
from collections import Counter

from nltk import FreqDist
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))))

Solutions

Expert Solution

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.


Related Solutions

Please convert this code written in Python to Java: import string import random #function to add...
Please convert this code written in Python to Java: import string import random #function to add letters def add_letters(number,phrase):    #variable to store encoded word    encode = ""       #for each letter in phrase    for s in phrase:        #adding each letter to encode        encode = encode + s        for i in range(number):            #adding specified number of random letters adding to encode            encode = encode +...
Please add comments to this code! JAVA Code: import java.text.NumberFormat; public class Item {    private...
Please add comments to this code! JAVA Code: import java.text.NumberFormat; public class Item {    private String name;    private double price;    private int bulkQuantity;    private double bulkPrice;    /***    *    * @param name    * @param price    * @param bulkQuantity    * @param bulkPrice    */    public Item(String name, double price, int bulkQuantity, double bulkPrice) {        this.name = name;        this.price = price;        this.bulkQuantity = bulkQuantity;        this.bulkPrice = bulkPrice;   ...
Please add to this Python, Guess My Number Program. Add code to the program to make...
Please add to this Python, Guess My Number Program. Add code to the program to make it ask the user for his/her name and then greet that user by their name. Please add code comments throughout the rest of the program If possible or where you add in the code to make it ask the name and greet them before the program begins. import random def menu(): print("\n\n1. You guess the number\n2. You type a number and see if the...
---In the code, create add and delete a student by ID number when prompted /////////////////////////////////////////////////////////////// import...
---In the code, create add and delete a student by ID number when prompted /////////////////////////////////////////////////////////////// import java.util.Scanner; public class COurseCom666 {     private String courseName;     private String [] students = new String[1];     private int numberOfStudents;     public COurseCom66(String courseName) {         this.courseName = courseName;     }     public String[] getStudents() {         return students;     }     public int getNumberOfStudents() {         return numberOfStudents;     }     public String getCourseName() {         return courseName;     }     public...
Please add comments to this code! JAVA code: import java.util.ArrayList; public class ShoppingCart { private final...
Please add comments to this code! JAVA code: import java.util.ArrayList; public class ShoppingCart { private final ArrayList<ItemOrder> itemOrder;    private double total = 0;    private double discount = 0;    ShoppingCart() {        itemOrder = new ArrayList<>();        total = 0;    }    public void setDiscount(boolean selected) {        if (selected) {            discount = total * .1;        }    }    public double getTotal() {        total = 0;        itemOrder.forEach((order) -> {            total +=...
Can you please add comments to this code? JAVA Code: import java.util.ArrayList; public class Catalog {...
Can you please add comments to this code? JAVA Code: import java.util.ArrayList; public class Catalog { String catalog_name; ArrayList<Item> list; Catalog(String cs_Gift_Catalog) { list=new ArrayList<>(); catalog_name=cs_Gift_Catalog; } String getName() { int size() { return list.size(); } Item get(int i) { return list.get(i); } void add(Item item) { list.add(item); } } Thanks!
Convert this pseudo code program into sentences. import java.util.ArrayList; import java.util.Collections; class Bulgarian {    public...
Convert this pseudo code program into sentences. import java.util.ArrayList; import java.util.Collections; class Bulgarian {    public static void main(String[] args)    {        max_cards=45;        arr->new ArraryList        col=1;        card=0;        left=max_cards;        do{            col->random number            row->new ArrayList;            for i=0 to i<col            {                card++                add card into row            }   ...
Overview For this program, add code to program 2 that will (potentially) allow for a discount...
Overview For this program, add code to program 2 that will (potentially) allow for a discount to be applied to the cost of the tickets to the movie theater. Basic Program Logic The program should start the same as program 2 by asking the user for the number of tickets to purchase for adult and children. The program should then ask the user if they have a discount coupon. This is a string value. A value of "Y" indicates the...
(Python) How would I add this input into my code? "Enter Y for Yes or N...
(Python) How would I add this input into my code? "Enter Y for Yes or N for No:" as a loop. def getMat(): mat = np.zeros((3, 3)) print("Enter your first 3 by 3 matrix:") for row in range(3): li = [int(x) for x in input().split()] mat[row,0] = li[0] mat[row,1] = li[1] mat[row,2] = li[2] print("Your first 3 by 3 matrix is :") for i in range(3): for k in range(3): print(str(int(mat[i][k]))+" ",end="") print() return mat def checkEntry(inputValue): try: float(inputValue) except...
JAVA ONLY - Complete the code import java.util.Scanner; /** * This program will use the HouseListing...
JAVA ONLY - Complete the code import java.util.Scanner; /** * This program will use the HouseListing class and display a list of * houses sorted by the house's listing number * * Complete the code below the numbered comments, 1 - 4. DO NOT CHANGE the * pre-written code * @author * */ public class HouseListingDemo { public static void main(String[] args) { Scanner scan = new Scanner(System.in); HouseListing[] list; String listNumber, listDesc; int count = 0; double listPrice; String...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT