我正在django制作一个博客,我正在尝试将此算法(algo.py)实现到我的一个页面中的搜索问题选项。算法从用户获取输入并应在基础上显示排序结果列表每个问题的分数。
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})
当我运行我的服务器并输入搜索查询时,我收到错误&#34;&#39; QuerySet&#39;对象不支持项目分配&#34;
考虑到我是python和django的新手
,请帮助我答案 0 :(得分:1)
我认为这只是因为您将查询集传递给期望列表的quickSort
功能。您可以将查询集强制转换为列表,它应该解决问题
sorted_list = list(Question.objects.all())