Stuff.
[python_utils.git] / string_utils.py
index 0829846bb26741bfb0c8bdd0226d56511fc31bbd..78e72cca5a36e672fdc8931cf9a9b9b946ac148e 100644 (file)
@@ -10,10 +10,12 @@ import numbers
 import random
 import re
 import string
-from typing import Any, Callable, Dict, Iterable, List, Optional, Tuple
+from typing import Any, Callable, Dict, Iterable, List, Optional, Sequence, Tuple
 import unicodedata
 from uuid import uuid4
 
+import list_utils
+
 logger = logging.getLogger(__name__)
 
 NUMBER_RE = re.compile(r"^([+\-]?)((\d+)(\.\d+)?([e|E]\d+)?|\.\d+)$")
@@ -1059,18 +1061,26 @@ def to_bool(in_str: str) -> bool:
 
     >>> to_bool('True')
     True
+
     >>> to_bool('1')
     True
+
     >>> to_bool('yes')
     True
+
     >>> to_bool('no')
     False
+
     >>> to_bool('huh?')
     False
+
+    >>> to_bool('on')
+    True
+
     """
     if not is_string(in_str):
         raise ValueError(in_str)
-    return in_str.lower() in ("true", "1", "yes", "y", "t")
+    return in_str.lower() in ("true", "1", "yes", "y", "t", "on")
 
 
 def to_date(in_str: str) -> Optional[datetime.date]:
@@ -1274,12 +1284,15 @@ def ngrams(txt: str, n: int):
 
     """
     words = txt.split()
-    return ngrams_presplit(words, n)
+    for ngram in ngrams_presplit(words, n):
+        ret = ''
+        for word in ngram:
+            ret += f'{word} '
+        yield ret.strip()
 
 
-def ngrams_presplit(words: Iterable[str], n: int):
-    for ngram in zip(*[words[i:] for i in range(n)]):
-        yield(' '.join(ngram))
+def ngrams_presplit(words: Sequence[str], n: int):
+    return list_utils.ngrams(words, n)
 
 
 def bigrams(txt: str):