def _getMatchingSets( ctx, query_literals ): eq = defaultdict( list ) for i, lit in enumerate( query_literals ): for j, term in enumerate( lit[1] ): eq[term] += ["p%d.arg%d" % (1+i, j+1)] def _pairwise_eq(x): return " AND ".join( ["%s = %s" % (x[i], x[i+1]) for i in xrange(len(x)-1)] ) query = "SELECT * FROM %s WHERE %s" % ( ", ".join( ["pehypothesis AS p%d" % (1+i) for i in xrange( len(query_literals) )] ), " AND ".join( ["p%d.predicate = '%s'" % (1+i, query_literals[i][0]) for i in xrange( len(query_literals) )] + [_pairwise_eq(x) for x in eq.values() if 1 < len(x)] ) ) #print >>sys.stderr, query eq = defaultdict(set) inst = henryext.getPotentialElementalHypotheses( ctx, query ) for literals in inst: for i, lit in enumerate( query_literals ): for j, term in enumerate( lit[1] ): eq[ query_literals[i][1][j] ] |= set( [literals[ i*(MaxBasicProp+MaxArguments) + MaxBasicProp+j ]] ) eq = dict( [(x, list(y)) for x, y in eq.iteritems()] ) return (eq, inst)
def cbGetUnificationEvidence(ti, tj, v2h): # print "hello", abc # for p1 in # print p1 # Different constants cannot be unified. if ti != tj and ti[0].isupper() and tj[0].isupper(): return [] ret = [] vret = [] for argpos in xrange(1, 7): # if 1 == argpos: # #for pp in g_disj.keys(): # for literals in henryext.getPotentialElementalHypotheses( "SELECT p1.*, p2.* FROM pehypothesis AS p1, pehypothesis AS p2 WHERE (%s) AND p1.predicate != p2.predicate AND (%s)" % (" OR ".join([ "(p1.predicate = '%s' OR p2.predicate = '%s')" % (x.split()[0][:-2], x.split()[1][:-2]) for x in g_disj.keys()]), "(p1.arg%d = '%s' AND p2.arg%d = '%s')" % (argpos,ti,argpos,tj) ) ): # pass #print literals for literals in henryext.getPotentialElementalHypotheses( "SELECT p1.*, p2.* FROM pehypothesis AS p1, pehypothesis AS p2 WHERE (%s)" % ("(p1.arg%d = '%s' AND p2.arg%d = '%s')" % (argpos, ti, argpos, tj)) ): p, q = literals[:8], literals[8:] pid, qid = int(p[0]), int(q[0]) if pid == qid or (ti == tj and pid > qid): continue # Evidence provided by the same predicate if p[1] == q[1]: ret += [(-0.1, "%s:%d" % (p[1], argpos), [pid, qid])] # Disjoint? if g_disj.has_key("%s/1\t%s/1" % (p[1], q[1])) or g_disj.has_key("%s/1\t%s/1" % (q[1], p[1])): ret += [(-9999, "%s,%s" % (p[1], q[1]), [pid, qid])] # print ti, tj, ret return ret