Bijoy
Bijoy

Reputation: 1131

Adding Emoticons to AFINN library for Sentiment analysis

How to add Emoticons to AFINN library

I want to add Emoticons to AFINN library for Sentiment Analysis , The Library already have have Words with their respective polarity , How to append some Emoticons so that the respective code can read its Polarity ???

afinn = dict(map(lambda (w, s): (w, int(s)), [ 
        ws.strip().split('\t') for ws in open(filenameAFINN) ]))
pattern_split = re.compile(r"\W+")
def sentiment(text):
    words = pattern_split.split(text.lower())
    sentiments = map(lambda word: afinn.get(word, 0), words)
    if sentiments:
        sentiment = float(sum(sentiments))/math.sqrt(len(sentiments))
    else:
        sentiment = 0
    return sentiment
if __name__ == '__main__':
    print("%s") % (text)
    print ("%6.2f") % (sentiment(text))
    if sentiment(text) < 0:
        print "================||| NEGATIVE |||================"
    elif sentiment(text) > 0:
        print "================||| POSITIVE |||================"
    else:
        print "================||| Seems NEUTRAL |||================"  

The library has words in order, Like.

yucky   -2
yummy   3
zealot  -2
zealots -2
zealous 2

how should i add these Emoticons in the library , and read its polarity

(^ ^)   1
(^-^)   1
(^.^)   1

Upvotes: 1

Views: 1451

Answers (1)

Finn &#197;rup Nielsen
Finn &#197;rup Nielsen

Reputation: 6726

I am the one behind the AFINN word list. My Python package named afinn already features some emoticons.

>>> afinn = Afinn(emoticons=True)
>>> afinn.score('I saw that yesterday :)')
2.0

You can get the afinn Python package here:

https://github.com/fnielsen/afinn

or from the Python package Index

https://pypi.python.org/pypi/afinn/

There is a file with my scoring of emoticons. On GitHub you find it here:

https://github.com/fnielsen/afinn/blob/master/afinn/data/AFINN-emoticon-8.txt

If you want to add your own emoticons, I suppose the presently less troublesome approach would be to extend the emoticon file after you have copied/forked a version of afinn.

Upvotes: 1

Related Questions