コード例 #1
0
def get_results_of_generator_query(cache, template):
    generator_query = getattr(template, 'generator_query')

    def first_attempt(template):
        return prepare_generator_query(template)

    def second_attempt(template):
        return prepare_generator_query(template,
                                       do_special_class_replacement=False)

    def third_attempt(template):
        return prepare_generator_query(template, add_type_requirements=False)

    for attempt, prepare_query in enumerate(
        [first_attempt, second_attempt, third_attempt], start=1):
        query = prepare_query(template)
        if query in cache:
            results = cache[query]
            break
        logging.debug('{}. attempt generator_query: {}'.format(attempt, query))
        results = query_dbpedia(query)
        sufficient_examples = len(
            results["results"]["bindings"]) >= EXAMPLES_PER_TEMPLATE / 3
        if sufficient_examples:
            cache[query] = results
            break
    return results
コード例 #2
0
if __name__ == '__main__':
    reload(sys)
    sys.setdefaultencoding("utf-8")

    # python2 interpreter.py ./fairseq/results/$1_$2.out.sys ./fairseq/results/$1_$2.out.ref 
    with open(sys.argv[1], 'r') as f_sys, open(sys.argv[2], 'r') as f_ref, \
        open(sys.argv[1]+'.decoded2', 'w') as f_decoded, open(sys.argv[1]+".decoded.final2", 'w') as f_out:
        
        count_false = 0

        for encoded_sparql_sys, encoded_sparql_ref in zip(f_sys, f_ref):
            decoded_sparql = decode(encoded_sparql_sys)
            f_decoded.write(decoded_sparql)
            decoded_sparql = fix_URI(decoded_sparql)
            return_json = query_dbpedia(decoded_sparql)
            is_sparql_right = "True"
            
            if '<unk>' in encoded_sparql_ref or ("results" in return_json.keys() and len(return_json["results"]["bindings"]) == 0):
                #print(encoded_sparql_sys)
                #print(encoded_sparql_ref)
                #print("false"+ str(count_false))
                is_sparql_right = "False"
                count_false = count_false + 1

            if count_false%100 == 0:
                print(count_false)

            f_out.write(is_sparql_right + ", " + decoded_sparql)