Search code examples
pythondjangopython-3.xtf-idfinverted-index

Show Total Retrieved Documents


I am using TF-IDF algorithm to retrieve the relevant documents with the query that i input. I have successfully retrieve the relevant documents, and show it too. But i want to show the TOTAL documents that has been retrieved.

I am using this code (in result.html)to count the documents, but it show anything.

{% for i in result %}
  {{i.pekerjaan.count}}
{% endfor %}

Here is the 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']))

The result is like this picture below, that show NULL (after Result :)

result

Like this picture above, it just show the documents, but not the total document.

The output that I expected should be like this:

enter image description here

I hope some one can help me fix this problem. Thank you.


Solution

  • You are trying to refer to count that is supposed to be used with Manager (as if you got results from database using django ORM), but actually you are providing a dict instead.

    You can use either length filter like this:

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

    Or prepopulate length of results into dict, like this:

    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)
    

    and output it in template:

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