Contractions and stuff in string_utils.
authorScott Gasch <[email protected]>
Thu, 3 Feb 2022 19:36:13 +0000 (11:36 -0800)
committerScott Gasch <[email protected]>
Thu, 3 Feb 2022 19:36:13 +0000 (11:36 -0800)
string_utils.py

index 55e67313253d650116a4fab3c9902420fb277a3c..1ed9b4ad10790685290838cd30a2322d59b1b373 100644 (file)
@@ -1245,6 +1245,32 @@ class SprintfStdout(object):
         return None  # don't suppress exceptions
 
 
+def capitalize_first_letter(txt: str) -> str:
+    """Capitalize the first letter of a string.
+
+    >>> capitalize_first_letter('test')
+    'Test'
+    >>> capitalize_first_letter("ALREADY!")
+    'ALREADY!'
+
+    """
+    return txt[0].upper() + txt[1:]
+
+
+def it_they(n: int) -> str:
+    """It or they?
+
+    >>> it_they(1)
+    'it'
+    >>> it_they(100)
+    'they'
+
+    """
+    if n == 1:
+        return "it"
+    return "they"
+
+
 def is_are(n: int) -> str:
     """Is or are?
 
@@ -1277,6 +1303,95 @@ def pluralize(n: int) -> str:
     return "s"
 
 
+def make_contractions(txt: str) -> str:
+    """Glue words together to form contractions.
+
+    >>> make_contractions('It is nice today.')
+    "It's nice today."
+
+    >>> make_contractions('I can    not even...')
+    "I can't even..."
+
+    >>> make_contractions('She could not see!')
+    "She couldn't see!"
+
+    >>> make_contractions('But she will not go.')
+    "But she won't go."
+
+    >>> make_contractions('Verily, I shall not.')
+    "Verily, I shan't."
+
+    >>> make_contractions('No you cannot.')
+    "No you can't."
+
+    >>> make_contractions('I said you can not go.')
+    "I said you can't go."
+
+    """
+
+    first_second = [
+        (
+            [
+                'are',
+                'could',
+                'did',
+                'has',
+                'have',
+                'is',
+                'must',
+                'should',
+                'was',
+                'were',
+                'would',
+            ],
+            ['(n)o(t)'],
+        ),
+        (
+            [
+                "I",
+                "you",
+                "he",
+                "she",
+                "it",
+                "we",
+                "they",
+                "how",
+                "why",
+                "when",
+                "where",
+                "who",
+                "there",
+            ],
+            ['woul(d)', 'i(s)', 'a(re)', 'ha(s)', 'ha(ve)', 'ha(d)', 'wi(ll)'],
+        ),
+    ]
+
+    # Special cases
+    txt = re.sub(r'\b(can)\s*no(t)\b', r"\1'\2", txt, count=0, flags=re.IGNORECASE)
+    txt = re.sub(
+        r'\b(sha)ll\s*(n)o(t)\b', r"\1\2'\3", txt, count=0, flags=re.IGNORECASE
+    )
+    txt = re.sub(
+        r'\b(w)ill\s*(n)(o)(t)\b', r"\1\3\2'\4", txt, count=0, flags=re.IGNORECASE
+    )
+
+    for first_list, second_list in first_second:
+        for first in first_list:
+            for second in second_list:
+                # Disallow there're.  It's valid English but sounds weird.
+                if first == 'there' and second == 'a(re)':
+                    continue
+
+                pattern = fr'\b({first})\s+{second}\b'
+                if second == '(n)o(t)':
+                    replacement = r"\1\2'\3"
+                else:
+                    replacement = r"\1'\2"
+                txt = re.sub(pattern, replacement, txt, count=0, flags=re.IGNORECASE)
+
+    return txt
+
+
 def thify(n: int) -> str:
     """Return the proper cardinal suffix for a number.