[+] Implement get_senti

This commit is contained in:
Azalea (on HyDEV-Daisy)
2022-04-14 20:27:02 -04:00
parent aa3c19e1e2
commit 2d8bb6b71b
+25
View File
@@ -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()]))