Beispiel #1
0
def transform_structurally(structure_opts, golden_id_file):
    if os.path.exists(structure_opts.tmp_file):
        debug('Structure Transformation result already exists!\n')
        return
    golden_ids = None
    if os.path.exists(golden_id_file):
        with open(golden_id_file) as fp:
            golden_ids = [line.strip() for line in fp]
    f = open(structure_opts.grammar, 'rb')
    debug('Loading the Grammar')
    grammar = pickle.load(f)
    debug('Grammar Loaded From : %s' % structure_opts.grammar)
    assert isinstance(grammar, JavaGrammar)
    all_scores, _, all_trees = structure_translate(structure_opts, grammar, structure_opts.n_best)
    if not os.path.exists('tmp'):
        os.mkdir('tmp')
    total_found = 0
    with open(structure_opts.tmp_file, 'w') as tmp:
        for cidx, (trees, scores) in enumerate(zip(all_trees, all_scores)):
            trees = [' '.join(tree) for tree in trees]
            if golden_ids is not None:
                golden_tree = golden_ids[cidx]
                total_found += check_existence(trees, golden_tree)
            t_strs = [tree + '/' + str(score) for tree, score in zip(trees, scores)]
            wstr = '\t'.join(t_strs)
            tmp.write(wstr + '\n')
        tmp.close()
    debug('Total Tree Correctly found', total_found)
def transform_structurally(structure_opts):
    if os.path.exists(structure_opts.tmp_file):
        debug('Structure Transformation result already exists!\n')
        return
    tgt = structure_options.src
    tgt = tgt.replace('prev.rule', 'next.token.id')
    debug(tgt)
    inp = open(tgt)
    golden_rules = [line.strip() for line in inp]
    inp.close()
    f = open(structure_opts.grammar, 'rb')
    debug('Loading the Grammar')
    grammar = pickle.load(f)
    debug('Grammar Loaded From : %s' % structure_opts.grammar)
    assert isinstance(grammar, JavaGrammar)
    all_scores, all_rules, all_trees = structure_translate(
        structure_opts, grammar, structure_opts.n_best)
    if not os.path.exists('tmp'):
        os.mkdir('tmp')
    correct_rule_count = 0
    with open(structure_opts.tmp_file, 'w') as tmp:
        for trees, rules, scores, golden_rule in zip(all_trees, all_rules,
                                                     all_scores, golden_rules):
            # debug(trees, scores)

            node_is_str_list = [
                ' '.join(tree) for tree, score in zip(trees, scores)
            ]
            debug('Length : ', len(trees))
            if golden_rule in node_is_str_list:
                debug('Index : ', node_is_str_list.index(golden_rule))
                correct_rule_count += 1
            else:
                debug('Index : ', -1)
            t_strs = [
                ' '.join(tree) + '/' + str(score)
                for tree, score in zip(trees, scores)
            ]
            wstr = '\t'.join(t_strs)
            tmp.write(wstr + '\n')
        tmp.close()
    debug(correct_rule_count)
    exit()
Beispiel #3
0
def transform_structurally(structure_opts):
    if os.path.exists(structure_opts.tmp_file):
        debug('Structure Transformation result already exists!\n')
        return
    f = open(structure_opts.grammar, 'rb')
    debug('Loading the Grammar')
    grammar = pickle.load(f)
    debug('Grammar Loaded From : %s' % structure_opts.grammar)
    assert isinstance(grammar, JavaGrammar)
    all_scores, _, all_trees = structure_translate(structure_opts, grammar,
                                                   structure_opts.n_best)
    if not os.path.exists('tmp'):
        os.mkdir('tmp')
    with open(structure_opts.tmp_file, 'w') as tmp:
        for trees, scores in zip(all_trees, all_scores):
            debug(trees, scores)
            t_strs = [
                ' '.join(tree) + '/' + str(score)
                for tree, score in zip(trees, scores)
            ]
            wstr = '\t'.join(t_strs)
            tmp.write(wstr + '\n')
        tmp.close()