From 5115bc85551e8d3472b308549cd7eed3cbf93e2f Mon Sep 17 00:00:00 2001 From: Harshad Sharma Date: Sat, 17 Jun 2017 06:13:15 +0530 Subject: [PATCH] Add example: Sentimark - sentence-level sentiment analysis and markup. --- examples/sentimark.py | 66 +++++++++++++++++++++++++++++ examples/sentimark_requirements.txt | 3 ++ 2 files changed, 69 insertions(+) create mode 100755 examples/sentimark.py create mode 100644 examples/sentimark_requirements.txt diff --git a/examples/sentimark.py b/examples/sentimark.py new file mode 100755 index 0000000..bd9f51d --- /dev/null +++ b/examples/sentimark.py @@ -0,0 +1,66 @@ +#!/usr/bin/env python +# coding=utf-8 +import dominate +from dominate.tags import * +from newspaper import Article +from textblob import TextBlob + +from qutescript import userscript + +polarity_map = { + 8: 'green', + 5: 'olive', + 2: '#333', + 0: '#777', + -2: 'orange', + -5: 'red', + -8: 'brown', +} + + +def get_polarity_color(polarity): + for thresh in reversed(sorted(polarity_map.keys())): + if (polarity * 10) >= thresh: + return polarity_map[thresh] + else: + return '#777' + + +def generate_html(paragraphs, title_text): + doc = dominate.document(title='Summary: {}'.format(title_text)) + + with doc.head: + style("""\ + body { + background-color: #F9F8F1; + color: #2C232A; + font-family: sans-serif; + font-size: 1.2em; + } + + """) + + with doc: + div(id='header').add(h1(title_text)) + with div(): + attr(cls='body') + for para in paragraphs: + tb = TextBlob(para) + with p(): + for sentence in tb.sentences: + span(sentence, style="color: {}".format(get_polarity_color(sentence.polarity))) + return doc + + +@userscript +def sentiment_markup(request): + article = Article(request.url) + # article.download(request.html, request.title) + article.download() + article.parse() + html = generate_html(article.text.split('\n\n'), article.title).render() + request.send_html(html) + + +if __name__ == '__main__': + sentiment_markup() diff --git a/examples/sentimark_requirements.txt b/examples/sentimark_requirements.txt new file mode 100644 index 0000000..fb2ce30 --- /dev/null +++ b/examples/sentimark_requirements.txt @@ -0,0 +1,3 @@ +dominate +newspaper3k +textblob