Skip to content

Instantly share code, notes, and snippets.

Forked from alexellis/Dockerfile
Last active May 9, 2018
What would you like to do?
Sentiment Analysis function for FaaS
FROM python:2.7-alpine
RUN pip install textblob
RUN python -m textblob.download_corpora
ADD /usr/bin
RUN chmod +x /usr/bin/fwatchdog
WORKDIR /root/
ENV fprocess="python"
HEALTHCHECK --interval=1s CMD [ -e /tmp/.lock ] || exit 1
CMD ["fwatchdog"]
import sys
import json
from textblob import TextBlob
def get_stdin():
buf = ""
for line in sys.stdin:
buf = buf + line
return buf
if(__name__ == "__main__"):
st = get_stdin()
blob = TextBlob(st)
res = {
"polarity": 0,
"subjectivity": 0
for sentence in blob.sentences:
res["subjectivity"] += sentence.sentiment.subjectivity
res["polarity"] = += sentence.sentiment.polarity
total = len(blob.sentences)
res["sentence_count"] = total
res["polarity"] /= total
res["subjectivity"] /= total
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment