-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsegmentation.py
192 lines (177 loc) · 6.34 KB
/
segmentation.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
from math import log
import random
from helper_classes import Lexicon, Phonemes
class Segmentation:
def __init__(self):
self.lexicon = Lexicon()
self.phonemes = Phonemes()
pass
def read_corpus(self, corpus):
with open(corpus, mode="r") as f:
utterances = f.read().splitlines()
random.shuffle(utterances)
self.start(utterances)
print(f"read {len(utterances)} lines")
def start(self, utterances):
for utterance in utterances:
print("Trying", utterance)
bestScore, bestUtterance = self.evalUtterance(utterance)
print(f"\nFor #{utterance}#, the segmentation is #{bestUtterance}# with a score of {bestScore} \n")
# print(self.lexicon.words_freq)
print("Finished going through the corpus!")
def evalUtterance(self, u):
self.store = dict()
bestScore, bestUtterance = self._evalUtterance(u)
self.store.clear()
self.update_lexicon(bestUtterance)
self.update_phonemes(bestUtterance)
return bestScore, bestUtterance
def __evalUtterance(self, u):
if u in self.store:
return self.store[u]
print("evaluating for :", u)
# print()
# if len(u) == 0:
# return float('inf'), ""
n = len(u) - 1
print(n)
bestSegPoint = n
bestScore = self.evalWord(u)
print(f"initial best score: {bestScore}")
bestUtterance = u
for i in range(n): # finding the segmenttion with smallest score
subUtterance = u[:i + 1]
word = u[i + 1:]
prev_score, prev_subUtterance = self._evalUtterance(subUtterance)
score = prev_score + self.evalWord(word)
print(f"Trying {subUtterance}//{word}")
print(score)
if score < bestScore:
# print("UAU")
bestScore = score
bestSegPoint = i
bestUtterance = prev_subUtterance + "#" + word # inserting word boundary
# new_utterance =
self.store[u] = bestScore, bestUtterance
# print(self.store)
return bestScore, bestUtterance
def _evalUtterance(self, u):
n = len(u) # u is from 0 to n
bestCost = [0 for _ in range(n)] # from 0 to n
previousBoundary = [None for _ in range(n)] # from 0 to n
if n == 0:
return 0, u
for i in range(n):
bestCost[i] = self.evalWord(u[:i + 1])
previousBoundary[i] = -1
for j in range(i + 1):
# prevWord = u[previousBoundary[j]+1:j+1]
# if prevWord[j] >= 0:
cost = bestCost[j] + self.evalWord(u[j + 1:i + 1])
if cost < bestCost[i]:
bestCost[i] = cost
previousBoundary[i] = j
i = n - 1
print("i", i)
final_utterance = ""
all_stops = []
curr = n - 1
last = None
print(previousBoundary)
if any(val != -1 for val in previousBoundary):
raise ValueError("yay!")
# print(bestCost)
while True:
last = previousBoundary[curr]
if last < 0: break
final_utterance = u[last:curr + 1] + "#" + final_utterance
curr = last
# final_utterance =
# all_stops.append(i)
# i = previousBoundary[i]
# print(all_stops)
# # assert max(previousBoundary) == n, previousBoundary
# for i in all_stops:
# print(i)
# final_utterance = final_utterance + u[i] + "#"
# final_utterance = final_utterance[:-1]
return bestCost[n - 1], final_utterance
def evalWord(self, w):
# TODO: implement bigrams/trigrams
r = self.lexicon.size()
n = self.lexicon.sumFrequencies()
f = self.lexicon.frequency(w)
cost = 0
# Unigrams
if f != 0 and r != 0:
cost += -log(f / (n + r))
elif r != 0:
cost += -log(r / n + r)
# Phonemes
P_0 = self.phonemes.relativeFrequency("#")
prob = P_0 / (1 - P_0)
for i in range(len(w)):
if w[i] != " ":
prob *= self.phonemes.relativeFrequency(w[i]) # TODO: in paper/official code they do it as w[i].phon()
cost += -log(prob)
return cost
# if self.lexicon.frequency(w) == 0: #unseen
# if self.lexicon.size() + self.lexicon.sumFrequencies() == 0: #first utterance
# print("empty")
# return float('inf')
# escape = self.lexicon.size() / (self.lexicon.size() + self.lexicon.sumFrequencies())
# P_0 = self.phonemes.relativeFrequency("#")
# score = -log(escape) -log(P_0/(1-P_0))
# for i in range(len(w)):
# if w[i] != " ":
# score -= log(self.phonemes.relativeFrequency(w[i]))
# else: # already seen
# P_w = self.lexicon.frequency(w) / (self.lexicon.size() + self.lexicon.sumFrequencies())
# score = -log(P_w)
# return score
def update_lexicon(self, new_utterance):
all_words = new_utterance.split("#")
for word in all_words:
self.lexicon.update_word(word)
return
def update_phonemes(self, new_utterance):
for char in new_utterance:
if char != " ":
self.phonemes.update_phoneme(char)
# def insertWordBoundary(self, u, bestSegPoint):
if __name__ == "__main__":
seg = Segmentation()
seg.read_corpus("./corpus")
utterances = [
"yu l9k D6 blaks",
"se blaks",
"WAts DIs &lIs",
"6 blak 6 blak",
"lUk &t D&t dali",
"WAt du yu TINk 6v D&t dali",
"&lIs kAm h(",
"6 bEtR wAn",
"wAn D&t wIl fl&p",
"In D6 briz",
"kAm h( 9 want yu tu sIt Ap h( f% GAst 6",
"mInIt",
"9 want tu So yu sAmTIN",
"lUk",
"du yu rImEmbR DIs",
"h(z 6 pApIt",
"hElo &lIs",
"hQ # yu",
"wan6 pEt mi",
"yu dont l9k It",
"oke",
"kOl d&di an D6 tEl6fon",
"yu du D&t",
"D&ts r9t",
"nQ pIk Ap D6 tEl6fon &nd se hElo",
"hElo d&di",
"y&",
"gUd",
"WAts D&t",
"D&ts 6 lItL t7 dali"
]
# seg.start(utterances)