[+] Implement get_senti
This commit is contained in:
@@ -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()]))
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user