- 
      
 - 
        
Save developius/c347edb319b11c339872ad78a4c36308 to your computer and use it in GitHub Desktop.  
    Sentiment Analysis function for FaaS
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | FROM python:2.7-alpine | |
| RUN pip install textblob | |
| RUN python -m textblob.download_corpora | |
| ADD https://github.com/alexellis/faas/releases/download/0.5.1-alpha/fwatchdog /usr/bin | |
| RUN chmod +x /usr/bin/fwatchdog | |
| WORKDIR /root/ | |
| COPY handler.py . | |
| ENV fprocess="python handler.py" | |
| HEALTHCHECK --interval=1s CMD [ -e /tmp/.lock ] || exit 1 | |
| CMD ["fwatchdog"] | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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 | |
| print(json.dumps(res)) | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment