File tree Expand file tree Collapse file tree 2 files changed +69
-0
lines changed
Expand file tree Collapse file tree 2 files changed +69
-0
lines changed Original file line number Diff line number Diff line change 1+ #!/usr/bin/env python
2+ # coding=utf-8
3+ import dominate
4+ from dominate .tags import *
5+ from newspaper import Article
6+ from textblob import TextBlob
7+
8+ from qutescript import userscript
9+
10+ polarity_map = {
11+ 8 : 'green' ,
12+ 5 : 'olive' ,
13+ 2 : '#333' ,
14+ 0 : '#777' ,
15+ - 2 : 'orange' ,
16+ - 5 : 'red' ,
17+ - 8 : 'brown' ,
18+ }
19+
20+
21+ def get_polarity_color (polarity ):
22+ for thresh in reversed (sorted (polarity_map .keys ())):
23+ if (polarity * 10 ) >= thresh :
24+ return polarity_map [thresh ]
25+ else :
26+ return '#777'
27+
28+
29+ def generate_html (paragraphs , title_text ):
30+ doc = dominate .document (title = 'Summary: {}' .format (title_text ))
31+
32+ with doc .head :
33+ style ("""\
34+ body {
35+ background-color: #F9F8F1;
36+ color: #2C232A;
37+ font-family: sans-serif;
38+ font-size: 1.2em;
39+ }
40+
41+ """ )
42+
43+ with doc :
44+ div (id = 'header' ).add (h1 (title_text ))
45+ with div ():
46+ attr (cls = 'body' )
47+ for para in paragraphs :
48+ tb = TextBlob (para )
49+ with p ():
50+ for sentence in tb .sentences :
51+ span (sentence , style = "color: {}" .format (get_polarity_color (sentence .polarity )))
52+ return doc
53+
54+
55+ @userscript
56+ def sentiment_markup (request ):
57+ article = Article (request .url )
58+ # article.download(request.html, request.title)
59+ article .download ()
60+ article .parse ()
61+ html = generate_html (article .text .split ('\n \n ' ), article .title ).render ()
62+ request .send_html (html )
63+
64+
65+ if __name__ == '__main__' :
66+ sentiment_markup ()
Original file line number Diff line number Diff line change 1+ dominate
2+ newspaper3k
3+ textblob
You can’t perform that action at this time.
0 commit comments