Skip to content

Add support for list of strings as input to sent_tokenize() #927

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Oct 28, 2024
14 changes: 10 additions & 4 deletions pythainlp/tokenize/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -323,16 +323,16 @@ def word_tokenize(


def sent_tokenize(
text: str,
text: Union[str, List[str]],
engine: str = DEFAULT_SENT_TOKENIZE_ENGINE,
keep_whitespace: bool = True,
) -> List[str]:
"""
Sentence tokenizer.

Tokenizes running text into "sentences"
Tokenizes running text into "sentences". Supports both string and list of strings.

:param str text: the text to be tokenized
:param text: the text (string) or list of words (list of strings) to be tokenized
:param str engine: choose among *'crfcut'*, *'whitespace'*, \
*'whitespace+newline'*
:return: list of split sentences
Expand Down Expand Up @@ -394,9 +394,15 @@ def sent_tokenize(
'และเขาได้รับมอบหมายให้ประจำในระดับภูมิภาค']
"""

if not text or not isinstance(text, str):
if not text or not isinstance(text, (str, list)):
return []

if isinstance(text, list):
try:
text = " ".join(text)
except TypeError:
return []

segments = []

if engine == "crfcut":
Expand Down
Loading