以下情况如何正确使用abstract class
How to use abstract class properly in the following case
我正在尝试实现一个代码来纠正给定单词中的小拼写错误。这些词是来自城市或州列表的专有名词。假设我们知道错字是来自城市还是州。我正在尝试实现两个 classes StateTypo
和 CityTypo
,它们是基础 class、BaseTypo
.[=18 的子 classes =]
这是 BaseTypo
和 StateTypo
的代码:
from collections import Counter
from abc import ABC, abstractmethod
class BaseTypo(ABC):
def __init__(self):
self.WORDS = self.dictionary()
self.N = sum(self.WORDS.values())
@abstractmethod
def dictionary(self):
return dict()
def known(self, words):
""""""
return set(w for w in words if w in self.WORDS)
def P(self, word):
"""Calculate probability of the given word"""
return self.WORDS[word] / self.N
def correction(self, word):
"""Most probable word"""
return max(self.candidates(word), key=self.P)
def candidates(self, word):
""" Generate possible correct words"""
return self.known([word]) or self.known(self.edits1(word)) or self.known(self.edits2(word)) or [word]
def edits1(self, word):
letters = 'abcdefghijklmnopqrstuvwxyz'
splits = [(word[:i], word[i:]) for i in range(len(word) + 1)]
deletes = [L + R[1:] for L, R in splits if R]
transposes = [L + R[1] + R[0] + R[2:] for L, R in splits if len(R) > 1]
replaces = [L + c + R[1:] for L, R in splits if R for c in letters]
inserts = [L + c + R for L, R in splits for c in letters]
return set(deletes + transposes + replaces + inserts)
def edits2(self, word):
return (e2 for e1 in self.edits1(word) for e2 in self.edits1(e1))
class StateTypo(BaseTypo):
def __init__(self):
super(BaseTypo, self).__init__()
self.address = 'states.txt'
def dictionary(self):
print(self.address)
with open(self.address, 'r') as file:
lines = file.readlines()
lines = [line.strip() for line in lines]
return Counter(lines)
if __name__ == '__main__':
a = StateTypo()
corrected = a.correction('himachal prfadesh')
print(corrected) # should ideally print himachal pradesh
但是,上面的代码遇到了以下错误:
Traceback (most recent call last):
File "correctTypo.py", line 59, in <module>
corrected = a.correction('himachal prfadesh')
File "correctTypo.py", line 25, in correction
return max(self.candidates(word), key=self.P)
File "correctTypo.py", line 29, in candidates
return self.known([word]) or self.known(self.edits1(word)) or self.known(self.edits2(w
ord)) or [word]
File "correctTypo.py", line 17, in known
return set(w for w in words if w in self.WORDS)
File "correctTypo.py", line 17, in <genexpr>
return set(w for w in words if w in self.WORDS)
AttributeError: 'StateTypo' object has no attribute 'WORDS'
现在我知道我们真的不需要抽象 class 来实现它,但是鉴于我最初的想法是在基础 [=26] 中有一个抽象方法,我该如何解决这个问题=] 是否保留?
super
表示super,它给你超级对象。
您可以通过 super(StateTypo, self).__init__()
或 BaseTypo.__init__(self)
初始化基础 class,但不能 super(BaseTypo, self).__init__()
.
而address
在初始化时用到,所以应该是
class StateTypo(BaseTypo):
def __init__(self):
self.address = 'states.txt'
super(StateTypo, self).__init__()
或者您可以更改其他代码以将基数 class 的 __init__
保留在第一行。
我正在尝试实现一个代码来纠正给定单词中的小拼写错误。这些词是来自城市或州列表的专有名词。假设我们知道错字是来自城市还是州。我正在尝试实现两个 classes StateTypo
和 CityTypo
,它们是基础 class、BaseTypo
.[=18 的子 classes =]
这是 BaseTypo
和 StateTypo
的代码:
from collections import Counter
from abc import ABC, abstractmethod
class BaseTypo(ABC):
def __init__(self):
self.WORDS = self.dictionary()
self.N = sum(self.WORDS.values())
@abstractmethod
def dictionary(self):
return dict()
def known(self, words):
""""""
return set(w for w in words if w in self.WORDS)
def P(self, word):
"""Calculate probability of the given word"""
return self.WORDS[word] / self.N
def correction(self, word):
"""Most probable word"""
return max(self.candidates(word), key=self.P)
def candidates(self, word):
""" Generate possible correct words"""
return self.known([word]) or self.known(self.edits1(word)) or self.known(self.edits2(word)) or [word]
def edits1(self, word):
letters = 'abcdefghijklmnopqrstuvwxyz'
splits = [(word[:i], word[i:]) for i in range(len(word) + 1)]
deletes = [L + R[1:] for L, R in splits if R]
transposes = [L + R[1] + R[0] + R[2:] for L, R in splits if len(R) > 1]
replaces = [L + c + R[1:] for L, R in splits if R for c in letters]
inserts = [L + c + R for L, R in splits for c in letters]
return set(deletes + transposes + replaces + inserts)
def edits2(self, word):
return (e2 for e1 in self.edits1(word) for e2 in self.edits1(e1))
class StateTypo(BaseTypo):
def __init__(self):
super(BaseTypo, self).__init__()
self.address = 'states.txt'
def dictionary(self):
print(self.address)
with open(self.address, 'r') as file:
lines = file.readlines()
lines = [line.strip() for line in lines]
return Counter(lines)
if __name__ == '__main__':
a = StateTypo()
corrected = a.correction('himachal prfadesh')
print(corrected) # should ideally print himachal pradesh
但是,上面的代码遇到了以下错误:
Traceback (most recent call last):
File "correctTypo.py", line 59, in <module>
corrected = a.correction('himachal prfadesh')
File "correctTypo.py", line 25, in correction
return max(self.candidates(word), key=self.P)
File "correctTypo.py", line 29, in candidates
return self.known([word]) or self.known(self.edits1(word)) or self.known(self.edits2(w
ord)) or [word]
File "correctTypo.py", line 17, in known
return set(w for w in words if w in self.WORDS)
File "correctTypo.py", line 17, in <genexpr>
return set(w for w in words if w in self.WORDS)
AttributeError: 'StateTypo' object has no attribute 'WORDS'
现在我知道我们真的不需要抽象 class 来实现它,但是鉴于我最初的想法是在基础 [=26] 中有一个抽象方法,我该如何解决这个问题=] 是否保留?
super
表示super,它给你超级对象。
您可以通过 super(StateTypo, self).__init__()
或 BaseTypo.__init__(self)
初始化基础 class,但不能 super(BaseTypo, self).__init__()
.
而address
在初始化时用到,所以应该是
class StateTypo(BaseTypo):
def __init__(self):
self.address = 'states.txt'
super(StateTypo, self).__init__()
或者您可以更改其他代码以将基数 class 的 __init__
保留在第一行。