So I have some reviews that I am trying to classify into positive or negative. I am trying to use NLTK and Stanford coreNLP to do so. I am able to do it on unigrams but its not working for bigrams. I tried the following for bigrams
def classifySentence(sen):
wn_lem = WordNetLemmatizer()
pos = 0
neg = 0
stop_words = set(stopwords.words('english'))
filtered_review = [token for token in nltk.word_tokenize(sen) if not token in stop_words]
for token in nltk.bigrams(filtered_review):
#lemma = wn_lem.lemmatize(token)
# print("lemma="+token)
if len(wn.synsets(token))>0:
synset = wn.synsets(token)[0]
#print("synset.name="+synset.name())
sent = swn.senti_synset(synset.name())
#print("Sentiment of "+token+" "+str(sent))
pos = pos + sent.pos_score()
neg = neg + sent.neg_score()
# print (token + "(pos_score): " + str(pos) +"\n")
# print (token + "(neg_score): " + str(neg) +"\n")
#print (filtered_review)
JoinedTokens = ' '.join(wo for wo in filtered_review)
return [JoinedTokens, pos, neg]
I was wondering if someone could suggest me ways to do this. I would like to use NLTK or can also use stanfordcoreNLP. I am also open to using other python packages but just need some guidance I have written some code for using it but it didn't work either. The code I wrote
def StanfordBigrams():
nlp = StanfordCoreNLP('http://localhost:9000')
operations = {'annotators': 'tokenize,lemma,pos,sentiment', 'outputFormat': 'json'}
string = "not bad"
tok = nltk.word_tokenize(string)
bigrams = nltk.bigrams(tok)
res = nlp.annotate(str(bigrams),operations)
for s in res["sentences"]:
for token in s["tokens"]:
print("Sentiment: "+str(s["sentiment"])+"SentimentValue: "+str(s["sentimentValue"]))
print (token)
I would be grateful if someone could point me in the right direction.