-1

我正在django中創建一個博客,並試圖在我的一個頁面中的搜索問題選項中實現此算法(algo.py)。該算法從用戶並且應該基於每個問題的分數顯示排序結果列表。'QuerySet'對象不支持項目分配'錯誤

algo.py

import string 
import math 
from consult.models import Question, Reply 


def main(search_text): 
    search_words = split_text(search_text) 
    idf_array = [0] * len(search_words) 

    for i in range(0, len(search_words)): 
     idf_array[i] = idf(search_words[i]) 

    score = [0] * Question.objects.all().count() 
    k = -1 
    for post in Question.objects.all(): 
     k += 1 
     score[k] = scores(post, search_words, idf_array) 

    sorted_list = Question.objects.all() 
    quickSort(score, sorted_list) 
    return sorted_list 


def split_text(text): 
    text_words = [word.strip(string.punctuation) for word in text.split()] 
    stop_words = {'with', 'at', 'from', 'into', 'of', 'on', 'by', 'and', 'after', 'how', 
       'since', 'but', 'for', 'to', 'in', 'what', 'when', 'where', 'who', 'whom', 'why'} 
for w1 in stop_words: 
    for w2 in text_words: 
     if w1 == w2: 
      text_words.remove(w1) 
return text_words 


def idf(term): 
    cnt = 0 
    for post in Question.objects.all(): 
     title_words = split_text(post.title) 
     body_words = split_text(post.body) 
     answers_words = [] 
     ans = Reply.objects.filter(name=post) 
     for a in ans: 
      a_words = split_text(a.text) 
      answers_words = answers_words + a_words 

     words = title_words + answers_words + body_words 
     for t in words: 
      if t == term: 
       cnt += 1 
       break 

    idf_value = math.log(Question.objects.all().count()/cnt) 
    return idf_value 


def scores(post, search_words, idf_array): 
    df_array = [0] * len(search_words) 
    title_words = split_text(post.title) 
    body_words = split_text(post.body) 
    answers_words = [] 
    ans = Reply.objects.filter(name=post) 
    for a in ans: 
     a_words = split_text(a.text) 
     answers_words = answers_words + a_words 
    words = title_words + answers_words + body_words 

    j = -1 
    for wrd1 in search_words: 
     j += 1 
     for wrd2 in words: 
      if wrd1 == wrd2: 
       df_array[j] += 1/len(words) 
    sum = 0 
    for l in range(0, len(search_words)): 
    sum += idf_array[l] * df_array[l] 
    return sum 


def quickSort(alist, a2): 
    quickSortHelper(alist, a2, 0, len(alist) - 1) 


def quickSortHelper(alist, a2, first, last): 
    if first < last: 
     splitpoint = partition(alist, a2, first, last) 

     quickSortHelper(alist, a2, first, splitpoint - 1) 
     quickSortHelper(alist, a2, splitpoint + 1, last) 


def partition(alist, a2, first, last): 
    pivotvalue = alist[first] 
    leftmark = first + 1 
    rightmark = last 

    done = False 
    while not done: 

     while leftmark <= rightmark and alist[leftmark] >= pivotvalue: 
      leftmark = leftmark + 1 

     while alist[rightmark] <= pivotvalue and rightmark >= leftmark: 
      rightmark = rightmark - 1 

     if rightmark < leftmark: 
      done = True 
     else: 
      temp = alist[leftmark] 
      temp1 = a2[leftmark] 
      alist[leftmark] = alist[rightmark] 
      a2[leftmark] = a2[rightmark] 
      alist[rightmark] = temp 
      a2[rightmark] = temp1 

    temp = alist[first] 
    temp1 = a2[first] 
    alist[first] = alist[rightmark] 
    a2[first] = a2[rightmark] 
    alist[rightmark] = temp 
    a2[rightmark] = temp1 

    return rightmark 

views.py

def search(request): 
s = request.POST.get('search') 
lis = algo.main(s) 
return render(request, 'consult/search.html', {'comment': lis}) 

當我跑我的服務器,並輸入搜索查詢我得到的錯誤「‘查詢集’對象不支持項目分配」

請幫我考慮的事實,我是新來的Python和Django的

+0

您需要顯示發生錯誤的位置以及完整的消息,我們無法讀取您的想法。 – snb

回答

1

我認爲這只是因爲您正在將一個查詢集傳遞到期望列表的quickSort函數中。您可以將查詢集強制轉換爲列表並且它應該修復問題

sorted_list = list(Question.objects.all()) 
相關問題