예제 #1
0
def nbest(obs, kb, maxdepth, n, skolemize=True):
    '''Returns n-best conjunctions of etcetera literals that logically entail the observations'''
    indexed_kb = abduction.index_by_consequent_predicate(kb)
    pr2beat = 0.0
    nbest = []  # solutions
    nbestPr = []  # probabilities
    listoflists = [
        abduction.and_or_leaflists([ob], indexed_kb, maxdepth) for ob in obs
    ]
    for u in itertools.product(*listoflists):
        u = list(itertools.chain.from_iterable(u))
        if bestCaseProbability(u) > pr2beat:
            for solution in abduction.crunch(u):
                jpr = jointProbability(solution)
                if jpr > pr2beat:
                    insertAt = bisect.bisect_left(nbestPr, jpr)
                    nbest.insert(insertAt, solution)
                    nbestPr.insert(insertAt, jpr)
                    if len(nbest) > n:
                        nbest.pop(0)
                        nbestPr.pop(0)
                        pr2beat = nbestPr[0]  # only if full
    nbest.reverse()  # [0] is now highest
    if skolemize:
        return [unify.skolemize(r) for r in nbest]
    else:
        return nbest
예제 #2
0
def cache_kb(fnout, kbkb, kbfacts):
    mk = mcdb.make(fnout)

    for p, rules in abduction.index_by_consequent_predicate(kbkb).iteritems():
        mk.add(p, cPickle.dumps(rules))

    mk.add("__fact__", cPickle.dumps(kbfacts))
    mk.finish()
예제 #3
0
def etcAbduction(obs, kb, maxdepth, skolemize = True):
    '''Trying something faster'''
    indexed_kb = abduction.index_by_consequent_predicate(kb)
    res = []
    listoflists = [abduction.and_or_leaflists([ob], indexed_kb, maxdepth) for ob in obs]
    for u in itertools.product(*listoflists):
        u = list(itertools.chain.from_iterable(u))
        res.extend(abduction.crunch(u))
    res.sort(key=lambda item: jointProbability(item), reverse=True)
    if skolemize:
        return [unify.skolemize(r) for r in res]
    else:
        return res
예제 #4
0
def etcAbduction(obs, kb, maxdepth, skolemize = True):
    '''Exhuastive search for conjunctions of etctera literals that logically entail the observations'''
    indexed_kb = abduction.index_by_consequent_predicate(kb)
    res = []
    listoflists = [abduction.and_or_leaflists([ob], indexed_kb, maxdepth) for ob in obs]
    for u in itertools.product(*listoflists):
        u = list(itertools.chain.from_iterable(u))
        res.extend(abduction.crunch(u))
    res.sort(key=lambda item: jointProbability(item), reverse=True)
    if skolemize:
        return [unify.skolemize(r) for r in res]
    else:
        return res
예제 #5
0
def incremental1(obs, kb, maxdepth, n, w, b, skolemize=True):
    # n = n-best
    # w = window size
    # b = beam of running candidate interpretations
    indexed_kb = abduction.index_by_consequent_predicate(kb)
    previous = []  # proofs of the previous
    while len(obs) > 0:
        window = obs[0:w]
        obs = obs[w:]
        listoflists = [
            abduction.and_or_leaflists([ob], indexed_kb, maxdepth)
            for ob in window
        ]
        if len(previous) > 0:
            listoflists.append(previous)
        pr2beat = 0.0
        nbest = []  # solutions
        nbestPr = []  # probabilities
        for u in itertools.product(*listoflists):
            u = list(itertools.chain.from_iterable(u))
            if etcetera.bestCaseProbability(u) > pr2beat:
                for solution in abduction.crunch(u):
                    jpr = etcetera.jointProbability(solution)
                    if jpr > pr2beat:
                        insertAt = bisect.bisect_left(nbestPr, jpr)
                        nbest.insert(insertAt, solution)
                        nbestPr.insert(insertAt, jpr)
                        if len(nbest) > b:
                            nbest.pop(0)
                            nbestPr.pop(0)
                            pr2beat = nbestPr[0]  # only if full
        nbest.reverse()  # [0] is now highest
        previous = nbest
    if skolemize:
        return [unify.skolemize(r)
                for r in previous[0:n]]  # only skolemize nbest
    else:
        return previous[0:n]
예제 #6
0
def nbest(obs, kb, maxdepth, n, skolemize = True):
    indexed_kb = abduction.index_by_consequent_predicate(kb)
    pr2beat = 0.0
    nbest = [] # solutions
    nbestPr = [] # probabilities
    listoflists = [abduction.and_or_leaflists([ob], indexed_kb, maxdepth) for ob in obs]
    for u in itertools.product(*listoflists):
        u = list(itertools.chain.from_iterable(u))
        if bestCaseProbability(u) > pr2beat:
            for solution in abduction.crunch(u):
                jpr = jointProbability(solution)
                if jpr > pr2beat:
                    insertAt = bisect.bisect_left(nbestPr, jpr)
                    nbest.insert(insertAt, solution)
                    nbestPr.insert(insertAt, jpr)
                    if len(nbest) > n:
                        nbest.pop(0)
                        nbestPr.pop(0)
                        pr2beat = nbestPr[0] # only if full
    nbest.reverse() # [0] is now highest
    if skolemize:
        return [unify.skolemize(r) for r in nbest]
    else:
        return nbest
예제 #7
0
def incremental2(obs, kb, maxdepth, n, w, b, skolemize=True):
    # n = n-best
    # w = window size
    # b = beam of running candidate interpretations
    iteration = 1  # count for skolem constants
    indexed_kb = abduction.index_by_consequent_predicate(kb)
    previous = []  # proofs of the previous
    remaining = obs[:]  # obs yet to be interpretated

    # first, interpret the first window as normal
    window = remaining[0:w]
    remaining = remaining[w:]
    listoflists = [
        abduction.and_or_leaflists([ob], indexed_kb, maxdepth) for ob in window
    ]
    pr2beat = 0.0
    nbest = []  # solutions
    nbestPr = []  # probabilities
    for u in itertools.product(*listoflists):
        u = list(itertools.chain.from_iterable(u))
        if etcetera.bestCaseProbability(u) > pr2beat:
            for solution in abduction.crunch(u):
                jpr = etcetera.jointProbability(solution)
                if jpr > pr2beat:
                    insertAt = bisect.bisect_left(nbestPr, jpr)
                    nbest.insert(insertAt, solution)
                    nbestPr.insert(insertAt, jpr)
                    if len(nbest) > b:
                        nbest.pop(0)
                        nbestPr.pop(0)
                        pr2beat = nbestPr[0]  # only if full
    nbest.reverse()  # [0] is now highest
    previous = nbest
    pre = "$" + str(iteration) + ":"
    previous = [unify.skolemize(r, prefix=pre)
                for r in previous]  # skolemize the past (required)

    # next, interpret remaining windows in a special way
    while len(remaining) > 0:
        iteration += 1
        window = remaining[0:w]
        remaining = remaining[w:]
        pr2beat = 0.0
        nbest = []  # solutions
        nbestPr = []  # probabilities
        for previousSolution in previous:
            previousSolutionJpr = etcetera.jointProbability(previousSolution)
            context = getContext(previousSolution, obs, kb)
            listoflists = [
                contextual_and_or_leaflists([ob], indexed_kb, maxdepth,
                                            context) for ob in window
            ]

            for u in itertools.product(*listoflists):
                u = list(itertools.chain.from_iterable(u))
                if etcetera.bestCaseProbability(
                        u) * previousSolutionJpr > pr2beat:
                    for solution in abduction.crunch(u):
                        jpr = etcetera.jointProbability(
                            solution) * previousSolutionJpr
                        if jpr > pr2beat:
                            insertAt = bisect.bisect_left(nbestPr, jpr)
                            nbest.insert(insertAt,
                                         previousSolution + solution)  # joined
                            nbestPr.insert(insertAt, jpr)
                            if len(nbest) > b:
                                nbest.pop(0)
                                nbestPr.pop(0)
                                pr2beat = nbestPr[0]  # only if full
        nbest.reverse()  # [0] is now highest
        previous = nbest
        pre = "$" + str(iteration) + ":"
        previous = [unify.skolemize(r, prefix=pre)
                    for r in previous]  # skolemize the past (required)
    return previous[0:n]
예제 #8
0
intext = "".join(inlines)
kb, obs = parse.definite_clauses(parse.parse(intext))

logging.info("Parameters:")

for k, v in sorted(vars(args).iteritems(), key=lambda x: x[0]):
    logging.info("  %s: %s" % (k, v if not type(v) == file else v.name))

logging.info("Loading knowledge base...")

if args.kb:
    kblines = args.kb.readlines()
    kbtext = "".join(kblines)
    kbkb, kbobs = parse.definite_clauses(parse.parse(kbtext))
    kb.extend(kbkb)
    indexed_kb = abduction.index_by_consequent_predicate(kbkb)

if args.kbmcdb:
    import knowledgebase
    indexed_kb = knowledgebase.mcdb_t(args.kbmcdb)
    logging.info("Knowledge base loaded.")

# Handle forward

if args.forward:
    entailed = forward.forward(obs, kb)
    if args.graph:
        print(forward.graph(obs, entailed), file=args.outfile)
    else:
        for e in entailed:
            print(parse.display(e[0]), file=args.outfile)