diff options
| author | Rose Hogenson <rhogenson@posteo.net> | 2022-02-15 07:04:12 -0800 |
|---|---|---|
| committer | Rose Hogenson <rhogenson@posteo.net> | 2022-02-15 07:04:12 -0800 |
| commit | a906adb9ea2073ee9c23913585ff821558717ac8 (patch) | |
| tree | 9fb4d22506c4711d78129d2313871c4992c86542 | |
| parent | 185851ec959d99c7c92309781bf5212c5433dc05 (diff) | |
| download | gpt-adventure-a906adb9ea2073ee9c23913585ff821558717ac8.tar.zst | |
Auto-download nltk packages if they're missing.
| -rwxr-xr-x | ai_dungeon.py | 7 |
1 files changed, 6 insertions, 1 deletions
diff --git a/ai_dungeon.py b/ai_dungeon.py index a73cc7e..58ad206 100755 --- a/ai_dungeon.py +++ b/ai_dungeon.py @@ -6,6 +6,7 @@ import argparse import re import sys +import nltk from nltk import tokenize import transformers @@ -15,7 +16,11 @@ sentence_end = re.compile(r"[.?!\"')]$") def trim_sentence(message: str) -> str: """Remove extra output after the last period.""" - sentences = tokenize.sent_tokenize(message) + try: + sentences = tokenize.sent_tokenize(message) + except LookupError: + nltk.download("punkt") + sentences = tokenize.sent_tokenize(message) if sentence_end.search(sentences[-1]) or len(sentences) == 1: return message return " ".join(sentences[:-1]) |
