Exemplo n.º 1
0
def parsing_examples():
    rules = list()
    rules.append("GAMMA -> R [1.0]")
    rules.append("R -> R '+' R [0.4]")
    rules.append("R -> '0' [0.3]")
    rules.append("R -> '1' [0.3]")
    grammar_rules = grammarutils.get_pcfg(rules)
    grammar = nltk.PCFG.fromstring(grammar_rules)

    sentence = '0 + 1'
    tokens = sentence.split(' ')

    # earley_parser = nltk.EarleyChartParser(grammar, trace=1)
    # e_chart = earley_parser.chart_parse(tokens)

    symbols = ['0', '1', '+']
    symbol_index = dict()
    for s in symbols:
        symbol_index[s] = symbols.index(s)
    grammar_rules = grammarutils.get_pcfg(rules,
                                          index=True,
                                          mapping=symbol_index)
    grammar = nltk.PCFG.fromstring(grammar_rules)

    classifier_output = [[0.8, 0.8, 0.1, 0.1, 0.1], [0.1, 0.1, 0.1, 0.8, 0.8],
                         [0.1, 0.1, 0.8, 0.1, 0.1]]
    classifier_output = np.array(classifier_output).T
    print classifier_output.shape
    gen_earley_parser = generalizedearley.GeneralizedEarley(grammar)
    best_string, prob = gen_earley_parser.parse(classifier_output)
    print best_string, prob
Exemplo n.º 2
0
 def __init__(self, rule_path, symbols):
     self._grammar = read_infuced_grammar(rule_path, symbols)
     self._symbols = symbols
     self._bottom_up_prob_matrix = []
     self._generalized_earley_parser = generalizedearley.GeneralizedEarley(
         self._grammar)
     self._current_action_seq = []
     self._exec_status = False
Exemplo n.º 3
0
def test_generalized_earley(grammar, classifier_output):
    gen_earley_parser = generalizedearley.GeneralizedEarley(grammar)
    best_string, prob = gen_earley_parser.parse(classifier_output)
    print 'best_string with prob {:.3f}:'.format(prob), best_string
    print gen_earley_parser.compute_labels()
    print np.argmax(classifier_output, axis=1)