-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathep1.py
executable file
·230 lines (186 loc) · 9.45 KB
/
ep1.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
"""
AO PREENCHER ESSE CABECALHO COM O MEU NOME E O MEU NUMERO USP,
DECLARO QUE SOU A UNICA PESSOA AUTORA E RESPONSAVEL POR ESSE PROGRAMA.
TODAS AS PARTES ORIGINAIS DESSE EXERCICIO PROGRAMA (EP) FORAM
DESENVOLVIDAS E IMPLEMENTADAS POR MIM SEGUINDO AS INSTRUCOES
DESSE EP E, PORTANTO, NAO CONSTITUEM ATO DE DESONESTIDADE ACADEMICA,
FALTA DE ETICA OU PLAGIO.
DECLARO TAMBEM QUE SOU A PESSOA RESPONSAVEL POR TODAS AS COPIAS
DESSE PROGRAMA E QUE NAO DISTRIBUI OU FACILITEI A
SUA DISTRIBUICAO. ESTOU CIENTE QUE OS CASOS DE PLAGIO E
DESONESTIDADE ACADEMICA SERAO TRATADOS SEGUNDO OS CRITERIOS
DIVULGADOS NA PAGINA DA DISCIPLINA.
ENTENDO QUE EPS SEM ASSINATURA NAO SERAO CORRIGIDOS E,
AINDA ASSIM, PODERAO SER PUNIDOS POR DESONESTIDADE ACADEMICA.
Nome : Arnaldo Alves Viana Junior
NUSP : 11596396
Referencias: Com excecao das rotinas fornecidas no enunciado
e em sala de aula, caso voce tenha utilizado alguma referencia,
liste-as abaixo para que o seu programa nao seja considerado
plagio ou irregular.
Exemplo:
- O algoritmo Quicksort foi baseado em:
https://pt.wikipedia.org/wiki/Quicksort
http://www.ime.usp.br/~pf/algoritmos/aulas/quick.html
Referencias:
https://jeremykun.com/2012/01/15/word-segmentation/
https://web.stanford.edu/class/archive/cs/cs221/cs221.1196/assignments/reconstruct/index.html
http://inst.eecs.berkeley.edu/~cs188/sp20/
"""
import util
############################################################
# Part 1: Segmentation problem under a unigram model
class SegmentationProblem(util.Problem):
def __init__(self, query, unigramCost):
self.query = query
self.unigramCost = unigramCost
def isState(self, state):
""" Metodo que implementa verificacao de estado """
#raise NotImplementedError
if (state == len(self.query)):
return True
else:
return False
def initialState(self):
""" Metodo que implementa retorno da posicao inicial """
#raise NotImplementedError
return 0
def actions(self, state):
""" Metodo que implementa retorno da lista de acoes validas
para um determinado estado
"""
# raise NotImplementedError
word = self.query[state:]
action = [word[:i] for i in range(1,len(word) + 1)]
#print("action", action)
return action
def nextState(self, state, action):
""" Metodo que implementa funcao de transicao """
#raise NotImplementedError
nextstate = state + len(action)
return nextstate
def isGoalState(self, state):
""" Metodo que implementa teste de meta """
#raise NotImplementedError
if (state == len(self.query)):
return True
else:
return False
def stepCost(self, state, action):
""" Metodo que implementa funcao custo """
#raise NotImplementedError
stepcost = self.unigramCost(action)
if state != self.initialState:
return stepcost
def segmentWords(query, unigramCost):
if len(query) == 0:
return ''
else:
# BEGIN_YOUR_CODE
# Voce pode usar a função getSolution para recuperar a sua solução a partir do no meta
# valid,solution = util.getSolution(goalNode,problem)
# raise NotImplementedError
problem = SegmentationProblem(query, unigramCost)
goal = util.uniformCostSearch(problem)
valid, solution = util.getSolution(goal,problem)
return solution
# END_YOUR_CODE
############################################################
# Part 2: Vowel insertion problem under a bigram cost
class VowelInsertionProblem(util.Problem):
def __init__(self, queryWords, bigramCost, possibleFills):
self.queryWords = queryWords
self.bigramCost = bigramCost
self.possibleFills = possibleFills
def isState(self, state):
""" Metodo que implementa verificacao de estado """
# raise NotImplementedError
return
def initialState(self):
""" Metodo que implementa retorno da posicao inicial """
#raise NotImplementedError
self.queryWords.insert(0,util.SENTENCE_BEGIN)
# result = self.queryWords #tem que ser tuple tem dicionario em util
result = tuple(self.queryWords,)
# print("initialState: ",result)
return result
def actions(self, state):
""" Metodo que implementa retorno da lista de acoes validas
para um determinado estado
"""
# raise NotImplementedError
# print("o que é possibleFills:", self.possibleFills)
# print("o que é possibleFills:", self.queryWords)
acao = []
# for i in range(0, len(state)):
# print(state[i])
# possiblefill = self.possibleFills(state[i])
# for j in possiblefill:
# acao.append((j,i))
acao = [(j,i) for i in range(0, len(state)) for j in self.possibleFills(state[i]) ]
print("acoes: ",acao)
return acao
def nextState(self, state, action):
""" Metodo que implementa funcao de transicao """
# raise NotImplementedError
# print("o que é state:", state)
# print("o que é action:", action)
nextstate = action
return nextstate
def isGoalState(self, state):
""" Metodo que implementa teste de meta """
# raise NotImplementedError
# print("action(state): ", self.actions(state))
if self.actions(state) != []:
# print("estadometa: ", False)
return False
# print("estadometa: ", True)
return True
def stepCost(self, state, action):
""" Metodo que implementa funcao custo """
# raise NotImplementedError
cost = 0
for i in range(len(action)-1):
cost += self.bigramCost(action[i], action[i+1])
return cost
def insertVowels(queryWords, bigramCost, possibleFills):
# BEGIN_YOUR_CODE
# Voce pode usar a função getSolution para recuperar a sua solução a partir do no meta
# valid,solution = util.getSolution(goalNode,problem)
# raise NotImplementedError
problem = VowelInsertionProblem(queryWords, bigramCost, possibleFills)
goalNode = util.uniformCostSearch(problem)
valid, solution = util.getSolution(goalNode,problem)
if valid:
return goalNode.state
return result
# END_YOUR_CODE
############################################################
def getRealCosts(corpus='corpus.txt'):
""" Retorna as funcoes de custo unigrama, bigrama e possiveis fills obtidas a partir do corpus."""
_realUnigramCost, _realBigramCost, _possibleFills = None, None, None
if _realUnigramCost is None:
print('Training language cost functions [corpus: '+ corpus+']... ')
_realUnigramCost, _realBigramCost = util.makeLanguageModels(corpus)
_possibleFills = util.makeInverseRemovalDictionary(corpus, 'aeiou')
print('Done!')
return _realUnigramCost, _realBigramCost, _possibleFills
def main():
""" Voce pode/deve editar o main() para testar melhor sua implementacao.
A titulo de exemplo, incluimos apenas algumas chamadas simples para
lhe dar uma ideia de como instanciar e chamar suas funcoes.
Descomente as linhas que julgar conveniente ou crie seus proprios testes.
"""
unigramCost, bigramCost, possibleFills = getRealCosts()
# frase = 'mynameis'
# frase = 'believeinyour'
# frase = 'believeinyourselfhavefaithinyourabilities'
# frase = 'believeinyourselfhavefaithinyourabilitiesbelieveinyourselfhavefaithinyourabilitiesbelieveinyourselfhavefaithinyourabilitiesbelieveinyourselfhavefaithinyourabilitiesbelieveinyourselfhavefaithinyourabilitiesbelieveinyourselfhavefaithinyourabilitiesbelieveinyourselfhavefaithinyourabilitiesbelieveinyourselfhavefaithinyourabilitiesbelieveinyourselfhavefaithinyourabilitiesbelieveinyourselfhavefaithinyourabilitiesbelieveinyourselfhavefaithinyourabilitiesbelieveinyourselfhavefaithinyourabilitiesbelieveinyourselfhavefaithinyourabilitiesbelieveinyourselfhavefaithinyourabilitiesbelieveinyourselfhavefaithinyourabilitiesbelieveinyourselfhavefaithinyourabilitiesbelieveinyourselfhavefaithinyourabilitiesbelieveinyourselfhavefaithinyourabilitiesbelieveinyourselfhavefaithinyourabilitiesbelieveinyourselfhavefaithinyourabilitiesbelieveinyourselfhavefaithinyourabilitiesbelieveinyourselfhavefaithinyourabilitiesbelieveinyourselfhavefaithinyourabilitiesbelieveinyourselfhavefaithinyourabilitiesbelieveinyourselfhavefaithinyourabilitiesbelieveinyourselfhavefaithinyourabilitiesbelieveinyourselfhavefaithinyourabilitiesbelieveinyourselfhavefaithinyourabilitiesbelieveinyourselfhavefaithinyourabilitiesbelieveinyourselfhavefaithinyourabilitiesbelieveinyourselfhavefaithinyourabilitiesbelieveinyourselfhavefaithinyourabilitiesbelieveinyourselfhavefaithinyourabilitiesbelieveinyourselfhavefaithinyourabilitiesbelieveinyourselfhavefaithinyourabilitiesbelieveinyourselfhavefaithinyourabilitiesbelieveinyourselfhavefaithinyourabilitiesbelieveinyourselfhavefaithinyourabilitiesbelieveinyourselfhavefaithinyourabilitiesbelieveinyourselfhavefaithinyourabilitiesbelieveinyourselfhavefaithinyourabilities'
# resulSegment = segmentWords(frase, unigramCost)
# resulSegment = segmentWords('believeinyour', unigramCost)
# print("frase:",frase)
# print("resultado:",resulSegment)
resultInsert = insertVowels('smtms ltr bcms nvr'.split(), bigramCost, possibleFills)
print(resultInsert)
if __name__ == '__main__':
main()