def main(): numQueries = 0 queries = [] numTruths = 0 truths = [] with open("./input.txt") as inp: line = inp.readline() # First line is always the number of queries numQueries = int(line) # n queries for i in range(numQueries): line = inp.readline() queries.append(line.strip()) line = inp.readline() # This is followed by the number of truths numTruths = int(line) for i in range(numTruths): line = inp.readline() truths.append(line.strip()) kb = KnowledgeBase(truths) results = [] for q in queries: lit = Literal(q, len(truths) + 1) lit.negate() result = kb.proveByResolution2([lit], 0) print("{}: {}".format(q, result)) results.append(str(result).upper()) with open('./output.txt', 'w') as op: op.write('\n'.join([r for r in results]))
def getProofByResolution(self, l: Literal): l.negate() # Negate the literal! inputLiterals = [l] literalCopy = deepcopy(self.literalMap) sentenceCopy = deepcopy(self.sentences) alreadySelected = {} while True: # Iterate though all input literals to get the best matching sentence in KB sentenceCountMap = {} bestSentence = -1 for inlit in inputLiterals: key = (inlit.identifier, inlit.litType) locations = literalCopy.get(key, []) for loc in locations: sentence = sentenceCopy[loc] literals = sentence.literals for lit in literals: if (lit.identifier, lit.litType) == key and ( lit.canBeResolvedBy(inlit) or inlit.canBeResolvedBy(lit) ) and lit.negated != inlit.negated: # These can be resolved sentenceCountMap[loc] = sentenceCountMap.get( loc, 0) + 1 if sentenceCountMap[loc] > sentenceCountMap.get( bestSentence, -1): bestSentence = loc break # There is no sentence with at least one of the appropriate literals if bestSentence == -1: return False neededSentence = sentenceCopy[bestSentence] inputLiterals = getResolution(neededSentence, inputLiterals) if len(inputLiterals) == 0: self.sentences.append(Sentence(str(l))) self.literalMap[(l.identifier, l.litType)] = self.literalMap.get( (l.identifier, l.litType), []) + [len(self.sentences) - 1] return True