【发布时间】:2019-05-18 03:52:01
【问题描述】:
我正在使用 TF-IDF 算法通过我输入的查询检索相关文档。我已经成功检索到相关文件,并且也显示出来了。但我想显示已检索到的 TOTAL 文档。
我正在使用此代码(在 result.html 中)来计算文档,但它显示了任何内容。
{% for i in result %}
{{i.pekerjaan.count}}
{% endfor %}
这是 main.py :
result = []
for i in range(len(list_of_query)):
l = []
for j in range(len(tokens_doc)):
dic = {}
for kata in list_of_query[i]:
sums = 0
ind = queries.index(kata)
#print(ind)
for val in weight[ind][j].values():
sums += val
if(sums!= 0):
dic['docno'] = j+1
dic['score'] = sums
dic['deskripsi'] = doc_deskripsi[j]
dic['pekerjaan'] = doc_pekerjaan[j]
# dic['text'] = doc_text[j]
if(len(dic) != 0): l.append(dic)
result.append(l)
result
a=0
for i in range(len(list_of_query)):
result[i] = sorted(result[i], key = lambda x : x['score'], reverse = True)
for i in range(len(list_of_query)):
with open('resultquery.txt'.format(counter = i+1), 'w') as f:
f.write('Top 5 Documents :\n')
f.write('q_Id - DOC NO - Pekerjaan - SCORE\n')
if len(result[i]) > 5:
for x in range(5):
c = i + 1
f.write('%s - %s - %s - %s\n' %(c,doc_number[result[i][x]['docno']-1],result[i][x]['title'],result[i][x]['score']))
else:
for x in result[i]:
c = i + 1
f.write('%s - %s - %s - %s\n' %(c,doc_number[x['docno']-1],x['pekerjaan'],x['score']))
结果如下图,显示为NULL(在Result之后:)
像上面这张图,它只显示文档,而不是整个文档。
我期望的输出应该是这样的:
我希望有人能帮我解决这个问题。 谢谢。
【问题讨论】:
-
发布您的模型/查询集
-
我很抱歉,但我不明白。你能解释得更清楚吗,拜托。谢谢。
-
提供从
models.py生成result和对应模型的代码。特别是pekerjaan字段是如何声明的。 -
感谢您的建议,我已将其发布为名称
main.py
标签: python django python-3.x tf-idf inverted-index