def demo_sent_subjectivity(text):
 from nltk.classify import NaiveBayesClassifier
 from nltk.tokenize import regexp
 word_tokenizer = regexp.WhitespaceTokenizer()
 try:
  sentim_analyzer = load('sa_subjectivity.pickle')
 except LookupError:
  print 'Cannot find the sentiment analyzer you want to load.'
  print 'Training a new one using NaiveBayesClassifier.'
  sentim_analyzer = demo_subjectivity(NaiveBayesClassifier.train, True)
 tokenized_text = [word.lower() for word in word_tokenizer.tokenize(text)]
 print sentim_analyzer.classify(tokenized_text)

def demo_sent_subjectivity ( text ) : from nltk . classify import NaiveBayesClassifier from nltk . tokenize import regexp word_tokenizer = regexp . WhitespaceTokenizer ( ) try : sentim_analyzer = load ( 'sa_subjectivity.pickle' ) except LookupError : print 'Cannot find the sentiment analyzer you want to load.' print 'Training a new one using NaiveBayesClassifier.' sentim_analyzer = demo_subjectivity ( NaiveBayesClassifier . train , True ) tokenized_text = [ word . lower ( ) for word in word_tokenizer . tokenize ( text ) ] print sentim_analyzer . classify ( tokenized_text )
classify a single sentence as subjective or objective using a stored sentimentanalyzer .