Skip to content

Instantly share code, notes, and snippets.

@alexellis
Created May 2, 2017 10:00
Show Gist options
  • Select an option

  • Save alexellis/abd084d1f57f97198389bcad731c3d29 to your computer and use it in GitHub Desktop.

Select an option

Save alexellis/abd084d1f57f97198389bcad731c3d29 to your computer and use it in GitHub Desktop.

Revisions

  1. alexellis created this gist May 2, 2017.
    16 changes: 16 additions & 0 deletions Dockerfile
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    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"]
    29 changes: 29 additions & 0 deletions handler.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    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"] = res["subjectivity"] + sentence.sentiment.subjectivity
    res["polarity"] = res["polarity"] + sentence.sentiment.polarity

    total = len(blob.sentences)

    res["sentence_count"] = total
    res["polarity"] = res["polarity"] / total
    res["subjectivity"] = res["subjectivity"] / total

    print(json.dumps(res))