显示检索到的文件总数

时间:2019-05-18 03:52:01

标签: python django python-3.x tf-idf inverted-index

我正在使用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之后:)

result

就像上面的图片一样,它仅显示文档,而不显示整个文档。

我期望的输出应该是这样的:

enter image description here

我希望有人可以帮助我解决此问题。 谢谢。

1 个答案:

答案 0 :(得分:0)

您试图引用应该与Manager一起使用的count(就像您使用django ORM从数据库中获得结果一样),但是实际上您是在提供一个字典。 >

您可以使用length过滤器,如下所示:

{% for i in result %}
  {{ i.pekerjaan|length }}
{% endfor %}

或将结果长度预填充为dict,如下所示:

main.py:

# ...
if(sums!= 0):
    dic['docno'] = j+1
    dic['score'] = sums
    dic['deskripsi'] = doc_deskripsi[j]
    dic['pekerjaan'] = doc_pekerjaan[j]
    dic['len_pekerjaan'] = len(doc_pekerjaan[j])
if(len(dic) != 0): l.append(dic)

并将其输出到模板中

{% for i in result %}
  {{ i.len_pekerjaan }}
{% endfor %}