コード例 #1
0
ファイル: search.py プロジェクト: advaypal/CS3245
def main():
    #Get arguments
    query_file = get_argument_value('-q')
    dictionary_file = get_argument_value('-d')
    postings_file = get_argument_value('-p')
    output_file = get_argument_value('-o')
    # Create searcher object
    searcher = Searcher(dictionary_file, postings_file)
    input_file = file(query_file, 'r')
    queries = input_file.read().splitlines()
    output = file(output_file, 'w')
    for query in queries:
        # No null queries
        if not query:
            continue
        result = searcher.evaluate_query(query)
        doc_string = ' '.join(map(str, result))
        output.write(doc_string + '\n')
        #output.write(query + " " + doc_string + '\n')
        #output.write(query + " " + str(len(result)) + '\n')
    input_file.close()
    output.close()
コード例 #2
0
def run_search(dict_file, postings_file, queries_file, results_file):
    """
    Using the given dictionary file and postings file,
    perform searching on the given queries file and output the results to a file
    """
    print('Running search on the queries...')

    dictionaries = Dictionaries(dict_file)
    dictionaries.load()
    postings = Postings(postings_file)
    searcher = Searcher(dictionaries, postings)

    result_string = ''
    with open(queries_file, 'r') as f, open(results_file, 'w') as o:
        for i, query in enumerate(f):
            searcher.set_query(query.strip())
            output = searcher.evaluate_query()
            result_string += output.strip() + '\n'
            searcher.clear_postings()
        f.close()
        o.write(result_string.strip())
        o.close()