Guest User

Untitled

a guest
Dec 14th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. from textblob import TextBlob
  2.  
  3. def calculatesentiment(text):
  4. """
  5. Calculate polarity and subjectivity
  6.  
  7. Input:
  8. text - text for which polarity and subjectivity needs to be calculated
  9.  
  10. Output:
  11. (polarity, subjectivity)
  12. """
  13. blob = TextBlob(text)
  14.  
  15. if blob.detect_language() != 'en':
  16. try:
  17. blob = blob.translate(to="en")
  18. except:
  19. print("Translation error:",blob.detect_language())
  20. print(blob)
  21.  
  22. blob.correct()
  23.  
  24. return (float(blob.sentiment.polarity),
  25. float(blob.sentiment.subjectivity))
Add Comment
Please, Sign In to add comment