def match_pred(self, p1, p2): if p1["name"] != p2["name"]: return False for arg1, arg2 in zip(p1["args"], p2["args"]): if isVariable(arg1) and isConstant(arg2): return False if isConstant(arg1) and isVariable(arg2): return False if isConstant(arg1) and isConstant(arg2) and arg1 != arg2: return False return True
def simpler_unify (predicate1, predicate2): if predicate1["name"] != predicate2["name"] or len(predicate1["args"]) != len(predicate2["args"]) or predicate1["truth"] != predicate2["truth"]: return None args1 = predicate1["args"] args2 = predicate2["args"] sub = {} for x, y in zip(args1, args2): if isVariable(x) and isConstant(y): return None if isConstant(x) and isVariable(y): return None if isConstant(x) and x!=y: return None if isVariable(x): sub[x] = y return sub
def unify_all (x, y, sub): if sub is None: return sub if x == y: return sub if isVariable(x): return unify_var (x, y, sub) if isVariable(y): return unify_var (y, x, sub) if type(x)==type([]) and type(y)==type([]): return unify_all ( x[1:], y[1:], unify_all ( x[0], y[0], sub ) ) return None
def isLiteral(self, sentenceNum): sentence = self.sentences[sentenceNum] sentences = sentence.split("|") for currSentence in sentences: predicate, terms=utils.getPredicateAndTerms(currSentence) for term in terms: if (utils.isVariable(term)): return False return True
def get_resolved_sentence(x, y, x_pred, y_pred, sub): x = [xx for xx in x if xx != x_pred] y = [yy for yy in y if yy != y_pred] resolved_sentence = x + y for i, pred in enumerate(resolved_sentence): for j, arg in enumerate(pred["args"]): if isVariable(arg) and arg in sub: resolved_sentence[i]["args"][j] = sub[arg] return resolved_sentence
def standardizeAllVariables(self): sentenceCount=0 for currSentence in self.sentences: predicates= currSentence.split('|') for j in range(len(predicates)): predicate, terms=utils.getPredicateAndTerms(predicates[j]) for i in range(len(terms)): if utils.isVariable(terms[i]): terms[i]=terms[i]+str(sentenceCount) arguments = ",".join(terms) predicates[j]=predicate+"("+arguments+")" sentence=" | ".join(predicates) self.sentences[sentenceCount]=sentence sentenceCount+=1
def get_kb(data): cnfs = [] for s in data: #print s new = get_cnf(s) for cnf in new: cnfs.append(cnf) counter = 0 for i, t in enumerate(cnfs): counter += 1 for k, child in enumerate(t): for j, var in enumerate(child): if isVariable(var): cnfs[i][k][j] = var + str(counter) return cnfs
def findSubstitution(eq_set): # Find if there is a constant or more than one constant currentConstant = None for t in eq_set: if utils.isConstant(t): if currentConstant == None: currentConstant = t else: # More than one constants that are different return {} sub_map = {} leader = currentConstant if leader == None: # Choose first variable as leader leader = eq_set.pop() sub_map[leader] = leader for t in eq_set: if utils.isVariable(t): sub_map[t] = leader return sub_map
def resolve(self, orgPredicate, sentence1, sentence2): predicates1 = sentence1.split("|") predicates2 = sentence2.split("|") variables1=set() variables2=set() unify_term1 = [] unify_term2 = [] structured_1 = [] structured_2 = [] for i in range(len(predicates1)): currPredicate,terms = utils.getPredicateAndTerms(predicates1[i]) structured_1.append(terms) for term in terms: if utils.isVariable(term): variables1.add(term) if (currPredicate == utils.negateQuery(orgPredicate)): unify_term1.append(i) for i in range(len(predicates2)): currPredicate,terms = utils.getPredicateAndTerms(predicates2[i]) structured_2.append(terms) for term in terms: if utils.isVariable(term): variables2.add(term) if (currPredicate == orgPredicate): unify_term2.append(i) # Create variable name such that there is no intersection between # the names used in sentence 1 and 2 intersectionSet=variables1.intersection(variables2) variableNames = variables1.union(variables2) mapToUpdateVariableNames={} for var in intersectionSet: newName = var count=0 while newName in variableNames: newName = "a" + str(count) count+=1 mapToUpdateVariableNames[var] = newName variableNames.add(newName) for term_tuple in structured_2: for i in range(len(term_tuple)): if term_tuple[i] in mapToUpdateVariableNames: term_tuple[i] = mapToUpdateVariableNames[term_tuple[i]] subst_ans = False done_i = -1 done_j = -1 for i in range(len(unify_term1)): for j in range(len(unify_term2)): substitution = self.unify(structured_1[unify_term1[i]], structured_2[unify_term2[j]]) if (substitution): subst_ans = True done_i = i done_j = j break if (not subst_ans): return "CANNOT RESOLVE", True, True resolvent = "" allPredicates=set() predicates1.pop(unify_term1[done_i]) predicates2.pop(unify_term2[done_j]) structured_1.pop(unify_term1[done_i]) structured_2.pop(unify_term2[done_j]) all_const=True # print terms1 # print terms2 uniqueVars=set() for i in range(len(predicates1)): currResolvent="" t = predicates1[i].strip() start = t.index("(") currResolvent = currResolvent + t[:start + 1] #variables = t[start + 1:-1].split(",") variables=structured_1[i] for j in range(len(variables)): v = variables[j] if (v in substitution): currResolvent = currResolvent + substitution[v] if(utils.isVariable(substitution[v])): all_const = False uniqueVars.add(substitution[v]) else: currResolvent = currResolvent + v if utils.isVariable(v): all_const=False uniqueVars.add(v) if (j == len(variables) - 1): currResolvent = currResolvent + ")" else: currResolvent = currResolvent + "," allPredicates.add(currResolvent) if (i != len(predicates1) - 1): resolvent = resolvent+ currResolvent + " | " else: resolvent = resolvent+currResolvent for i in range(len(predicates2)): currResolvent="" if (i == 0 and resolvent != ""): resolvent = resolvent + " | " t = predicates2[i].strip() start = t.index("(") currResolvent = currResolvent + t[:start + 1] #variables = t[start + 1:-1].split(",") variables = structured_2[i] for j in range(len(variables)): v = variables[j] if (v in substitution): currResolvent = currResolvent + substitution[v] if (utils.isVariable(substitution[v])): uniqueVars.add(substitution[v]) else: currResolvent = currResolvent + v if utils.isVariable(v): uniqueVars.add(v) if (j == len(variables) - 1): currResolvent = currResolvent + ")" else: currResolvent = currResolvent + "," allPredicates.add(currResolvent) if (i != len(predicates2) - 1): resolvent = resolvent+currResolvent + " | " else: resolvent = resolvent+currResolvent allPredicates=set(allPredicates) resolvent1="|".join(allPredicates) return resolvent1, len(uniqueVars), all_const