Algorithm or formula to measure happiness?
You should be looking towards Natural Language Processing, specifically at Sentiment Analysis.  

The link I provided is a good starting point for learning about sentiment analysis.  If this is what you are looking for, it is available as part of Stanford's Core NLP.