Search code examples
pythonabstract-classabstract

How to use abstract class properly in the following case


I am trying to implement a code that corrects minor typos in a given word. These words are proper nouns which are either from a list of cities or states. It is given that we know whether the typo is from a city or a state. I am trying to implement two classes StateTypo and CityTypo which are subclasses to a base class, BaseTypo.

Here's the code for BaseTypo and 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

However, the above code runs into the following error:

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'

Now I know we don't really need to have an abstract class to implement this, but how can I solve this issue given my initial idea of having an abstract-method in a base class is preserved?


Solution

  • super means super,it gives you the super object.

    You can init base class by super(StateTypo, self).__init__() or BaseTypo.__init__(self) ,but not super(BaseTypo, self).__init__().

    And address is used during init,so it should be

    class StateTypo(BaseTypo):
        def __init__(self):
            self.address = 'states.txt'
            super(StateTypo, self).__init__()
    

    Or you can change other code to keep __init__ of base class at the first line.