import random
from collections import Counter
from itertools import chain, combinations, islice
-from typing import Any, Iterator, List, MutableSequence, Sequence, Tuple
+from typing import (
+ Any,
+ Generator,
+ Iterator,
+ List,
+ MutableSequence,
+ Sequence,
+ Tuple,
+ TypeVar,
+)
def shard(lst: List[Any], size: int) -> Iterator[Any]:
return [list(_) for _ in transposed]
-def ngrams(lst: Sequence[Any], n: int):
+T = TypeVar('T')
+
+
+def ngrams(lst: Sequence[T], n: int) -> Generator[Sequence[T], T, None]:
"""
Return the ngrams in the sequence.
yield lst[i : i + n]
-def permute(seq: str):
+def permute(seq: str) -> Generator[str, str, None]:
"""
Returns all permutations of a sequence.
yield from _permute(seq, "")
-def _permute(seq: str, path: str):
+def _permute(seq: str, path: str) -> Generator[str, str, None]:
"""Internal helper to permute items recursively."""
seq_len = len(seq)
if seq_len == 0:
Any,
Callable,
Dict,
+ Generator,
Iterable,
List,
Literal,
return "th"
-def ngrams(txt: str, n: int):
+get_cardinal_suffix = thify
+
+
+def add_cardinal_suffix(n: int):
+ """
+ Args:
+ n: the number to return as a string with a cardinal suffix.
+
+ Returns:
+ A string containing the number with its cardinal suffix.
+
+ >>> add_cardinal_suffix(123)
+ '123rd'
+
+ >>> add_cardinal_suffix(1)
+ '1st'
+
+ >>> add_cardinal_suffix(0)
+ '0th'
+
+ >>> add_cardinal_suffix(-123)
+ '-123rd'
+ """
+ return f'{n}{get_cardinal_suffix(n)}'
+
+
+def remove_cardinal_suffix(txt: str) -> Optional[str]:
+ """
+ Args:
+ txt: the number with cardinal suffix to strip.
+
+ Returns:
+ The same string with its cardinal suffix removed or None on error.
+
+ >>> remove_cardinal_suffix('123rd')
+ '123'
+
+ >>> remove_cardinal_suffix('-10th')
+ '-10'
+
+ >>> remove_cardinal_suffix('1ero') is None
+ True
+ """
+ suffix = txt[-2:]
+ if suffix in set(['st', 'nd', 'rd', 'th']):
+ return txt[:-2]
+ return None
+
+
+def ngrams(txt: str, n: int) -> Generator[str, str, None]:
"""
Args:
txt: the string to create ngrams using
yield ret.strip()
-def ngrams_presplit(words: Sequence[str], n: int):
+def ngrams_presplit(
+ words: Sequence[str], n: int
+) -> Generator[Sequence[str], str, None]:
"""
Same as :meth:`ngrams` but with the string pre-split.
return list_utils.ngrams(words, n)
-def bigrams(txt: str):
+def bigrams(txt: str) -> Generator[str, str, None]:
"""Generates the bigrams (n=2) of the given string.
See also :meth:`ngrams`, :meth:`trigrams`.
return ngrams(txt, 2)
-def trigrams(txt: str):
+def trigrams(txt: str) -> Generator[str, str, None]:
"""Generates the trigrams (n=3) of the given string.
See also :meth:`ngrams`, :meth:`bigrams`.