0

I'm trying to make class that can clean text. The class has several methods, like converting text to lower case, spell checking the text, lemmatizing the text, removing special characters etc. Finally I have a method (cleaned_text) that calls all the above methods in order and returns the final cleaned text. Here is the code:

class TextCleaner:
    def __init__(self, string):
        self.string = string

    def lowercase(self):
        string_lower = self.string.lower()
        return string_lower

    def regex_stripper(self):
        stripped = re.sub(r"[^a-zA-Z0-9 ']+", " ", self.string)
        no_double_spaces = re.sub(r' +', ' ', stripped)
        return no_double_spaces

    def spell_checker(self):
        spell_checked = sym_spell.lookup_compound(self.string, max_edit_distance=2)[0].term
        return spell_checked

    def remove_stop(self):
        no_stop_words = " ".join(i for i in self.string.split() if i not in stop)
        return no_stop_words

    def lemmatize(self):
        doc = nlp(self.string)
        lemmatized_sentence = " ".join([token.lemma_ for token in doc])
        return lemmatized_sentence

    @lowercase
    @regex_stripper
    @spell_checker
    @remove_stop
    @lemmatize
    def cleaned_text(self):
        return self.string

I'm not very well versed with decorators so sorry for the clumsy code. The cleaned_text method ought to do the following methods in order - lowercase, regex_stripper, remove_stop, lemmatize and then finally return the cleaned text.

martineau
  • 119,623
  • 25
  • 170
  • 301
FahdS
  • 45
  • 5
  • decorator works bottom to top in order. you can just call all the method in cleaned_text method, instead of doing or using decorators – sahasrara62 Jan 08 '22 at 23:05
  • yes, i'm aware of that. I can just use a foo variable and keep running the methods on the variable one by one. Was curious if this task could be achieved by a Decorator/Wrapper – FahdS Jan 08 '22 at 23:10
  • [chaining function decorators](https://stackoverflow.com/questions/739654/how-to-make-function-decorators-and-chain-them-together) this may help – sahasrara62 Jan 08 '22 at 23:21
  • Thank you!! Looks like a workable solutions. Will check it out. Thanks again !!! – FahdS Jan 09 '22 at 00:44

0 Answers0