Esempio n. 1
0
def print_result_json(d, output):
    """Pretty print result.

    :param d: result from run_problog
    :param output: output file
    :param precision:
    :return:
    """
    import json
    result = {}
    success, d = d
    if success:
        prob, facts = d
        result['SUCCESS'] = True
        if facts is not None:
            result['atoms'] = list(
                map(
                    lambda n: (str(-n), False)
                    if n.is_negated() else (str(n), True), facts))
        if prob is not None:
            result['prob'] = round(prob, 10)
    else:
        result['SUCCESS'] = False
        result['err'] = process_error(d)
        result['original'] = str(d)
    print(json.dumps(result), file=output)
    return 0
Esempio n. 2
0
def print_result_standard(result, output=sys.stdout):
    success, result = result
    if success:
        print(result, file=output)
        return 0
    else:
        print(process_error(result), file=output)
        return 1
Esempio n. 3
0
def print_result(d, output, precision=8):
    success, d = d
    if success:
        score, weights, names, iterations, lfi = d
        weights = list(map(lambda x: round(x, precision), weights))
        print(score, weights, names, iterations, file=output)
        return 0
    else:
        print(process_error(d), file=output)
        return 1
Esempio n. 4
0
def print_result(result, output=sys.stdout, oneline=False):
    success, result = result
    if success:
        # result is a sequence of samples
        first = True
        for s in result:
            if not oneline and not first:
                print("----------------", file=output)
            first = False
            print(s, file=output)
    else:
        print(process_error(result), file=output)
Esempio n. 5
0
def main(filename, examplefile):
    try:
        examples = list(lfi.read_examples(examplefile))
        program = PrologFile(filename)
        score, weights, names, iterations = lfi.run_lfi(program, examples)

        new_names = []
        for n in names:
            new_names.append((str(n.with_probability()), ) +
                             program.lineno(n.location)[1:])

        return True, (score, weights, new_names, iterations)
    except Exception as err:
        return False, {"err": process_error(err)}
Esempio n. 6
0
def print_result(result, output=sys.stdout):
    success, result = result
    if success:
        prob, facts = result
        if facts is None:
            print('%% The model is not satisfiable.', file=output)
        else:
            for atom in facts:
                print(atom, file=output)
            if prob is not None:
                print('%% Probability: %.10g' % prob)
        return 0
    else:
        print(process_error(result), file=output)
        return 1
Esempio n. 7
0
def print_result_json(d, output, **kwdargs):
    """Pretty print result.

    :param d: result from run_problog
    :param output: output file
    :param precision:
    :return:
    """
    import json
    result = {}
    success, d = d
    if success:
        result['SUCCESS'] = True
        result['results'] = [[(str(k), str(v)) for k, v in dc.items()]
                             for dc in d]
    else:
        result['SUCCESS'] = False
        result['test'] = str(type(d))
        result['err'] = process_error(d)
        result['original'] = str(d)
    print(json.dumps(result), file=output)
    return 0