-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path003_reconstruct.py
More file actions
executable file
·131 lines (94 loc) · 3.79 KB
/
003_reconstruct.py
File metadata and controls
executable file
·131 lines (94 loc) · 3.79 KB
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
import shell
import util
import wordsegUtil
############################################################
# Problem 1b: Solve the segmentation problem under a unigram model
class SegmentationProblem(util.SearchProblem):
def __init__(self, query, unigramCost):
self.query = query
self.unigramCost = unigramCost
def startState(self):
return self.query
def isGoal(self, state):
return len(state) == 0
def succAndCost(self, state):
result = [] # action, state, cost
split_prefixes = [(state[:i+1], state[i+1:]) for i in range(len(state))]
for prefix_word in split_prefixes:
result.append( (prefix_word[0], prefix_word[1], self.unigramCost( prefix_word[0]) ) )
return( result )
def segmentWords(query, unigramCost):
if len(query) == 0:
return ''
ucs = util.UniformCostSearch(verbose=0)
ucs.solve(SegmentationProblem(query, unigramCost))
answer = query
if( len(ucs.actions) ):
answer = ' '.join( ucs.actions )
return answer
############################################################
# Problem 2b: Solve the vowel insertion problem under a bigram cost
class VowelInsertionProblem(util.SearchProblem):
def __init__(self, queryWords, bigramCost, possibleFills):
self.queryWords = queryWords
self.bigramCost = bigramCost
self.possibleFills = possibleFills
def startState(self):
return ( wordsegUtil.SENTENCE_BEGIN, tuple(self.queryWords) )
def isGoal(self, state):
return len(state[1]) == 0
def succAndCost(self, state):
result = [] # action, state, cost
if( len(state[1]) >= 1):
pfills = self.possibleFills( state[1][0] )
if(len(pfills)==0): pfills = set( [ state[1][0] ] )
for wrd in pfills:
result.append( (wrd, (wrd, tuple(state[1][1:])), self.bigramCost(state[0], wrd)) )
return( result )
def insertVowels(queryWords, bigramCost, possibleFills):
if queryWords is None:
return ''
if len(queryWords) == 0:
return ''
ucs = util.UniformCostSearch(verbose=0)
ucs.solve(VowelInsertionProblem(queryWords, bigramCost, possibleFills))
answer = ' '.join( queryWords )
if ucs.actions is None:
return answer
elif( len(ucs.actions) ):
answer = ' '.join( ucs.actions )
return answer
############################################################
# Problem 3b: Solve the joint segmentation-and-insertion problem
class JointSegmentationInsertionProblem(util.SearchProblem):
def __init__(self, query, bigramCost, possibleFills):
self.query = query
self.bigramCost = bigramCost
self.possibleFills = possibleFills
def startState(self):
return ( wordsegUtil.SENTENCE_BEGIN, self.query )
def isGoal(self, state):
return len(state[1]) == 0
def succAndCost(self, state):
result = [] # action, state, cost
split_prefixes = [(state[1][:i+1], state[1][i+1:]) for i in range(len(state[1]))]
for prefix_word in split_prefixes:
if( len(state[1]) >= 1):
pfills = self.possibleFills( prefix_word[0] )
for wrd in pfills:
result.append( (wrd, (wrd, prefix_word[1]), self.bigramCost(state[0], wrd)) )
return( result )
def segmentAndInsert(query, bigramCost, possibleFills):
if len(query) == 0:
return ''
ucs = util.UniformCostSearch(verbose=0)
ucs.solve(JointSegmentationInsertionProblem(query, bigramCost, possibleFills))
answer = query
if ucs.actions is None:
return answer
elif( len(ucs.actions) ):
answer = ' '.join( ucs.actions )
return answer
############################################################
#if __name__ == '__main__':
# shell.main()