summaryrefslogtreecommitdiff
path: root/string_utils.py
diff options
context:
space:
mode:
authorScott Gasch <[email protected]>2021-08-05 14:56:34 -0700
committerScott Gasch <[email protected]>2021-08-05 14:56:34 -0700
commitb843703134a166013518c707fa5a77373f1bf0bf (patch)
treeedd85dee1f0ca34d7720ff8dc44b7a1b1bcc85a0 /string_utils.py
parenta08ca309cb5bd7971210a9247a38c9bbe376a6e6 (diff)
Adds profanity filter, fixes bugs.
Diffstat (limited to 'string_utils.py')
-rw-r--r--string_utils.py20
1 files changed, 19 insertions, 1 deletions
diff --git a/string_utils.py b/string_utils.py
index 6fc257d..45cf5aa 100644
--- a/string_utils.py
+++ b/string_utils.py
@@ -9,7 +9,7 @@ import logging
import random
import re
import string
-from typing import Any, Callable, List, Optional
+from typing import Any, Callable, Iterable, List, Optional
import unicodedata
from uuid import uuid4
@@ -963,3 +963,21 @@ def thify(n: int) -> str:
return "rd"
else:
return "th"
+
+
+def ngrams(txt: str, n: int):
+ words = txt.split()
+ return ngrams_presplit(words, n)
+
+
+def ngrams_presplit(words: Iterable[str], n: int):
+ for ngram in zip(*[words[i:] for i in range(n)]):
+ yield(' '.join(ngram))
+
+
+def bigrams(txt: str):
+ return ngrams(txt, 2)
+
+
+def trigrams(txt: str):
+ return ngrams(txt, 3)