[+] Implement get_senti_list

This commit is contained in:
Azalea (on HyDEV-Daisy)
2022-04-14 20:45:51 -04:00
parent f839f1e3f9
commit 20943b54fb
+16
View File
@@ -55,3 +55,19 @@ def get_senti(text: str) -> SentiResult:
return _decode_output(sp.check_output([*_cmd(), 'trinary', 'text', text.strip()]))
def get_senti_list(lst: Iterable[str]) -> list[SentiResult]:
"""
Get sentiment score of a list of text
This should be faster than running `get_senti` in a loop because this function only opens the
java runtime once.
:param lst: List of texts
:return: Sentiment scores of the list of texts
"""
# Open process
with sp.Popen([*_cmd(), 'trinary', 'stdin'], stdin=sp.PIPE, stdout=sp.PIPE, text=True) as p:
# Send input
combined_text = '\n'.join(lst)
stdout, _ = p.communicate(combined_text)
return [_decode_output(s) for s in stdout.split('\n') if s != '']