Skip to content

Commit

Permalink
Now System is working based on customized dic and also SentiWordNet
Browse files Browse the repository at this point in the history
  • Loading branch information
JoyeBright committed Sep 1, 2018
1 parent 34ad3ff commit cc82703
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 15 deletions.
11 changes: 6 additions & 5 deletions InputTxt.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
text = """What can I say about this place. The staff of the restaurant is
nice and the eggplant is not bad. Apart from that, very uninspired food,
lack of atmosphere and too expensive. I am a staunch vegetarian and was
sorely disappointed with the veggie options on the menu. Will be the last
time I visit, I recommend others to avoid."""
text = "This toy does not look like the picture at all." \
" The bread, tomato, and lettuce were all hard. " \
"The only parts that are a bit realistic are the cheese and meats." \
" This toy came with only one tomato and one lettuce." \
" That's not enough objects to make multiple full sandwiches." \
" I would recommend not buying this toy if you want your money's worth."
13 changes: 3 additions & 10 deletions SA.py → SACustomizedDic.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import nltk
import InputTxt
import yaml
from nltk.corpus import sentiwordnet as swn



class PreProcess(object):
Expand Down Expand Up @@ -119,14 +119,7 @@ def sa_score(self, dictionary_tagged_sentence):
print(POS)
print("Adjust with the Text Structure ......")
print(Adjust)
Option = input("Select the mode of tagging tokens:\n 1.Customized Dictionaries\n 2.SentiWordNet3.0\n 3.SentiPers\n")
if Option == "1":
print("Tagging the tokens with Dictionaries ......")
print(dict_tagged_sentences)
if Option == "2":
print("Tagging the tokens with SentiWordNet")
if Option == "3":
print("Tagging the tokens with Sentipers")

print("Tagging the tokens with Dictionaries ......")
print(dict_tagged_sentences)
print("..... Sentiment Analysing .....")
print("AVG:", Score)
51 changes: 51 additions & 0 deletions SASentiWordNet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import nltk
from nltk.corpus import sentiwordnet as swn
import InputTxt

sentences = nltk.sent_tokenize(InputTxt.text)
print(sentences)
sentences_tokens = [nltk.word_tokenize(s) for s in sentences]
print(sentences_tokens)
tagged_list = []
for i in sentences_tokens:
tagged_list.append(nltk.pos_tag(i))
print(tagged_list)
wordNetLemmatizer = nltk.WordNetLemmatizer()

score_list = []
for i, j in enumerate(tagged_list):
score_list.append([])
for m, n in enumerate(j):
new_tag = ''
lemmatized = wordNetLemmatizer.lemmatize(n[0])
if n[1].startswith('NN'):
new_tag = 'n'
elif n[1].startswith('JJ'):
new_tag = 'a'
elif n[1].startswith('V'):
new_tag = 'v'
elif n[1].startswith('R'):
new_tag = 'r'
else:
new_tag = ''
if new_tag != '':
synsets = list(swn.senti_synsets(lemmatized, new_tag))
score = 0
if len(synsets) > 0:
for s in synsets:
score += s.pos_score() - s.neg_score()
score_list[i].append(score/len(synsets))

print(score_list)
sentences_sentiment = []

for s in score_list:
sentences_sentiment.append(sum([w for w in s])/len(s))
print("Final Sentiment Analysis for each sentences in Input text: .........")
print(sentences_sentiment)






0 comments on commit cc82703

Please sign in to comment.