ahmedraza

analyzer.py

Jan 19th, 2017
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.51 KB | None | 0 0
  1. import nltk
  2. import re
  3. class Analyzer():
  4.     """Implements sentiment analysis."""
  5.  
  6.     def __init__(self, positives, negatives):
  7.         """Initialize Analyzer."""
  8.         #loading positive.txt as dict
  9.         self.positives = []
  10.         #opening file for reading as fpos
  11.         with open('positive-words.txt', 'r') as fpos:
  12.             #iterate over every line
  13.                 for line in fpos:
  14.                     #if line don,t start with ";" load it
  15.                     if line.startswith(";") != True:
  16.                         self.positives.append(eval(line.strip()))
  17.         #loading negative.txt as dict
  18.         self.negatives = []
  19.         #opening file for reading as fneg
  20.         with open('negative-words.txt', 'r') as fneg:
  21.             #iterate over every line
  22.             for line in fneg:
  23.                 #if line don,t start with ";" load it
  24.                 if (line.startswith(";") != True):
  25.                     self.positives.append(eval(line.strip()))
  26.  
  27.     def analyze(self, text):
  28.         #defining tokenizer
  29.         tokenizer = nltk.tokenize.TweetTokenizer()
  30.         #tokenizing text/tweet
  31.         tokens = tokenizer.tokenize(text)
  32.         score = 0
  33.         #iterating over all token and checking if they are in positive or negative        
  34.         for token in tokens:
  35.             if token in self.positives:
  36.                 score += 1
  37.             elif token in self.negatives:
  38.                 score -= 1
  39.             else:
  40.                 score = score
  41.            
  42.         return score
Add Comment
Please, Sign In to add comment