import nltk
from nltk.sentiment import SentimentIntensityAnalyzer
import textwrap

inputFileName = 'input.txt'

# read input file
with open(inputFileName) as f:
    lines = f.readlines()

# convert from a string list to string
input = ""
for i in lines: 
        input += i 

# split string into tokens/words, deletes punctuations
wordTokens = [w for w in nltk.word_tokenize(input) if w.isalpha()] 

# split string into sentences
sentences = [w for w in nltk.sent_tokenize(input)]

# split string into tokens, deletes punctuations and filters "stopwords"
# stopwords are common words like "of", "a", and "the" that hinder sentiment score analysis
stopwords = nltk.corpus.stopwords.words("english")
wordTokens = [w for w in wordTokens if w.lower() not in stopwords]

# create new string with simplified tokens
updatedInput = " "
for t in wordTokens: 
        updatedInput += t
        updatedInput += " "

sia = SentimentIntensityAnalyzer()
finalScore = sia.polarity_scores(updatedInput)
print("\nSentence                    neg     neu     pos     comp")

negScore = 0
neuScore = 0
posScore = 0

# Analyze each sentence's sentiment score 
for i in sentences:
    tokens = [w for w in nltk.word_tokenize(i) if w.isalpha() and w.lower() not in stopwords]
    updatedSentence = ""
    updatedInput = ""
    k = 0
    for j in tokens:
        k+=1
        updatedSentence += j
        updatedSentence += " "
        if k == 2:
            updatedSentence += ".."
            break
    
    score = sia.polarity_scores(updatedSentence)

    #checking whenther it's a neg, neu, or pos sentence
    neg = score['neg'] 
    neu = score['neu']
    pos = score['pos']
    comp = score['compound']
   
    if(neg >= neu and neg >= pos): negScore = negScore + 1
    elif(neu >= neg and neu >= pos): neuScore = neuScore + 1
    else: posScore = posScore + 1

    string = "{sent:<27} {neg:<7} {neu:<7} {pos:<7} {comp:<10}".format(sent = "\"" + updatedSentence + "\"" , neg = neg, 
    neu = neu, pos = pos, comp = comp)
    
    
    print(string)

print("\n{neg} negative sentences\n{neu} neutral sentences\n{pos} positive sentences\n".format(neg = negScore, neu = neuScore, pos = posScore))

print("Before simplifying: ")
print(sia.polarity_scores(input))
print("\nAfter simplifying: ")
print(finalScore)