本文整理汇总了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]))
示例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))
示例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'))