Example #1
0
#===================================================================================================
sentences = [
    # adjunction
    "john saw the boy",
    "john saw the nice boy",
    "john saw the nice little boy",
    "john ate the very tasty apple",
    "some colorless green ideas sleep furiously",

    # conjunction
    "john and mary ate the banana",
    "john ate the banana and the apple",
    "john ate the banana and apple",
    "john ate the apple and saw the boy",
    "john kissed and hugged mary",

    # ambiguity
    "john saw the nice boy with the telescope",
    "john saw the boy with the glasses and the telescope",
]

for text in sentences:
    print "==============================================================="
    print text
    print "==============================================================="
    trees = parse(g, S, text.split())
    for i, t in enumerate(trees):
        print "(%d)" % (i + 1, )
        t.show()
        print
Example #2
0
# Here we define the simple (CFG-like) grammar
#     T -> a | T OP T
#     OP -> +
#
#===================================================================================================
T = NonTerminal("T")
OP = NonTerminal("OP")
g = TIG(init_trees=[
    T("a"),
    T(T, OP("+"), T),
], aux_trees=[])

#===================================================================================================
# Let's print a couple of trees
#===================================================================================================
for i, t in enumerate(parse(g, T, "a + a + a")):
    print "[%d]" % (i + 1, )
    t.show()
    print

#===================================================================================================
# Now let's see we really generate the Catalan sequence (first 10 numbers)
#===================================================================================================
produced = [
    len(parse(g, T, " + ".join("a" * i).split())) for i in range(1, 11)
]

print "The sequence we got is ", produced
assert produced == [1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862]
print "Hooray!"
Example #3
0
sentences = [
    # adjunction
    "john saw the boy",
    "john saw the nice boy",
    "john saw the nice little boy",
    "john ate the very tasty apple",
    "some colorless green ideas sleep furiously",
    
    # conjunction
    "john and mary ate the banana",
    "john ate the banana and the apple",
    "john ate the banana and apple",
    "john ate the apple and saw the boy",
    "john kissed and hugged mary",
    
    # ambiguity
    "john saw the nice boy with the telescope",
    "john saw the boy with the glasses and the telescope",
]

for text in sentences:
    print "==============================================================="
    print text
    print "==============================================================="
    trees = parse(g, S, text.split())
    for i, t in enumerate(trees):
        print "(%d)" % (i + 1,)
        t.show()
        print

Example #4
0
#     OP -> +
#
#===================================================================================================
T = NonTerminal("T")
OP = NonTerminal("OP")
g = TIG(init_trees = [
        T("a"), 
        T(T, OP("+"), T),
    ],
    aux_trees = []
)

#===================================================================================================
# Let's print a couple of trees
#===================================================================================================
for i, t in enumerate(parse(g, T, "a + a + a")):
    print "[%d]" % (i + 1,)
    t.show()
    print

#===================================================================================================
# Now let's see we really generate the Catalan sequence (first 10 numbers)
#===================================================================================================
produced = [len(parse(g, T, " + ".join("a" * i).split())) 
    for i in range(1, 11)] 

print "The sequence we got is ", produced
assert produced == [1, 1, 2, 5, 14, 42, 132, 429, 1430, 4862]
print "Hooray!"