MCPcopy
hub / github.com/InstaPy/InstaPy / sentiment_analysis

Function sentiment_analysis

instapy/text_analytics.py:236–284  ·  view source on GitHub ↗

Perform a detailed multilingual sentiment analysis of texts from different sources Available responses: https://github.com/MeaningCloud/meaningcloud-python/blob/master /meaningcloud/Response.py :return: Dictionary with the sentiment results or None

(text, language_of_text, logger)

Source from the content-addressed store, hash-verified

234
235
236def sentiment_analysis(text, language_of_text, logger):
237 """
238 Perform a detailed multilingual sentiment analysis of texts from
239 different sources
240 Available responses:
241 https://github.com/MeaningCloud/meaningcloud-python/blob/master
242 /meaningcloud/Response.py
243
244 :return:
245 Dictionary with the sentiment results or None
246 """
247
248 try:
249 # make a request to the Sentiment Analysis API
250 sentiment_response = SentimentResponse(
251 SentimentRequest(
252 key=MEANINGCLOUD_CONFIG["license_key"],
253 lang=language_of_text,
254 txt=text,
255 txtf="plain",
256 ).sendReq()
257 )
258 # check if there are any errors in the request
259 request_state = lift_meaningcloud_request(sentiment_response)
260 if request_state is not True:
261 return None
262
263 # get results
264 sentiment = sentiment_response.getResults()
265 if sentiment and "score_tag" in sentiment.keys() and sentiment["score_tag"]:
266 # if text has a question mark & its polarity is neither negative
267 # nor none, then label it neutral
268 return sentiment
269
270 else:
271 status_message = sentiment_response.getStatusMsg()
272 print("")
273 logger.error(
274 "{}\t~there was an unexpected error :|"
275 "\n{}\n".format(MEANINGCLOUD_FAILURE_MSG, status_message)
276 )
277 return None
278
279 except (ValueError, ConnectionError) as exc:
280 print("")
281 logger.exception(
282 "{}\t~{}\n".format(MEANINGCLOUD_FAILURE_MSG, str(exc).encode("utf-8"))
283 )
284 return None
285
286
287def detect_language(text):

Callers 1

text_analysisFunction · 0.85

Calls 1

Tested by

no test coverage detected