I have a nested list of objects called "words". It consists of objects of a class that has data like conf(float), end(float), start(float), word(string) I want to remove duplicate occuring objects which has same "word"
class Word:
''' A class representing a word from the JSON format for vosk speech recognition API '''
def __init__(self, dict):
'''
Parameters:
dict (dict) dictionary from JSON, containing:
conf (float): degree of confidence, from 0 to 1
end (float): end time of the pronouncing the word, in seconds
start (float): start time of the pronouncing the word, in seconds
word (str): recognized word
'''
self.conf = dict["conf"]
self.end = dict["end"]
self.start = dict["start"]
self.word = dict["word"]
def to_string(self):
''' Returns a string describing this instance '''
return "{:20} from {:.2f} sec to {:.2f} sec, confidence is {:.2f}%".format(
self.word, self.start, self.end, self.conf*100)
def compare(self, other):
if self.word == other.word:
return True
else:
return False
I tried this but couldn't get it working
nr_words = []
c = custom_Word.Word({'conf': 0.0, 'end': 0.00, 'start': 0.00, 'word': ''})
nr_words.append(c)
for w in words:
for nr in nr_words:
if w.compare(nr_words[nr]):
print("same")
else:
print("not same")
nr_words.append(w.word)
nr_words.append(w.start)
nr_words.append(w.end)
here is the collection of objects

each object contain data like this
{'conf': 0.0, 'end': 0.00, 'start': 0.00, 'word': 'hello'}
{'conf': 0.0, 'end': 1.00, 'start': 0.00, 'word': 'hello'}
{'conf': 0.0, 'end': 2.00, 'start': 0.00, 'word': 'to'}
my compare function from the class "Word" works perfectly
words[0].compare(words[1])
True
I also tried this way
for i in range(0,len(words)):
for o in range(0,len(nr_words)):
if words[i].compare(nr_words[o]):
print("same")
else:
print("not same")
nr_words.append(w.word)
nr_words.append(w.start)
nr_words.append(w.end)
but got error "AttributeError: 'str' object has no attribute 'word'"
I am not sure whats wrong in attribute word can some good soul guide me on how to remove the duplicate objects by "word" Thanks in advance!
dictas a parameter, aslistorstr, it shadows a python keyword and is not recommended.