本文整理汇总了Python中Preprocess.list_to_dict方法的典型用法代码示例。如果您正苦于以下问题:Python Preprocess.list_to_dict方法的具体用法?Python Preprocess.list_to_dict怎么用?Python Preprocess.list_to_dict使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Preprocess
的用法示例。
在下文中一共展示了Preprocess.list_to_dict方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_tf_idf
# 需要导入模块: import Preprocess [as 别名]
# 或者: from Preprocess import list_to_dict [as 别名]
def get_tf_idf(query, src="google"):
tokens = get_tokens(query, src)
#converts into a dictionary
query_dictionary = Preprocess.list_to_dict(tokens,{})
#print query_dictionary
#creates a dictionary from a random wikipedia corpus
#dictionary = Preprocess.get_corpus(num_articles)
#loads in the dictionary of existing tf_idf words
dictionary = load_idf()
tf_idf_dictionary = {}
#calculates the tfidf for each key, storing it in a new dictionary
for key in query_dictionary.keys():
tf = query_dictionary[key]
if key in dictionary:
idf = dictionary[key]
else:
idf = math.log(18,10)
tf_idf_dictionary[key] = (float(tf)*float(idf))
#print tf_idf_dictionary
#sorts the dictionary based on the tfidf value, returning it as a list
sorted_dictionary = sorted(tf_idf_dictionary.iteritems(), key=operator.itemgetter(1), reverse = True)
return sorted_dictionary