summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRose Hogenson <rhogenson@posteo.net>2022-02-15 07:04:12 -0800
committerRose Hogenson <rhogenson@posteo.net>2022-02-15 07:04:12 -0800
commita906adb9ea2073ee9c23913585ff821558717ac8 (patch)
tree9fb4d22506c4711d78129d2313871c4992c86542
parent185851ec959d99c7c92309781bf5212c5433dc05 (diff)
downloadgpt-adventure-a906adb9ea2073ee9c23913585ff821558717ac8.tar.zst
Auto-download nltk packages if they're missing.
-rwxr-xr-xai_dungeon.py7
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])