Skip to content

Instantly share code, notes, and snippets.

@0asa
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save 0asa/37bc839b331b1c4abc0b to your computer and use it in GitHub Desktop.
Save 0asa/37bc839b331b1c4abc0b to your computer and use it in GitHub Desktop.
(brutally) Add color to a given text according to 'sentiment analysis' with Pattern (http://www.clips.ua.ac.be/pattern).
from pattern.en import sentiment
import colors
items = sentiment.viewitems()
# some text
text = ("Then, the module's variables, functions, and classes will be available to the caller through "
"the module’s namespace, a central concept in programming that is particularly helpful and "
"powerful in Python. Thanks to the way imports and modules are handled in Python, it is "
"relatively easy to structure a Python project. Easy, here, means that you do not have many "
"constraints and that the module importing model is easy to grasp. Therefore, you are left "
"with the pure architectural task of crafting the different parts of your project and their "
"interactions. A few years back, Hadoop was essentially MapReduce, a batch-oriented system for "
"processing large amounts of data, leading people to mistakenly conflate Hadoop with Big Data "
"and Big Data with lots and lots of data. But if the market was confused then, it's far worse "
"today. Because Hadoop is taking on all sorts of capabilities that just two years ago were "
"considered impossible.")
assess = sentiment(text).assessments
colored = text
c = 0
for a in assess:
c += 1
col = round(abs(a[1]) * 255)
if a[1] > 0:
color = '#%s' % colors.rgb(0,col,0).hex
else:
color = '#%s' % colors.rgb(col,0,0).hex
colored = colored.replace(a[0][0],'<span style="color:'+color+'">'+a[0][0]+'</span>')
out = open('out.html','w')
out.write('<span style="font-size:1.25em;font-family:Arial;color:#BBB">' + colored + '</span>')
out.close()
@0asa
Copy link
Author

0asa commented Oct 16, 2014

Note that you might need to do the following:

pip install pattern

pip install colors.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment