Skip to content

Instantly share code, notes, and snippets.

@databrit
Last active February 16, 2018 23:51
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save databrit/c7c0cb116c924b17eef689d27032f8f0 to your computer and use it in GitHub Desktop.
Save databrit/c7c0cb116c924b17eef689d27032f8f0 to your computer and use it in GitHub Desktop.
#SCRIPT_REAL is a function in Tableau which returns a result from an external service script. It's in this function we pass the python code.
SCRIPT_REAL("from nltk.sentiment import SentimentIntensityAnalyzer
text = _arg1 #you have to use _arg1 to reference the data column you're analyzing, in this case [Word]. It gets word further down after the ,
scores = [] #this is a python list where the scores will get stored
sid = SentimentIntensityAnalyzer() #this is a class from the nltk (Natural Language Toolkit) library. We'll pass our words through this to return the score
for word in text: # this loops through each row in the column you pass via _arg1; in this case [Word]
ss = sid.polarity_scores(word) #passes the word through the sentiment analyzer to get the score
scores.append(ss['compound']) #appends the score to the list of scores
return scores #returns the scores
"
,ATTR([Word]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment