【问题标题】:Show Total Retrieved Documents显示检索到的文档总数
【发布时间】: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


【解决方案1】:

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

您可以像这样使用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 %}

【讨论】:

    猜你喜欢
    • 2023-03-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-04
    • 2014-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多