Ejemplo n.º 1
0
def learn(lexicon, data):
    parser = chart.CCGChartParser(lex, chart.DefaultRuleSet)

    learning_rate = 0.1
    for x, y in data:
        weighted_results = parser.parse(x, return_weights=True)

        # Very dumb perceptron learning
        for result, score in weighted_results:
            print("\n================= %s / %s / %f" %
                  (" ".join(x), result.label()[0].semantics(), score))
            chart.printCCGDerivation(result)

            root_token, _ = result.label()
            correct = str(root_token.semantics()) == y
            sign = 1 if correct else -1

            for _, leaf_token in result.pos():
                leaf_token._weight += sign * 1

        print()
Ejemplo n.º 2
0
complains => S\\NP {complain}
''', True)

print(l3)
print()

print(
    '''====================================================================================
=== Derivation for \'somebody admires everybody\' obtained with ApplicationRuleSet ===
=== The semantics is the expected one.                                           ===
===================================================================================='''
)

parser1 = chart.CCGChartParser(l3, chart.ApplicationRuleSet)
parses = list(parser1.parse("somebody admires everybody".split()))
printCCGDerivation(parses[0])

print('''
=======================================================================================
=== Derivation for \'somebody admires everybody\' obtained with                       ===
=== ForwardTypeRaiseRule + ForwardApplication.                                      ===
=== The result has scrambled scopes when run in the development branch.             ===
======================================================================================='''
      )

RightwardRuleSet = [
    chart.BinaryCombinatorRule(chart.ForwardApplication),
    chart.ForwardTypeRaiseRule()
]

parser2 = chart.CCGChartParser(l3, RightwardRuleSet)
Ejemplo n.º 3
0
    parser.add_argument("--accept",
                        type=str,
                        help="check whether a grammar accepts stdin")
    parser.add_argument("--parse",
                        type=str,
                        help="print parses for stdin according to a grammar")
    parser.add_argument("--grade",
                        action='store_true',
                        help="run the autograder")
    parser.add_argument("-c",
                        action="store_true",
                        help="tokenize by character instead of words")

    args = parser.parse_args()

    if args.accept:
        raw = sys.stdin.read().strip()
        sent = [c for c in raw] if args.c else raw.split()
        print accept(sent, args.accept)

    if args.parse:
        sent = sys.stdin.read().split()
        for i, p in enumerate(parse(sent, args.parse)):
            print "===== Parse Sequence #{} =====".format(i + 1)
            printCCGDerivation(p)
            print "\n\n"

    if args.grade:
        os.chdir("autograder")
        call("python autograder.py".split())
Ejemplo n.º 4
0
    nueva => N/N
    ley => N
    compro => (S\\NP)/NP
    las => Det
    bebidas => N
    panaderia => N
    super => N
	pero => var\\.,var/.,var
''')

parser = chart.CCGChartParser(lex, chart.DefaultRuleSet)

print "\n################Bienvenido################\n"

entry = ""

while (entry != "fin"):
    entry = raw_input("\nIngrese la oracion para realizar el analisis o fin para salir:\n> ")
    
    if (entry != "fin"):
        cont = 0
        for parse in parser.parse(entry.split()):
            cont = cont + 1
            print "\n"
            chart.printCCGDerivation(parse)
            break
        
        if (cont == 0):
		    print "Error! La oracion ingresada es incorrecta.\n"

Ejemplo n.º 5
0
#TODO:
#Left, right, etc
#that
#Hard one:
#Is the purple thing the same shape as the large gray rubber thing?
#equal_shape(query_shape(unique(filter_color(scene,u'purple'))),query_shape(unique(filter_material(filter_color(filter_size(scene,u'large'),u'gray'),u'rubber'))))
parser = chart.CCGChartParser(lex2, chart.DefaultRuleSet)

#results = list(parser.parse("the same shape as the big metallic object".split()))
#results = list(parser.parse("a big brown object of the same shape as the green thing".split()))
results = list(parser.parse("the material of the big purple object".split()))
#results = list(parser.parse("any sphere to the left of it".split()))
#results = list(parser.parse("the purple thing the same shape as the large gray rubber thing".split()))

chart.printCCGDerivation(results[0])

#are there any other things that are => S {\x.exist(x)}
#right => ADJ {\x.right(x,'right')}
#right => NN {'right'}
#front => ADJ {\x.front(x)}
#front => NN {'front'}
#behind => ADV {\x.filter_size(x,'behind')}
#behind => NN {'behind'}

##Inference
semantics = True
lex_inference = lexicon.fromstring(r"""
    :- NN, INP, ADJ, DET, IN

    DET :: NN/NN