def insertVowels(queryWords, bigramCost, possibleFills): problem = VowelInsertionProblem(queryWords, bigramCost, possibleFills) goalNode = util.uniformCostSearch(problem) finalState = goalNode.state solutionArray = [i for i in finalState[1:]] solution = ' '.join(solutionArray) return solution
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) if not queryWords: return '' query = '#'.join(queryWords) vowProb = VowelInsertionProblem(query, bigramCost, possibleFills) vowSol = util.uniformCostSearch(vowProb) steps = [] while vowSol.parent is not None: new_n = vowSol.parent if vowSol.state != vowProb.nextState(new_n.state, vowSol.action): return (False, steps) steps.append(vowSol.action) vowSol = new_n if vowSol is not None and vowSol.action is not None: steps.append(vowSol.action) steps = list(reversed(steps)) condStr = True for i in steps: solut = i.split('#') if condStr: vowStr = solut[2] condStr = False else: vowStr = vowStr + ' ' + solut[2] return vowStr
def insertVowels(queryWords, bigramCost, possibleFills): # BEGIN_YOUR_CODE ''' Implementação da função que faz a inserção de vogais. Nessa implementação, o node.state é uma lista de strings que não possuem vogais, a não ser pelo primeiro elemento da lista, este, representa a ultima palavra que foi escolhida, já com as vogais. Além disso, o node.action representa a palavra que foi escolhida naquele passo. Logo, temos que initial_node.state == (<BEGIN> + query words) e initial_node.action = "", além disso, temos que goal_node.state == goal_node.action == <ultima palavra gerada>. Assim, temos: state => action and new_state e para calcular o custo, basta pegarmos bigramCost(state[0], action). ''' insert_problem = VowelInsertionProblem(queryWords, bigramCost, possibleFills) goal_node = util.uniformCostSearch(insert_problem) if (goal_node != None): is_valid, solution = util.getSolution(goal_node, insert_problem) if (is_valid): return solution else: print("Could not recover solution for the insert vowels problem") return None else: print("Could not complete search for the insert vowels problem") return None
def segmentWords(query, unigramCost): if len(query) == 0: return '' problem = SegmentationProblem(query, unigramCost) goal_node = util.uniformCostSearch(problem) valid, solution = util.getSolution(goal_node, problem) print('Query:{}, Solução:{} valida?{} goal_node:{}'.format( query, solution, valid, goal_node.state)) return goal_node.state
def insertVowels(queryWords, bigramCost, possibleFills): for i in range (len(queryWords)): queryWords[i] = queryWords[i].lower() vp = VowelInsertionProblem(queryWords, bigramCost, possibleFills) ret = util.uniformCostSearch(vp) if(ret): return ret.state else: return ''
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 problem = VowelInsertionProblem(queryWords, bigramCost, possibleFills) goalNode = util.uniformCostSearch(problem) valid, solution = util.getSolution(goalNode, problem) if valid: return goalNode.state
def segmentWords(query, unigramCost): if len(query) == 0: return '' problem = SegmentationProblem(query, unigramCost) goalNode = util.uniformCostSearch(problem) solution = ' '.join(goalNode.state[:-1]) return solution
def segmentWords(query, unigramCost): if len(query) == 0: return '' query = query.lower() sp = SegmentationProblem(query, unigramCost) ret = util.uniformCostSearch(sp) if(ret): return ret.state else: return ''
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) prob = VowelInsertionProblem(queryWords, bigramCost, possibleFills) goal = util.uniformCostSearch(prob) if goal == None: saida = ' '.join(queryWords) else: saida = prob.devolveString(goal.state) return saida
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) goal = util.uniformCostSearch(problem) valid, solution = util.getSolution(goal, problem) if valid: return goal.state return result
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
def segmentWords(query, unigramCost): if len(query) == 0: return '' # 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) segProb = SegmentationProblem(query, unigramCost) segSol = util.uniformCostSearch(segProb) segBool, segStr = util.getSolution(segSol, segProb) return segStr + " " + segSol.state
def segmentWords(query, unigramCost): #unigramCost é uma funcao de custo if len(query) == 0: return '' # 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) problem = SegmentationProblem(query, unigramCost) goal = util.uniformCostSearch(problem) return goal.state[0][1:]
def segmentWords(query, unigramCost): if len(query) == 0: return '' # BEGIN_YOUR_CODE # Voce pode usar a função getSolution para recuperar a sua solução # a partir do no meta problem = SegmentationProblem(query, unigramCost) goalNode = util.uniformCostSearch(problem) valid, solution = util.getSolution(goalNode, problem) if valid: result = goalNode.state return solution
def insertVowels(queryWords, bigramCost, possibleFills): # BEGIN_YOUR_CODE # não precisei usar o getSolution pela maneira que os estados foram modelados insVow = VowelInsertionProblem (queryWords, bigramCost, possibleFills) solution = util.uniformCostSearch (insVow) solution = solution.state # tira o prefixo do estado prefix = 0 while (solution[prefix] != "_"): prefix += 1 solution = solution[prefix + 2:] return solution
def segmentWords(query, unigramCost): if len(query) == 0: return '' # BEGIN_YOUR_CODE segProb = SegmentationProblem (query, unigramCost) solution = util.uniformCostSearch (segProb) solution = solution.state # tira o prefixo do estado prefix = 0 while (solution[prefix] != "_"): prefix += 1 solution = solution[prefix + 2:] return solution
def segmentWords(query, unigramCost): if len(query) == 0: return '' # 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) problem = SegmentationProblem(query, unigramCost) goal = util.uniformCostSearch(problem) valid, solution = util.getSolution(goal, problem) words = [] if valid: actions = [0] + [int(a) for a in solution.strip(' ').split(' ')] for i in range(0, len(actions) - 1): word = query[actions[i]:actions[i + 1]] words.append(word) return ' '.join(words) return None
def segmentWords(query, unigramCost): if len(query) == 0: return '' # BEGIN_YOUR_CODE ''' Implementação da função que faz a seguimentação de palavras. Nessa implementação, o campo node.action vai conter a ultima palavra originada, e o campo node.state representa o restante da string, parte que ainda não foi dividida. Logo, temos que initial_node.action == "" e que initial_node.state == query, além disso, temos que goal_node.state == "" e que goal_node.action == <utlima palavra da frase gerada>. Ademais, nossa transição de estado é feita colocando um espaço em todas as posições possiveis dentro da string que representa a estado, e a partir disso, criamos a divisão: <state> => <action> + <new_state> onde, para calcularmos o custo, basta pegarmos unigramCost(action). ''' segmentation_problem = SegmentationProblem(query, unigramCost) goal_node = util.uniformCostSearch(segmentation_problem) if (goal_node != None): is_valid, solution = util.getSolution(goal_node, segmentation_problem) if (is_valid): return solution else: print("Could not recover solution for the segmentation problem") return None else: print("Could not complete search for the segmentation problem") return None
def segmentWords(query, unigramCost): if len(query) == 0: return '' # BEGIN_YOUR_CODE # Voce pode usar a funo getSolution para recuperar a sua soluo a partir do no meta # valid,solution = util.getSolution(goalNode,problem) prob = SegmentationProblem(query, unigramCost) goal = util.uniformCostSearch(prob) #separa as palavras como o isGoalState usando o estado meta listaPalavras = [] aux = "" copQuery = query for i in range(len(goal.state)): j = i + 1 aux = copQuery[goal.state[-j]:] listaPalavras.append(aux) copQuery = copQuery[:goal.state[-j]] listaPalavras.append(copQuery) #Como a lista contem as palavras em ordem reversa, a inverte para montar a string de saida listaPalavras.reverse() #novamente, remove os espacos vazios i = 0 length = len(listaPalavras) while (i < length): if listaPalavras[i] == '': listaPalavras.remove(listaPalavras[i]) length = length - 1 else: i = i + 1 aux = "" #adiciona na saida cada palavra e um espaço for palavra in listaPalavras: aux += palavra aux += " " return aux[:-1] #retorna string de saida sem o espaço final desnecessário
def segmentWords(query, unigramCost): if len(query) == 0: return '' # 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) problem = SegmentationProblem(query, unigramCost) goalNode = util.uniformCostSearch(problem) valid, solution = util.getSolution(goalNode, problem) if valid: solution = solution.split(' ') answer = "" prev = 0 for index in solution: cut = int(index) answer += query[prev:cut] + " " prev = cut return answer[:-1] return None