I am trying to implement a recursive function, in which my base condition is working fine, another condition is also working properly. But when I jump to the recursive condition it gives me error that,"class name" object has no attribute "function name" .
My class:
class NGram(object):
def __init__(self):
self.n = None
self.ngramprobability = None
self.uniquegrams = [
["sample", "this"],
["is", "a"],
["this", "is"],
["sample"],
["this"],
["a"],
["is"],
["a", "sample"],
]
self.out = [
[
["sample", 0.16666666666666666],
["this", 0.3333333333333333],
["a", 0.16666666666666666],
["is", 0.3333333333333333],
],
[
["sample", "this", 1.0],
["is", "a", 0.5],
["this", "is", 1.0],
["a", "sample", 1.0],
],
]
def get_prob(self, words):
if len(words) == 1:
probability = [j[-1] for i in self.out for j in i if j[:-1] == words][0]
return probability # condition works fine
elif words in self.uniquegrams:
probability = [j[-1] for i in self.out for j in i if j[:-1] == words][0]
return probability # condition works fine
else:
return self.get_prob(self, words[1:]) * 0.4
My script that is raising errors:
# train bi_gram
bi_gram = NGram()
c = bi_gram.get_prob(["this", "sample"])
print(c)
Where I am making mistake?
self.function_nameand when you callself.function_nameyou shouldn't include theselfvar