I am trying to instantiate all the sublcasses of my abstract class that i have in my code, because the tagger function takes the same input. this is the code:
import spacy
nlp = spacy.load("en_core_web_sm")
from abc import ABC, abstractmethod
class SpacyTagger(ABC):
@abstractmethod
def tagger(self, texts):
pass
class SpacyTokenizer(SpacyTagger):
def tagger(Self,texts):
for doc in nlp.pipe([texts]):
return ([n.text for n in doc])
class SpacyLemmatizer(SpacyTagger):
def tagger(self,texts):
for doc in nlp.pipe([texts]):
return ([n.lemma_ for n in doc])
of course, i can instantiate all subclasses separately like this:
token= SpacyTokenizer().tagger('she is a girl')
lemma= SpacyLemmatizer().tagger('she is a girl')
but instead I would like to have it all in one line, or in a function that takes the text as the input. i also tried the function below to get all the subclasses' names, but do not know how to proceed from here, so i can just instantiate them once with the same input.
def all_subclasses(cls):
return set(cls.__subclasses__()).union(
[s for c in cls.__subclasses__() for s in all_subclasses(c)])