def modify_parse(parse): # modify parsetree modparse = regex_replace_phrases(parse, regex_replacement_phrases) # listify parsetrees lparse = to_list(parse) lmodparse = to_list(modparse) # extract relation and landmark chunks of the parsetree rel_chunk = ['REL'] + lmodparse[1][1:-1] lmk_chunk = ['LMK'] + [lmodparse[1][-1]] rel_words = get_words(rel_chunk) lmk_words = get_words(lmk_chunk) rel_phrases = get_phrases(rel_chunk) lmk_phrases = get_phrases(lmk_chunk) return(rel_words, lmk_words, rel_phrases, lmk_phrases)
counts = {} location = 0.5 countbys = zip( *compute_landmark_posteriors(location) ) for landmark,landmarkname in ((0.1,'beginning'),(0.5,'middle'),(0.9,'end')): reldeg, posterior = compute_rel_posteriors(location,landmark) reldeg = [landmarkname+"-"+rel+""+str(deg) for rel,deg in reldeg] posterior = posterior.reshape((12,)) countbys += zip(reldeg,posterior) for key, trash in countbys: counts[key] = {} for row in reader: location, region, nearfar, precise, phrase = row parse = parse_sentence(phrase)['parsetree'] parse = regex_replace_phrases(parse,regex_replacement_phrases) print phrase print parse listified = listify(parse.encode('utf-8').split()) print listified location = float(location)/100.0 countbys = zip( *compute_landmark_posteriors(location) ) for landmark,landmarkname in ((0.1,'beginning'),(0.5,'middle'),(0.9,'end')): reldeg, posterior = compute_rel_posteriors(location,landmark) reldeg = [landmarkname+"-"+rel+""+str(deg) for rel,deg in reldeg] posterior = posterior.reshape((12,)) countbys += zip(reldeg,posterior) count( listified[0],None,countbys ) print ('-' * 50)+str(rownum)
rel_scores = [] for relation in relations: rel_scores.append( relation.probability(location, sampled_landmark)) rel_scores = array(rel_scores) rel_probabilities = rel_scores / sum(rel_scores) index = rel_probabilities.cumsum().searchsorted( random.sample(1))[0] sampled_relation = relations[index] #lmk_name, lmk_loc = sample_landmark(location/100) #relation, degree = sample_reldeg(location/100, lmk_loc) # parse sentence #parse = parse_sentence(phrase)['parsetree'] # modify parsetree modparse = regex_replace_phrases(parse, regex_replacement_phrases) # listify parsetrees lparse = to_list(parse) lmodparse = to_list(modparse) # extract relation and landmark chunks of the parsetree rel_chunk = lmodparse[1][1:-1] lmk_chunk = lmodparse[1][-1][1:] if args.verbose: print '-' * 70 print 'Phrase: %r' % phrase print 'Location: %s' % location print 'Landmark: %s (%s)' % (int(lmk_loc * 100), lmk_name) print 'Relation: %s' % relation print 'Degree: %s' % degree