sentiment.doctest 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. .. Copyright (C) 2001-2019 NLTK Project
  2. .. For license information, see LICENSE.TXT
  3. ===================
  4. Sentiment Analysis
  5. ===================
  6. >>> from nltk.classify import NaiveBayesClassifier
  7. >>> from nltk.corpus import subjectivity
  8. >>> from nltk.sentiment import SentimentAnalyzer
  9. >>> from nltk.sentiment.util import *
  10. >>> n_instances = 100
  11. >>> subj_docs = [(sent, 'subj') for sent in subjectivity.sents(categories='subj')[:n_instances]]
  12. >>> obj_docs = [(sent, 'obj') for sent in subjectivity.sents(categories='obj')[:n_instances]]
  13. >>> len(subj_docs), len(obj_docs)
  14. (100, 100)
  15. Each document is represented by a tuple (sentence, label). The sentence is tokenized,
  16. so it is represented by a list of strings:
  17. >>> subj_docs[0]
  18. (['smart', 'and', 'alert', ',', 'thirteen', 'conversations', 'about', 'one',
  19. 'thing', 'is', 'a', 'small', 'gem', '.'], 'subj')
  20. We separately split subjective and objective instances to keep a balanced uniform
  21. class distribution in both train and test sets.
  22. >>> train_subj_docs = subj_docs[:80]
  23. >>> test_subj_docs = subj_docs[80:100]
  24. >>> train_obj_docs = obj_docs[:80]
  25. >>> test_obj_docs = obj_docs[80:100]
  26. >>> training_docs = train_subj_docs+train_obj_docs
  27. >>> testing_docs = test_subj_docs+test_obj_docs
  28. >>> sentim_analyzer = SentimentAnalyzer()
  29. >>> all_words_neg = sentim_analyzer.all_words([mark_negation(doc) for doc in training_docs])
  30. We use simple unigram word features, handling negation:
  31. >>> unigram_feats = sentim_analyzer.unigram_word_feats(all_words_neg, min_freq=4)
  32. >>> len(unigram_feats)
  33. 83
  34. >>> sentim_analyzer.add_feat_extractor(extract_unigram_feats, unigrams=unigram_feats)
  35. We apply features to obtain a feature-value representation of our datasets:
  36. >>> training_set = sentim_analyzer.apply_features(training_docs)
  37. >>> test_set = sentim_analyzer.apply_features(testing_docs)
  38. We can now train our classifier on the training set, and subsequently output the
  39. evaluation results:
  40. >>> trainer = NaiveBayesClassifier.train
  41. >>> classifier = sentim_analyzer.train(trainer, training_set)
  42. Training classifier
  43. >>> for key,value in sorted(sentim_analyzer.evaluate(test_set).items()):
  44. ... print('{0}: {1}'.format(key, value))
  45. Evaluating NaiveBayesClassifier results...
  46. Accuracy: 0.8
  47. F-measure [obj]: 0.8
  48. F-measure [subj]: 0.8
  49. Precision [obj]: 0.8
  50. Precision [subj]: 0.8
  51. Recall [obj]: 0.8
  52. Recall [subj]: 0.8
  53. Vader
  54. ------
  55. >>> from nltk.sentiment.vader import SentimentIntensityAnalyzer
  56. >>> sentences = ["VADER is smart, handsome, and funny.", # positive sentence example
  57. ... "VADER is smart, handsome, and funny!", # punctuation emphasis handled correctly (sentiment intensity adjusted)
  58. ... "VADER is very smart, handsome, and funny.", # booster words handled correctly (sentiment intensity adjusted)
  59. ... "VADER is VERY SMART, handsome, and FUNNY.", # emphasis for ALLCAPS handled
  60. ... "VADER is VERY SMART, handsome, and FUNNY!!!",# combination of signals - VADER appropriately adjusts intensity
  61. ... "VADER is VERY SMART, really handsome, and INCREDIBLY FUNNY!!!",# booster words & punctuation make this close to ceiling for score
  62. ... "The book was good.", # positive sentence
  63. ... "The book was kind of good.", # qualified positive sentence is handled correctly (intensity adjusted)
  64. ... "The plot was good, but the characters are uncompelling and the dialog is not great.", # mixed negation sentence
  65. ... "A really bad, horrible book.", # negative sentence with booster words
  66. ... "At least it isn't a horrible book.", # negated negative sentence with contraction
  67. ... ":) and :D", # emoticons handled
  68. ... "", # an empty string is correctly handled
  69. ... "Today sux", # negative slang handled
  70. ... "Today sux!", # negative slang with punctuation emphasis handled
  71. ... "Today SUX!", # negative slang with capitalization emphasis
  72. ... "Today kinda sux! But I'll get by, lol" # mixed sentiment example with slang and constrastive conjunction "but"
  73. ... ]
  74. >>> paragraph = "It was one of the worst movies I've seen, despite good reviews. \
  75. ... Unbelievably bad acting!! Poor direction. VERY poor production. \
  76. ... The movie was bad. Very bad movie. VERY bad movie. VERY BAD movie. VERY BAD movie!"
  77. >>> from nltk import tokenize
  78. >>> lines_list = tokenize.sent_tokenize(paragraph)
  79. >>> sentences.extend(lines_list)
  80. >>> tricky_sentences = [
  81. ... "Most automated sentiment analysis tools are shit.",
  82. ... "VADER sentiment analysis is the shit.",
  83. ... "Sentiment analysis has never been good.",
  84. ... "Sentiment analysis with VADER has never been this good.",
  85. ... "Warren Beatty has never been so entertaining.",
  86. ... "I won't say that the movie is astounding and I wouldn't claim that \
  87. ... the movie is too banal either.",
  88. ... "I like to hate Michael Bay films, but I couldn't fault this one",
  89. ... "It's one thing to watch an Uwe Boll film, but another thing entirely \
  90. ... to pay for it",
  91. ... "The movie was too good",
  92. ... "This movie was actually neither that funny, nor super witty.",
  93. ... "This movie doesn't care about cleverness, wit or any other kind of \
  94. ... intelligent humor.",
  95. ... "Those who find ugly meanings in beautiful things are corrupt without \
  96. ... being charming.",
  97. ... "There are slow and repetitive parts, BUT it has just enough spice to \
  98. ... keep it interesting.",
  99. ... "The script is not fantastic, but the acting is decent and the cinematography \
  100. ... is EXCELLENT!",
  101. ... "Roger Dodger is one of the most compelling variations on this theme.",
  102. ... "Roger Dodger is one of the least compelling variations on this theme.",
  103. ... "Roger Dodger is at least compelling as a variation on the theme.",
  104. ... "they fall in love with the product",
  105. ... "but then it breaks",
  106. ... "usually around the time the 90 day warranty expires",
  107. ... "the twin towers collapsed today",
  108. ... "However, Mr. Carter solemnly argues, his client carried out the kidnapping \
  109. ... under orders and in the ''least offensive way possible.''"
  110. ... ]
  111. >>> sentences.extend(tricky_sentences)
  112. >>> for sentence in sentences:
  113. ... sid = SentimentIntensityAnalyzer()
  114. ... print(sentence)
  115. ... ss = sid.polarity_scores(sentence)
  116. ... for k in sorted(ss):
  117. ... print('{0}: {1}, '.format(k, ss[k]), end='')
  118. ... print()
  119. VADER is smart, handsome, and funny.
  120. compound: 0.8316, neg: 0.0, neu: 0.254, pos: 0.746,
  121. VADER is smart, handsome, and funny!
  122. compound: 0.8439, neg: 0.0, neu: 0.248, pos: 0.752,
  123. VADER is very smart, handsome, and funny.
  124. compound: 0.8545, neg: 0.0, neu: 0.299, pos: 0.701,
  125. VADER is VERY SMART, handsome, and FUNNY.
  126. compound: 0.9227, neg: 0.0, neu: 0.246, pos: 0.754,
  127. VADER is VERY SMART, handsome, and FUNNY!!!
  128. compound: 0.9342, neg: 0.0, neu: 0.233, pos: 0.767,
  129. VADER is VERY SMART, really handsome, and INCREDIBLY FUNNY!!!
  130. compound: 0.9469, neg: 0.0, neu: 0.294, pos: 0.706,
  131. The book was good.
  132. compound: 0.4404, neg: 0.0, neu: 0.508, pos: 0.492,
  133. The book was kind of good.
  134. compound: 0.3832, neg: 0.0, neu: 0.657, pos: 0.343,
  135. The plot was good, but the characters are uncompelling and the dialog is not great.
  136. compound: -0.7042, neg: 0.327, neu: 0.579, pos: 0.094,
  137. A really bad, horrible book.
  138. compound: -0.8211, neg: 0.791, neu: 0.209, pos: 0.0,
  139. At least it isn't a horrible book.
  140. compound: 0.431, neg: 0.0, neu: 0.637, pos: 0.363,
  141. :) and :D
  142. compound: 0.7925, neg: 0.0, neu: 0.124, pos: 0.876,
  143. <BLANKLINE>
  144. compound: 0.0, neg: 0.0, neu: 0.0, pos: 0.0,
  145. Today sux
  146. compound: -0.3612, neg: 0.714, neu: 0.286, pos: 0.0,
  147. Today sux!
  148. compound: -0.4199, neg: 0.736, neu: 0.264, pos: 0.0,
  149. Today SUX!
  150. compound: -0.5461, neg: 0.779, neu: 0.221, pos: 0.0,
  151. Today kinda sux! But I'll get by, lol
  152. compound: 0.2228, neg: 0.195, neu: 0.531, pos: 0.274,
  153. It was one of the worst movies I've seen, despite good reviews.
  154. compound: -0.7584, neg: 0.394, neu: 0.606, pos: 0.0,
  155. Unbelievably bad acting!!
  156. compound: -0.6572, neg: 0.686, neu: 0.314, pos: 0.0,
  157. Poor direction.
  158. compound: -0.4767, neg: 0.756, neu: 0.244, pos: 0.0,
  159. VERY poor production.
  160. compound: -0.6281, neg: 0.674, neu: 0.326, pos: 0.0,
  161. The movie was bad.
  162. compound: -0.5423, neg: 0.538, neu: 0.462, pos: 0.0,
  163. Very bad movie.
  164. compound: -0.5849, neg: 0.655, neu: 0.345, pos: 0.0,
  165. VERY bad movie.
  166. compound: -0.6732, neg: 0.694, neu: 0.306, pos: 0.0,
  167. VERY BAD movie.
  168. compound: -0.7398, neg: 0.724, neu: 0.276, pos: 0.0,
  169. VERY BAD movie!
  170. compound: -0.7616, neg: 0.735, neu: 0.265, pos: 0.0,
  171. Most automated sentiment analysis tools are shit.
  172. compound: -0.5574, neg: 0.375, neu: 0.625, pos: 0.0,
  173. VADER sentiment analysis is the shit.
  174. compound: 0.6124, neg: 0.0, neu: 0.556, pos: 0.444,
  175. Sentiment analysis has never been good.
  176. compound: -0.3412, neg: 0.325, neu: 0.675, pos: 0.0,
  177. Sentiment analysis with VADER has never been this good.
  178. compound: 0.5228, neg: 0.0, neu: 0.703, pos: 0.297,
  179. Warren Beatty has never been so entertaining.
  180. compound: 0.5777, neg: 0.0, neu: 0.616, pos: 0.384,
  181. I won't say that the movie is astounding and I wouldn't claim that the movie is too banal either.
  182. compound: 0.4215, neg: 0.0, neu: 0.851, pos: 0.149,
  183. I like to hate Michael Bay films, but I couldn't fault this one
  184. compound: 0.3153, neg: 0.157, neu: 0.534, pos: 0.309,
  185. It's one thing to watch an Uwe Boll film, but another thing entirely to pay for it
  186. compound: -0.2541, neg: 0.112, neu: 0.888, pos: 0.0,
  187. The movie was too good
  188. compound: 0.4404, neg: 0.0, neu: 0.58, pos: 0.42,
  189. This movie was actually neither that funny, nor super witty.
  190. compound: -0.6759, neg: 0.41, neu: 0.59, pos: 0.0,
  191. This movie doesn't care about cleverness, wit or any other kind of intelligent humor.
  192. compound: -0.1338, neg: 0.265, neu: 0.497, pos: 0.239,
  193. Those who find ugly meanings in beautiful things are corrupt without being charming.
  194. compound: -0.3553, neg: 0.314, neu: 0.493, pos: 0.192,
  195. There are slow and repetitive parts, BUT it has just enough spice to keep it interesting.
  196. compound: 0.4678, neg: 0.079, neu: 0.735, pos: 0.186,
  197. The script is not fantastic, but the acting is decent and the cinematography is EXCELLENT!
  198. compound: 0.7565, neg: 0.092, neu: 0.607, pos: 0.301,
  199. Roger Dodger is one of the most compelling variations on this theme.
  200. compound: 0.2944, neg: 0.0, neu: 0.834, pos: 0.166,
  201. Roger Dodger is one of the least compelling variations on this theme.
  202. compound: -0.1695, neg: 0.132, neu: 0.868, pos: 0.0,
  203. Roger Dodger is at least compelling as a variation on the theme.
  204. compound: 0.2263, neg: 0.0, neu: 0.84, pos: 0.16,
  205. they fall in love with the product
  206. compound: 0.6369, neg: 0.0, neu: 0.588, pos: 0.412,
  207. but then it breaks
  208. compound: 0.0, neg: 0.0, neu: 1.0, pos: 0.0,
  209. usually around the time the 90 day warranty expires
  210. compound: 0.0, neg: 0.0, neu: 1.0, pos: 0.0,
  211. the twin towers collapsed today
  212. compound: -0.2732, neg: 0.344, neu: 0.656, pos: 0.0,
  213. However, Mr. Carter solemnly argues, his client carried out the kidnapping under orders and in the ''least offensive way possible.''
  214. compound: -0.5859, neg: 0.23, neu: 0.697, pos: 0.074,