我正在执行一项任务,其中我必须使一个优先级队列包含一个以元组为单位的条目,并根据起始位置或概率进行排序。 我有一个用于按适当顺序将条目放入队列的类。 当我尝试输入队列时,它给了我错误:
TypeError: '<' not supported between instances of 'Entry' and 'Entry'
这是我的代码:
class Entry:
#Entry to be used
def __init__(self, word, start_pos, log_prob, back_ptr):
self.word=word
self.start_pos=start_pos
self.log_prob=log_probability
self.back_ptr=back_ptr
我在优先级队列中输入项目的代码:
keys= Pw.keys()
pq = PriorityQueue()
input=""
count=0
with open("data/input/dev.txt") as f:
#getting the input ready, ommitting newline, joining together
for line in f:
n_line = line.strip()
input=n_line
finalindex=len(input)-1
chart = [None] * (finalindex+1)
#initializing priorityqueue, gather candidates for first word
inserted=0
for allowed_length in range(1,14):
first_word=input[0:allowed_length]
if first_word in Pw:
#Pw is word probability
entry=Entry(first_word,0,log10(Pw(first_word)),None)
#print(entry)
pq.put((0,entry))
inserted+=1
if inserted==0:
first_word=input[0]
entry = Entry(first_word,0,log10(Pw(first_word)), None)
pq.put((0,entry))
我在for循环的pq.put中遇到错误。 我可以将队列排序到start_pos或log_prob。
我在class Entry
中尝试过:
def _lt_ (self, other):
return self.start_pos<other.start_pos
它不能解决我的问题。
谢谢。