Chapter VII
N-gram Tokenization
NLP / Tokenization
❦ ❦ ❦
The Source
§ I format_punctuation
def format_punctuation(text, chars):
for char in chars:
text = text.replace(char, f" {char} ")
return text
§ II generate_char_level_ngrams
def generate_char_level_ngrams(text: str, n: int):
joint_text = "_".join(text.lower().split())
tokens = []
L = len(joint_text)
# n=3; 'abcd'; last iter starts from idx 1 (4-3+1, and -1 is 1); that is 'b' to capture 'bcd'
for i in range(L - n + 1):
tokens.append(tuple(joint_text[i : i + n])) # 'abc' -> ('a', 'b', 'c')
return tokens
§ III generate_word_level_ngrams
def generate_word_level_ngrams(text: str, n: int):
text = format_punctuation(text, punctuation) # "!"#$%&'()*+,-./:;<=>?
split_text = text.lower().split()
tokens = []
L = len(split_text)
for i in range(L - n + 1):
tokens.append(tuple(split_text[i : i + n])) # ['the', 'cat'] -> ('the', 'cat')
return tokens
§ IV generate_ngrams
def generate_ngrams(text: str, n: int, level="char") -> list[tuple[str, ...]]:
if n <= 0:
raise ValueError("n must be greater than 0")
match level:
case "char":
return generate_char_level_ngrams(text, n)
case "word":
return generate_word_level_ngrams(text, n)
case _:
raise ValueError("wrong level")
§ V Demonstration
if __name__ == "__main__":
text = """Sitting alone in a café without distractions only gets better when there is something to write on. Not with a keyboard. You must use your single hand to write, not two. Ideally, with a pen on paper."""
# ngrams = generate_ngrams(text, 1, "word")
ngrams = generate_ngrams(text, 2, "word")
# ngrams = generate_ngrams(text, 3, "char")
# ngrams = generate_ngrams(text, 4, "char")
# ngrams = generate_ngrams(text, 5, "char")
print(ngrams)
print(len(ngrams))
✦ ✦ ✦
Verification Appendix
§ I TestFormatPunctuation
class TestFormatPunctuation(unittest.TestCase):
def test(self):
f = format_punctuation
tests = {
"given_empty_text_return_empty_text": {
"text": "",
"chars": "any",
"want": "",
},
"given_text_with_no_puntuation_return_same_text": {
"text": "a b c d",
"chars": ".",
"want": "a b c d",
},
"given_text_with_punctuation_return_formatted_text_0": {
"text": "a.b.c.d",
"chars": ".",
"want": "a . b . c . d",
},
"given_text_with_punctuation_return_formatted_text_1": {
"text": "a. b. c. d",
"chars": ".",
"want": "a . b . c . d",
},
"given_text_with_punctuation_return_formatted_text_2": {
"text": "¿a.b.c.d?",
"chars": ".¿?",
"want": " ¿ a . b . c . d ? ",
},
"given_text_with_unmatched_punctuation_return_same_text": {
"text": "a.b.c.d",
"chars": "_", # no matching punctuation char
"want": "a.b.c.d",
},
"given_only_punctuated_text_return_formatted_text": {
"text": ".....",
"chars": ".",
"want": " . . . . . ",
},
}
for name, case in tests.items():
with self.subTest(name):
self.assertEqual(f(case["text"], case["chars"]), case["want"])
§ II TestGenerateCharLevelNGrams
class TestGenerateCharLevelNGrams(unittest.TestCase):
def test(self):
f = generate_char_level_ngrams
tests = {
"given_empty_text_return_empty_list": {
"text": "",
"n": 3, # any
"want": [],
},
"given_one_char_and_unigram_return_one_unigram": {
"text": "a",
"n": 1,
"want": [("a",)],
},
"given_one_char_and_bigram_return_empty_list": {
"text": "a",
"n": 2, # no bigrams in text
"want": [],
},
"given_two_chars_and_trigram_return_empty_list": {
"text": "ab",
"n": 3, # no trigrams in text
"want": [],
},
"given_two_chars_and_bigram_return_one_bigram": {
"text": "ab",
"n": 2,
"want": [("a", "b")],
},
"given_text_and_n_return_ngrams_0": {
"text": "abc",
"n": 2,
"want": [("a", "b"), ("b", "c")],
},
"given_text_and_n_return_ngrams_1": {
"text": "a c",
"n": 2,
"want": [("a", "_"), ("_", "c")],
},
"given_text_and_n_return_ngrams_2": {
"text": "a.b",
"n": 2,
"want": [("a", "."), (".", "b")],
},
"given_text_and_n_return_ngrams_3": {
"text": "a _",
"n": 2,
"want": [("a", "_"), ("_", "_")],
},
"given_text_and_n_return_ngrams_4": {
"text": "abcd",
"n": 3,
"want": [("a", "b", "c"), ("b", "c", "d")],
},
}
for name, case in tests.items():
with self.subTest(name):
self.assertEqual(f(case["text"], case["n"]), case["want"])
§ III TestGenerateWordLevelNGrams
class TestGenerateWordLevelNGrams(unittest.TestCase):
def test(self):
f = generate_word_level_ngrams
tests = {
"given_empty_text_return_empty_list": {
"text": "",
"n": 3, # any
"want": [],
},
"given_one_word_and_unigram_return_one_word_unigram": {
"text": "word",
"n": 1,
"want": [("word",)],
},
"given_one_word_and_bigram_return_empty_list": {
"text": "word",
"n": 2, # no bigrams in text
"want": [],
},
"given_two_words_and_trigram_return_empty_list": {
"text": "two words",
"n": 3, # no trigrams in text
"want": [],
},
"given_two_words_and_bigram_return_one_bigram": {
"text": "hello world",
"n": 2,
"want": [("hello", "world")],
},
"given_text_and_n_return_ngrams_0": {
"text": "happy new year",
"n": 2,
"want": [("happy", "new"), ("new", "year")],
},
"given_text_and_n_return_ngrams_1": {
"text": "hello world!", # ! is puntuaction: a separate token
"n": 2,
"want": [("hello", "world"), ("world", "!")],
},
"given_text_and_n_return_ngrams_2": {
"text": "HAPPY NEW YEAR!", # tokens get lowecased
"n": 3,
"want": [("happy", "new", "year"), ("new", "year", "!")],
},
}
for name, case in tests.items():
with self.subTest(name):
self.assertEqual(f(case["text"], case["n"]), case["want"])