TecentNLP for sentiment analysis

Sentiment Analysis:

The process of computationally identifying and categorizing opinions expressed in a piece of text, especially in order to determine whether the writer’s sentiment and attitude towards a particular topic, product, etc. is positive, negative, or neutral.Sentiment analysis is widely applied to voice of the customer materials such as reviews and survey responses, online and social media, and healthcare materials for applications that range from marketing to customer service to clinical medicine. A basic task in sentiment analysis is classifying the polarity of a given text at the document, sentence, or feature/aspect level—whether the expressed opinion in a document, a sentence or an entity feature/aspect is positive, negative, or neutral. Advanced, “beyond polarity” sentiment classification looks, for instance, at emotional states such as “angry”, “sad”, and “happy“.

Sentiment analysis is often employed by researchers for text mining.It’s getting more and more accurate today. But details such as irony, humor or sarcasm are barely discernible with a simple sentiment analysis. However,There are some studies that have yielded very interesting findings using simple sentiment analysis.

For example,Siqi Zheng(2019)and his colleague claim that high levels of air pollution in China may contribute to the urban population’s reported low level of happiness. The important thing is,how they measure “happiness”? Actually, they did it with a very simple way - use the tweets with geo tags to construct their city/day happiness index.

  • use the ‘Tencent’ natural language processing (NLP) platform to do sentiment analysis.
  • ‘Boson’ for a robustness check.

See? They didn’t even do manual tagging!

I am currently working on a research on the GM discussion on a Knowledge Q&A website.I used the emotional dictionary method to analyze the sentiment in the texts and want to use the Tencent NLP platform for robustness testing. But the official website only provides PHP code examples. So I wrote an example of using Python to call this api, and put this demo here, hoping to help those who want to use this platform to do research but are not familiar with programming.

#Python 3.6
from QcloudApi.qcloudapi import QcloudApi

module = 'wenzhi'
action = 'TextSentiment'
config = {
    'method': 'GET',
    'Region': 'ap-guangzhou',
    'secretId': 'Your secretId',
    'secretKey': 'Your secretKey',
   }
   
service = QcloudApi(module, config)
#service.setRegion('ap-shanghai')

def get_sentiments(text):
    try:
        params = {'content':text,'type':4}
        s=service.call(action,params).decode() 
        s= eval(s)     
        positive=s['positive']#positive possiblity  
        negative=s['negative'] #negative possiblity 
        output = [positive,negative]
        return output
    except Exception as e:
        print ('a', str(e))
        return [0,0]

Very simple, right?