def segmentWords(query, unigramCost): if len(query) == 0: return '' ucs = util.UniformCostSearch(verbose=0) ucs.solve(SegmentationProblem(query, unigramCost)) # (Retorna a juncao das palavras guarda em acoes, caso tenha alguma palavra) if ( len(ucs.actions) > 0 ): return ' '.join(ucs.actions) else: return query
def insertVowels(queryWords, bigramCost, possibleFills): # BEGIN_YOUR_CODE (our solution is 3 lines of code, but don't worry if you deviate from this) if len(queryWords) == 0: return '' else: queryWords.insert(0, wordsegUtil.SENTENCE_BEGIN) ucs = util.UniformCostSearch(verbose=1) ucs.solve(VowelInsertionProblem(queryWords, bigramCost, possibleFills)) words = ' '.join(ucs.actions) return words
def segmentWords(query, unigramCost): if len(query) == 0: return '' ucs = util.UniformCostSearch(verbose=0) ucs.solve(SegmentationProblem(query, unigramCost)) # BEGIN_YOUR_CODE (our solution is 3 lines of code, but don't worry if you deviate from this) words = ' '.join(ucs.actions) return words
def segmentAndInsert(query, bigramCost, possibleFills): if len(query) == 0: return '' # BEGIN_YOUR_CODE (our solution is 4 lines of code, but don't worry if you deviate from this) ucs = util.UniformCostSearch(verbose=1) ucs.solve( JointSegmentationInsertionProblem(query, bigramCost, possibleFills)) words = ' '.join(ucs.actions) return words
def segmentWords(query, unigramCost): if len(query) == 0: return '' ucs = util.UniformCostSearch(verbose=0) ucs.solve(SegmentationProblem(query, unigramCost)) if (len(ucs.actions) > 0): return ' '.join(ucs.actions) else: return query
def insertVowels(queryWords, bigramCost, possibleFills): # BEGIN_YOUR_CODE (our solution is 3 lines of code, but don't worry if you deviate from this) if len(queryWords) == 0: return '' ucs = util.UniformCostSearch(verbose=0) ucs.solve(VowelInsertionProblem(queryWords, bigramCost, possibleFills)) #setence = [] #for word in ucs.actions: # setence.append(word) #return ' '.join(setence) return ' '.join(ucs.actions)
def insertVowels(queryWords, bigramCost, possibleFills): # BEGIN_YOUR_CODE (our solution is 3 lines of code, but don't worry if you deviate from this) if len(queryWords) == 0: return "" ucs = util.UniformCostSearch(verbose = 1) ucs.solve(VowelInsertionProblem(queryWords, bigramCost, possibleFills)) if ucs.actions != None: return " ".join(ucs.actions) else: return " ".join(queryWords)
def segmentWords(query, unigramCost): if len(query) == 0: return '' ucs = util.UniformCostSearch(verbose=0) ucs.solve(SegmentationProblem(query, unigramCost)) # BEGIN_YOUR_CODE (solucao em 3 linhas de codigo, mas utilize quantas linhas julgar necessario) if(len(ucs.actions) > 0): return ' '.join(ucs.actions) else: return query
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
def segmentAndInsert(query, bigramCost, possibleFills): if len(query) == 0: return '' # BEGIN_YOUR_CODE (solucao em 4 linhas de codigo, mas utilize quantas linhas julgar necessario) prob = JointSegmentationInsertionProblem(query,bigramCost,possibleFills) ucs = util.UniformCostSearch(verbose=0) ucs.solve(prob) results = [] for word in ucs.actions: results.append(word) return ' '.join(results)
def testNonTrivialHeuristic(): # Make sure putting a non-trivial heuristic in does not change the solution. problem = util.trivialProblem heuristic = util.trivialProblemHeuristic ucs = util.UniformCostSearch() astar = submission.AStarSearch(heuristic) ucs.solve(problem) astar.solve(problem) if ucs.actions == astar.actions and ucs.totalCost == astar.totalCost: grader.assignFullCredit() else: grader.fail("Reduced problem has different solution!")
def segmentWords(query, unigramCost): if len(query) == 0: return '' ucs = util.UniformCostSearch(verbose=0) ucs.solve(SegmentationProblem(query, unigramCost)) # BEGIN_YOUR_CODE (around 3 lines of code expected) if (len(ucs.actions) > 0): return ' '.join(ucs.actions) else: return query
def segmentAndInsert(query, bigramCost, possibleFills): if len(query) == 0: return '' # BEGIN_YOUR_CODE (around 5 lines of code expected) ucs = util.UniformCostSearch(verbose=0) ucs.solve( JointSegmentationInsertionProblem(query, bigramCost, possibleFills)) if (ucs.actions is not None): return ' '.join(ucs.actions) else: return ''
def segmentAndInsert(query, bigramCost, possibleFills): if len(query) == 0: return '' # BEGIN_YOUR_CODE (our solution is 4 lines of code, but don't worry if you deviate from this) #raise Exception("Not implemented yet") print("Query is " + str(query)) ucs = util.UniformCostSearch(verbose=0) ucs.solve( JointSegmentationInsertionProblem(query, bigramCost, possibleFills)) print("Result is " + str(ucs.actions)) return ' '.join(ucs.actions)
def segmentAndInsert(query, bigramCost, possibleFills): if len(query) == 0: return '' # BEGIN_YOUR_CODE (around 5 lines of code expected) problem = JointSegmentationInsertionProblem(query, bigramCost, possibleFills) ucs = util.UniformCostSearch(verbose=0) ucs.solve(problem) result = [] for word in ucs.actions: result.append(word) return ' '.join(result)
def segmentAndInsert(query, bigramCost, possibleFills): if len(query) == 0: return '' # BEGIN_YOUR_CODE (around 5 lines of code expected) ucs = util.UniformCostSearch(verbose=0) ucs.solve( JointSegmentationInsertionProblem(query, bigramCost, possibleFills)) print "segmentandinsert" print "here are the results:" print ucs.actions return ' '.join(ucs.actions)
def solve(self, problem): # Reduce the |problem| to |newProblem|, which is solved by UCS. newProblem = astarReduction(problem, self.heuristic) algorithm = util.UniformCostSearch() algorithm.solve(newProblem) # Copy solution back self.actions = algorithm.actions if algorithm.totalCost != None: self.totalCost = algorithm.totalCost + self.heuristic( problem.startState()) else: self.totalCost = None self.numStatesExplored = algorithm.numStatesExplored
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
def segmentWords(query, unigramCost): if len(query) == 0: return '' ucs = util.UniformCostSearch(verbose=0) ucs.solve(SegmentationProblem(query, unigramCost)) # BEGIN_YOUR_CODE (our solution is 10 lines of code, but don't worry if you deviate from this) actions = ucs.actions actions.insert(0, -1) words = [] for i in range(len(actions) - 1): words.append(query[actions[i] + 1:actions[i + 1] + 1]) return ' '.join(words)
def insertVowels(queryWords, bigramCost, possibleFills): # BEGIN_YOUR_CODE (our solution is 3 lines of code, but don't worry if you deviate from this) # raise Exception("Not implemented yet") if len(queryWords) == 0: return '' ucs = util.UniformCostSearch(verbose=0) ucs.solve(VowelInsertionProblem(queryWords, bigramCost, possibleFills)) if len(ucs.actions) > 0: #print 'Problem 2-b: ', ' '.join(ucs.actions) return ' '.join(ucs.actions) else: return ''
def segmentWords(query, unigramCost): if len(query) == 0: return '' ucs = util.UniformCostSearch(verbose=0) ucs.solve(SegmentationProblem(query, unigramCost)) # BEGIN_YOUR_CODE (our solution is 3 lines of code, but don't worry if you deviate from this) words = [] state = 0 for i in ucs.actions: words.append(query[state:(state + i)]) state = state + i return ' '.join(words)
def segmentAndInsert(query, bigramCost, possibleFills): if len(query) == 0: return '' # BEGIN_YOUR_CODE (solucao em 4 linhas de codigo, mas utilize quantas linhas julgar necessario) ucs = util.UniformCostSearch(verbose=0) ucs.solve( JointSegmentationInsertionProblem(query, bigramCost, possibleFills)) if (ucs.actions is not None): return ' '.join(ucs.actions) else: return '' raise Exception("Not implemented yet")
def segmentWords(query, unigramCost): if len(query) == 0: return '' ucs = util.UniformCostSearch(verbose=0) ucs.solve(SegmentationProblem(query, unigramCost)) # BEGIN_YOUR_CODE (our solution is 3 lines of code, but don't worry if you deviate from this) # raise Exception("Not implemented yet") if len(ucs.actions) > 0: #print 'Problem 1-b: ', ' '.join(ucs.actions) return ' '.join(ucs.actions) else: return ''
def segmentWords(query, unigramCost): if len(query) == 0: return '' ucs = util.UniformCostSearch(verbose=0) ucs.solve(SegmentationProblem(query, unigramCost)) # BEGIN_YOUR_CODE (around 5 lines of code expected) state = 0 words = [] for i in ucs.actions: word = query[state:state + i] state = state + i words.append(word) return ' '.join(words)
def main(): AANode = Node('AA', 0, 0, ['AB', 'BA']) #AB->BC is possible! ABNode = Node('AB', 1, 0, ['AA', 'BB','AC','BC']) ACNode = Node('AC', 2, 0, ['AB', 'BC']) BANode = Node('BA', 0, 1, ['AA', 'BB','CA']) BBNode = Node('BB', 1, 1, ['AB', 'BA','CB','BC']) BCNode = Node('BC', 2, 1, ['AC', 'CC','BB']) CANode = Node('CA', 0, 2, ['BA', 'CB']) CBNode = Node('CB', 1, 2, ['CA', 'BB','CC']) CCNode = Node('CC', 2, 2, ['BC', 'CB']) NodeMap = {'AA':AANode, 'AB':ABNode, 'AC':ACNode, 'BA':BANode, 'BB':BBNode, 'BC':BCNode, 'CA':CANode, 'CB':CBNode, 'CC':CCNode} ucs = util.UniformCostSearch(verbose=0) ucs.solve(PathProblem('AA','CC',NodeMap)) actions = ucs.actions print actions
def insertVowels(queryWords, bigramCost, possibleFills): # BEGIN_YOUR_CODE (around 5 lines of code expected) if len(queryWords) == 0: return '' potential_single_word_input = possibleFills(queryWords[0]) if len(queryWords) == 1 and not potential_single_word_input: return queryWords[0] else: ucs = util.UniformCostSearch(verbose=0) ucs.solve(VowelInsertionProblem(queryWords, bigramCost, possibleFills)) print "here are the results:" print ucs.actions return ' '.join(ucs.actions)
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
def main(): inputFile = open('traffic_result.csv', 'r') NodeMap = {} result = {} crimemap = processTrain() for line in inputFile.readlines(): if line[0] == ',': continue s1 = line.split('|')[0].split(',') sKey = (s1[1], s1[2]) s2 = line.split('|')[-1].split(',') latLng = (float(s2[-2]), float(s2[-1].replace('\n', ''))) #print latLng adjacentList = [] for x, y in zip(*[iter(s2[0:-2])] * 2): x = x.replace('"', '') y = y.replace('"', '') adjacentList.append((x.strip(), y.strip())) crimeOccurrence = crimesAt(latLng[0], latLng[1], 0.00325016538, 0.00325016538, crimemap) NodeMap[sKey] = Node(sKey, latLng, adjacentList, crimeOccurrencd) #print NodeMap #AANode = Node('AA', (0, 0), ['AB', 'BA']) #AB->BC is possible! #ABNode = Node('AB', (1, 0), ['AA', 'BB','AC','BC']) #ACNode = Node('AC', (2, 0), ['AB', 'BC']) #BANode = Node('BA', (0, 1), ['AA', 'BB','CA']) #BBNode = Node('BB', (1, 1), ['AB', 'BA','CB','BC']) #BCNode = Node('BC', (2, 1), ['AC', 'CC','BB']) #CANode = Node('CA', (0, 2), ['BA', 'CB']) #CBNode = Node('CB', (1, 2), ['CA', 'BB','CC']) #CCNode = Node('CC', (2, 2), ['BC', 'CB']) #NodeMap = {'AA':AANode, 'AB':ABNode, 'AC':ACNode, 'BA':BANode, 'BB':BBNode, 'BC':BCNode, 'CA':CANode, 'CB':CBNode, 'CC':CCNode} ucs = util.UniformCostSearch(verbose=0) ucs.solve( PathProblem(('EDDY ST', 'POLK ST'), ('GEARY ST', 'MASON ST'), NodeMap)) actions = ucs.actions print actions
def InsertSentences(sentences, para, cost, costData): if len(para) == 0: return '' ucs = util.UniformCostSearch(verbose=0) sentences.append(None) ucs.solve(InsertionProblem(sentences, para, cost, costData)) newpara=[] start=0 newpara.append(str(para[0])) for e in ucs.actions: if e==None: start+=1 newpara.append(para[start]) else: newpara.append(e) lastindex=para.index(newpara[-1]) if lastindex!=len(para)-1: newpara=newpara+para[lastindex+1:] return [str(v) for v in newpara]
def segmentWords(query, unigramCost): if len(query) == 0: return '' ucs = util.UniformCostSearch(verbose=0) ucs.solve(SegmentationProblem(query, unigramCost)) # BEGIN_YOUR_CODE (solucao em 3 linhas de codigo, mas utilize quantas linhas julgar necessario) #aqui eu pego cada palavra e jogo em uma variavel auxiliar, depois jogo na lista de palavras para ficar um vetor , e depois formulo a frase state = 0 words = [] for i in ucs.actions: word = query[state:state + i] state = state + i words.append(word) return ' '.join(words)