Advertisement
Guest User

Untitled

a guest
Apr 4th, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. import json
  2.  
  3. from watson_developer_cloud import ToneAnalyzerV3
  4.  
  5. from nltk.sentiment.vader import SentimentIntensityAnalyzer
  6.  
  7.  
  8. class SentimentService():
  9. def __init__(self):
  10. self.service = SentimentIntensityAnalyzer()
  11.  
  12. def analyze(self, text):
  13. return self.format_polarity(self.service.polarity_scores(text))
  14.  
  15. def format_polarity(self, polarity):
  16. return polarity
  17.  
  18.  
  19. class ToneWatsonService():
  20. DEFAULT_VERSION = '2016-02-11'
  21.  
  22. def __init__(self, access):
  23. '''
  24. access is a dict with username and password keys
  25. '''
  26. self.access = access
  27.  
  28. def process(self, text):
  29. '''
  30. Response:
  31. {
  32. "document_tone": {
  33. "tone_categories": [
  34. {
  35. "tones": [
  36. {
  37. "score": 0.000264,
  38. "tone_id": "anger",
  39. "tone_name": "Anger"
  40. },
  41. {
  42. "score": 0.00016,
  43. "tone_id": "disgust",
  44. "tone_name": "Disgust"
  45. },
  46. {
  47. "score": 0.000376,
  48. "tone_id": "fear",
  49. "tone_name": "Fear"
  50. },
  51. {
  52. "score": 0.996063,
  53. "tone_id": "joy",
  54. "tone_name": "Joy"
  55. },
  56. {
  57. "score": 0.001101,
  58. "tone_id": "sadness",
  59. "tone_name": "Sadness"
  60. }
  61. ],
  62. "category_id": "emotion_tone",
  63. "category_name": "Emotion Tone"
  64. },
  65. {
  66. "tones": [
  67. {
  68. "score": 0.0,
  69. "tone_id": "analytical",
  70. "tone_name": "Analytical"
  71. },
  72. {
  73. "score": 0.97759,
  74. "tone_id": "confident",
  75. "tone_name": "Confident"
  76. },
  77. {
  78. "score": 0.0,
  79. "tone_id": "tentative",
  80. "tone_name": "Tentative"
  81. }
  82. ],
  83. "category_id": "writing_tone",
  84. "category_name": "Writing Tone"
  85. },
  86. {
  87. "tones": [
  88. {
  89. "score": 0.096859,
  90. "tone_id": "openness_big5",
  91. "tone_name": "Openness"
  92. },
  93. {
  94. "score": 0.264058,
  95. "tone_id": "conscientiousness_big5",
  96. "tone_name": "Conscientiousness"
  97. },
  98. {
  99. "score": 0.472657,
  100. "tone_id": "extraversion_big5",
  101. "tone_name": "Extraversion"
  102. },
  103. {
  104. "score": 0.61522,
  105. "tone_id": "agreeableness_big5",
  106. "tone_name": "Agreeableness"
  107. },
  108. {
  109. "score": 0.104851,
  110. "tone_id": "neuroticism_big5",
  111. "tone_name": "Emotional Range"
  112. }
  113. ],
  114. "category_id": "social_tone",
  115. "category_name": "Social Tone"
  116. }
  117. ]
  118. }
  119. }
  120. '''
  121. tone_analyzer = ToneAnalyzerV3(
  122. username=self.access['username'],
  123. password=self.access['password'],
  124. version=self.DEFAULT_VERSION)
  125.  
  126. return tone_analyzer.tone(text)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement