def demo_liu_hu_lexicon(sentence, plot=False):
 from nltk.corpus import opinion_lexicon
 from nltk.tokenize import treebank
 tokenizer = treebank.TreebankWordTokenizer()
 pos_words = 0
 neg_words = 0
 tokenized_sent = [word.lower() for word in tokenizer.tokenize(sentence)]
 x = list(range(len(tokenized_sent)))
 y = []
 for word in tokenized_sent:
  if (word in opinion_lexicon.positive()):
   pos_words += 1
   y.append(1)
  elif (word in opinion_lexicon.negative()):
   neg_words += 1
   y.append((-1))
  else:
   y.append(0)
 if (pos_words > neg_words):
  print 'Positive'
 elif (pos_words < neg_words):
  print 'Negative'
 elif (pos_words == neg_words):
  print 'Neutral'
 if (plot == True):
  _show_plot(x, y, x_labels=tokenized_sent, y_labels=['Negative', 'Neutral', 'Positive'])

def demo_liu_hu_lexicon ( sentence , plot = False ) : from nltk . corpus import opinion_lexicon from nltk . tokenize import treebank tokenizer = treebank . TreebankWordTokenizer ( ) pos_words = 0 neg_words = 0 tokenized_sent = [ word . lower ( ) for word in tokenizer . tokenize ( sentence ) ] x = list ( range ( len ( tokenized_sent ) ) ) y = [ ] for word in tokenized_sent : if ( word in opinion_lexicon . positive ( ) ) : pos_words += 1 y . append ( 1 ) elif ( word in opinion_lexicon . negative ( ) ) : neg_words += 1 y . append ( ( - 1 ) ) else : y . append ( 0 ) if ( pos_words > neg_words ) : print 'Positive' elif ( pos_words < neg_words ) : print 'Negative' elif ( pos_words == neg_words ) : print 'Neutral' if ( plot == True ) : _show_plot ( x , y , x_labels = tokenized_sent , y_labels = [ 'Negative' , 'Neutral' , 'Positive' ] )
basic example of sentiment classification using liu and hu opinion lexicon .