Пример #1
0
Файл: mace.py Проект: sp00/nltk
def test_build_model(arguments):
    """
    Try to build a ``nltk.sem.Valuation``.
    """
    lp = LogicParser()
    g = lp.parse('all x.man(x)')
    alist = [lp.parse(a) for a in ['man(John)',
                                   'man(Socrates)',
                                   'man(Bill)',
                                   'some x.(-(x = John) & man(x) & sees(John,x))',
                                   'some x.(-(x = Bill) & man(x))',
                                   'all x.some y.(man(x) -> gives(Socrates,x,y))']]

    m = MaceCommand(g, assumptions=alist)
    m.build_model()
    spacer()
    print "Assumptions and Goal"
    spacer()
    for a in alist:
        print '   %s' % a
    print '|- %s: %s\n' % (g, decode_result(m.build_model()))
    spacer()
    #print m.model('standard')
    #print m.model('cooked')
    print "Valuation"
    spacer()
    print m.valuation, '\n'
Пример #2
0
def test_build_model(arguments):
    """
    Try to build a ``nltk.sem.Valuation``.
    """
    lp = LogicParser()
    g = lp.parse("all x.man(x)")
    alist = [
        lp.parse(a)
        for a in [
            "man(John)",
            "man(Socrates)",
            "man(Bill)",
            "some x.(-(x = John) & man(x) & sees(John,x))",
            "some x.(-(x = Bill) & man(x))",
            "all x.some y.(man(x) -> gives(Socrates,x,y))",
        ]
    ]

    m = MaceCommand(g, assumptions=alist)
    m.build_model()
    spacer()
    print("Assumptions and Goal")
    spacer()
    for a in alist:
        print("   %s" % a)
    print("|- %s: %s\n" % (g, decode_result(m.build_model())))
    spacer()
    # print m.model('standard')
    # print m.model('cooked')
    print("Valuation")
    spacer()
    print(m.valuation, "\n")
Пример #3
0
Файл: mace.py Проект: sp00/nltk
def test_model_found(arguments):
    """
    Try some proofs and exhibit the results.
    """
    lp = LogicParser()
    for (goal, assumptions) in arguments:
        g = lp.parse(goal)
        alist = [lp.parse(a) for a in assumptions]
        m = MaceCommand(g, assumptions=alist, end_size=50)
        found = m.build_model()
        for a in alist:
            print '   %s' % a
        print '|- %s: %s\n' % (g, decode_result(found))
Пример #4
0
Файл: mace.py Проект: sp00/nltk
def test_transform_output(argument_pair):
    """
    Transform the model into various Mace4 ``interpformat`` formats.
    """
    lp = LogicParser()
    g = lp.parse(argument_pair[0])
    alist = [lp.parse(a) for a in argument_pair[1]]
    m = MaceCommand(g, assumptions=alist)
    m.build_model()
    for a in alist:
        print '   %s' % a
    print '|- %s: %s\n' % (g, m.build_model())
    for format in ['standard', 'portable', 'xml', 'cooked']:
        spacer()
        print "Using '%s' format" % format
        spacer()
        print m.model(format=format)
Пример #5
0
def test_transform_output(argument_pair):
    """
    Transform the model into various Mace4 ``interpformat`` formats.
    """
    lp = LogicParser()
    g = lp.parse(argument_pair[0])
    alist = [lp.parse(a) for a in argument_pair[1]]
    m = MaceCommand(g, assumptions=alist)
    m.build_model()
    for a in alist:
        print("   %s" % a)
    print("|- %s: %s\n" % (g, m.build_model()))
    for format in ["standard", "portable", "xml", "cooked"]:
        spacer()
        print("Using '%s' format" % format)
        spacer()
        print(m.model(format=format))
Пример #6
0
def parse_fol(s):
    """
    Temporarily duplicated from L{nltk.sem.util}.
    Convert a  file of First Order Formulas into a list of C{Expression}s.
    
    @parameter s: the contents of the file
    @type s: C{str}
    @return: a list of parsed formulas.
    @rtype: C{list} of L{Expression}
    """
    from nltk.sem import LogicParser
    statements = []
    lp = LogicParser()
    for linenum, line in enumerate(s.splitlines()):
        line = line.strip()
        if line.startswith('#') or line=='': continue
        try:
            statements.append(lp.parse(line))
        except Error:
            raise ValueError, 'Unable to parse line %s: %s' % (linenum, line)
    return statements
Пример #7
0
def parse_fol(s):
    """
    Temporarily duplicated from ``nltk.sem.util``.
    Convert a  file of first order formulas into a list of ``Expression`` objects.

    :param s: the contents of the file
    :type s: str
    :return: a list of parsed formulas.
    :rtype: list(Expression)
    """
    from nltk.sem import LogicParser
    statements = []
    lp = LogicParser()
    for linenum, line in enumerate(s.splitlines()):
        line = line.strip()
        if line.startswith('#') or line=='': continue
        try:
            statements.append(lp.parse(line))
        except Error:
            raise ValueError('Unable to parse line %s: %s' % (linenum, line))
    return statements
Пример #8
0
def parse_fol(s):
    """
    Temporarily duplicated from L{nltk.sem.util}.
    Convert a  file of First Order Formulas into a list of C{Expression}s.
    
    @parameter s: the contents of the file
    @type s: C{str}
    @return: a list of parsed formulas.
    @rtype: C{list} of L{Expression}
    """
    from nltk.sem import LogicParser
    statements = []
    lp = LogicParser()
    for linenum, line in enumerate(s.splitlines()):
        line = line.strip()
        if line.startswith('#') or line == '': continue
        try:
            statements.append(lp.parse(line))
        except Error:
            raise ValueError, 'Unable to parse line %s: %s' % (linenum, line)
    return statements
Пример #9
0
def parse_fol(s):
    """
    Temporarily duplicated from ``nltk.sem.util``.
    Convert a  file of first order formulas into a list of ``Expression`` objects.

    :param s: the contents of the file
    :type s: str
    :return: a list of parsed formulas.
    :rtype: list(Expression)
    """
    from nltk.sem import LogicParser
    statements = []
    lp = LogicParser()
    for linenum, line in enumerate(s.splitlines()):
        line = line.strip()
        if line.startswith('#') or line == '': continue
        try:
            statements.append(lp.parse(line))
        except Exception:
            raise ValueError('Unable to parse line %s: %s' % (linenum, line))
    return statements