当前位置: 首页>>代码示例>>Python>>正文


Python Dictionary.from_json方法代码示例

本文整理汇总了Python中dictionary.Dictionary.from_json方法的典型用法代码示例。如果您正苦于以下问题:Python Dictionary.from_json方法的具体用法?Python Dictionary.from_json怎么用?Python Dictionary.from_json使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在dictionary.Dictionary的用法示例。


在下文中一共展示了Dictionary.from_json方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: search

# 需要导入模块: from dictionary import Dictionary [as 别名]
# 或者: from dictionary.Dictionary import from_json [as 别名]
def search(dictionary_file, postings_file, queries_file, output_file):
    # Build in memory dict from dictionary_file.
    with open(dictionary_file) as dict_file:
        dictionary = Dictionary.from_json(dict_file.read())

    # Process queries.
    with open(output_file, 'w+') as output:
        with open(queries_file) as qfile:
            with PostingsFile(postings_file, mode='r') as pfile:
                for query in qfile:
                    # Strip newline character.
                    query = query.replace('\n', '')
                    query = query.replace('\r', '')
                    prefix_notation = parse_query.infix_to_prefix(query)

                    # Process all words in the query here.
                    processed = []
                    for token in prefix_notation:
                        if parse_query.is_operand(token):
                            token = process_word(token)
                        processed.append(token)

                    query = parse_query.process_infix_query(processed)
                    result = execute_query(query, dictionary, pfile)

                    output.write('%s\n' % ' '.join([str(x) for x in result]))
开发者ID:ymichael,项目名称:cs3245-hw,代码行数:28,代码来源:search_index.py

示例2: search

# 需要导入模块: from dictionary import Dictionary [as 别名]
# 或者: from dictionary.Dictionary import from_json [as 别名]
def search(dictionary_file, postings_file, queries_file, output_file):
    # Build in memory dict from dictionary_file.
    with open(dictionary_file) as dict_file:
        dictionary = Dictionary.from_json(dict_file.read())

    # Process queries.
    with open(output_file, 'w+') as output:
        with open(queries_file) as qfile:
            with PostingsFile(postings_file, mode='r',
                    entry_cls=PostingsFileEntryWithFrequencies) as pfile:
                for query in qfile:
                    # Strip newline character.
                    query = query.strip()

                    # Process all words in the query here.
                    query_tokens = process_query(query)
                    query_tf = collections.Counter(query_tokens)
                    query_terms = sorted(set(query_tokens))

                    # Calculate query vector
                    query_vector = \
                        [logtf(query_tf[term]) for term in query_terms]
                    query_vector = list(unit_vector(query_vector))

                    # Execute query
                    results = execute_query(
                        query_terms, query_vector, dictionary, pfile)

                    # Write doc_ids to output file.
                    results = [str(x) for x in results]
                    output.write('%s\n' % ' '.join(results))
开发者ID:kaiserahmed,项目名称:cs3245-hw,代码行数:33,代码来源:search_index.py

示例3: test_dictionary_to_json_from_json

# 需要导入模块: from dictionary import Dictionary [as 别名]
# 或者: from dictionary.Dictionary import from_json [as 别名]
def test_dictionary_to_json_from_json():
    d = Dictionary()
    d.add_term('asdf', 1, 1)
    d.add_term('asdf', 2, 1)
    d.add_term('qwer', 1, 1)
    d.add_term('zxcv', 1, 1)

    d2 = Dictionary.from_json(d.to_json())
    assert_eq(d2.all_docs(), d.all_docs())
    assert_eq(d2.all_terms(), d.all_terms())

    assert_eq(d2.get_frequency('asdf'), d.get_frequency('asdf'))
    assert_eq(d2.get_frequency('qwer'), d.get_frequency('qwer'))
    assert_eq(d2.get_frequency('zxcv'), d.get_frequency('zxcv'))

    assert_eq(d2.get_head('asdf'), d.get_head('asdf'))
    assert_eq(d2.get_head('qwer'), d.get_head('qwer'))
    assert_eq(d2.get_head('zxcv'), d.get_head('zxcv'))

    assert_eq(d2.get_tail('asdf'), d.get_tail('asdf'))
    assert_eq(d2.get_tail('qwer'), d.get_tail('qwer'))
    assert_eq(d2.get_tail('zxcv'), d.get_tail('zxcv'))
开发者ID:kaiserahmed,项目名称:cs3245-hw,代码行数:24,代码来源:test_dictionary.py


注:本文中的dictionary.Dictionary.from_json方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。