Friday, July 26, 2024

 Tweet sentiment analyzer:

import sys


def hw():

    afinnfile = open("AFINN-111.txt")

    scores = {} # initialize an empty dictionary

    for line in afinnfile:

      term, score  = line.split("\t")  # The file is tab-delimited. "\t" means "tab character"

      scores[term] = int(score)  # Convert the score to an integer.

    print scores.items()


    import json

    outputfile = open("output.txt")

    tweets = []

    for line in outputfile:

      tweets += [json.loads(line)]


    for item in tweets:

        if item.text:

           sentence = trim(item.text)

           words = sentence.split()

           score = 0

           for word in words:

               term = tolower(trim(word))

               if term in scores:

                   if scores[term] > 0:

                      score += 1

                   else if scores[term] < 0:

                      score -= 1

                   else:

                      score += 0

           if len(words) > 0:

              score = score/len(words)

           print(score)

        else:

           print(0)


def lines(fp):

    print str(len(fp.readlines()))


def main():

    sent_file = open(sys.argv[1])

    tweet_file = open(sys.argv[2])

    hw()

    lines(sent_file)

    lines(tweet_file)


if __name__ == '__main__':

    main()


No comments:

Post a Comment