From 2d8bb6b71b2c9d674ec9d7d0df690588fecd8183 Mon Sep 17 00:00:00 2001 From: "Azalea (on HyDEV-Daisy)" Date: Thu, 14 Apr 2022 20:27:02 -0400 Subject: [PATCH] [+] Implement get_senti --- pysenti/__init__.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/pysenti/__init__.py b/pysenti/__init__.py index d1db1ea..1a2df67 100644 --- a/pysenti/__init__.py +++ b/pysenti/__init__.py @@ -1,5 +1,6 @@ from __future__ import annotations +import subprocess as sp from typing import NamedTuple, Iterable import pkg_resources @@ -30,3 +31,27 @@ def _cmd() -> list[str]: jar, data = _paths() return [JAVA_COMMAND, '-jar', jar, 'sentidata', data] + +def _decode_output(stdout: str | bytes) -> SentiResult: + """ + Decode SentiStrength java output + + :param stdout: The result that SentiStrength printed + :return: Parsed SentiResult + """ + if isinstance(stdout, bytes): + stdout = stdout.decode() + + return SentiResult(*[int(s) for s in stdout.strip().replace('\t', ' ').split(' ')]) + + +def get_senti(text: str) -> SentiResult: + """ + Get sentiment score of a text + + :param text: + :return: Sentiment result + """ + return _decode_output(sp.check_output([*_cmd(), 'trinary', 'text', text.strip()])) + +