-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathngram.py
144 lines (100 loc) · 3.9 KB
/
ngram.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
import math
class NGram:
"""
N-gram language model.
"""
def __init__(self, n: int, vocab_set: set) -> None:
assert n >= 1
self.n = n
self.vocab_set = vocab_set
self.counter = None
def tokenize(self, sentence: str) -> list[str]:
start = ["<START>"] * (self.n - 1) if self.n > 1 else ["<START>"]
end = ["<END>"] * (self.n - 1) if self.n > 1 else ["<END>"]
tokens = start + sentence.strip().split() + end
for i in range(len(tokens)):
if tokens[i] not in self.vocab_set:
tokens[i] = "<UNK>"
return tokens
def fit(self, train_lines: list[str]) -> None:
counter = {}
for line in train_lines:
tokens = self.tokenize(line)
for i in range(len(tokens) - self.n + 1):
token_set = tuple(tokens[i : i + self.n])
subset = tuple(token_set[:-1])
if token_set in counter:
counter[token_set] += 1
else:
counter[token_set] = 1
if subset in counter:
counter[subset] += 1
else:
counter[subset] = 1
self.counter = counter
def get_log_prob(self, token_set: tuple) -> float:
subset = tuple(token_set[:-1])
return math.log2(self.counter[token_set] / self.counter[subset])
def get_perplexity(self, test_lines: list[str]) -> float:
assert self.counter != None
log_sum = 0
N = 0
for line in test_lines:
tokens = self.tokenize(line)
N += len(tokens)
for i in range(len(tokens) - self.n + 1):
token_set = tuple(tokens[i : i + self.n])
if token_set in self.counter:
log_sum += self.get_log_prob(token_set)
else:
return float("inf")
return pow(2, (-1 / N) * log_sum)
def predict(self, context: tuple) -> str:
assert self.counter != None
max_prob = 0
next_token = "<UNK>"
for token in self.vocab_set:
token_set = context + (token, )
if token_set in self.counter:
prob = self.counter[token_set] / self.counter[context]
if prob > max_prob:
max_prob = prob
next_token = token
return next_token
def predict_sequence(self, context: tuple, max_length: int = 50) -> list[str]:
assert self.counter != None
tokens = list(context)
max_length -= len(context)
for _ in range(max_length):
next_token = self.predict(tuple(tokens[-self.n + 1 : ]))
tokens.append(next_token)
return tokens
class SmoothedNGram(NGram):
"""
N-gram language model with add-k smoothing.
"""
def __init__(self, n: int, vocab_set: set) -> None:
super().__init__(n, vocab_set)
self.V = len(vocab_set)
def get_log_prob(self, token_set: tuple, k: float = 0) -> float:
assert self.counter != None
subset = tuple(token_set[:-1])
if token_set in self.counter:
return math.log2((self.counter[token_set] + k) / (self.counter[subset] + k * self.V))
else:
subset_count = 0
if subset in self.counter:
subset_count = self.counter[subset]
return math.log2(k / (subset_count + k * self.V))
# Override
def get_perplexity(self, test_lines: list[str], k: float) -> float:
assert self.counter != None
log_sum = 0
N = 0
for line in test_lines:
tokens = self.tokenize(line)
N += len(tokens)
for i in range(len(tokens) - self.n + 1):
token_set = tuple(tokens[i : i + self.n])
log_sum += self.get_log_prob(token_set, k)
return pow(2, (-1 / N) * log_sum)