How to make all possible combination of optional parameters (boolean value) in a self-defined function work in python?

Viewed 25

I have a self-defined function like this:

def clean(text, punct=True, lowercase=True, stopword=True, lemma=True):

    import re
    import nltk
    #stop words
    from nltk.corpus import stopwords
    nltk.download('stopwords')
    eng_stops = set(stopwords.words('english'))
    #lemmatization
    from nltk.stem import WordNetLemmatizer 
    lemmatizer = WordNetLemmatizer()

    if punct==True and lowercase==True and stopword==True and lemma==True:
        # remove punctuation
        text = re.sub(r'[^\w\s]', '', text)
        # convert all letters to lower case
        words = text.lower().split()
        # remove stop words
        words = [w for w in words if not w in eng_stops]    
        # lemmatize all words
        words = [lemmatizer.lemmatize(word) for word in words]

    elif punct==True and lowercase==True and stopword==True:
        text = re.sub(r'[^\w\s]', '', text)
        words = text.lower().split()
        words = [w for w in words if not w in eng_stops]
    
    elif punct==True and lowercase==True:
        text = re.sub(r'[^\w\s]', '', text)
        words = text.lower().split()
    
    elif punct==True:
        words = ' '.join(re.sub(r'[^\w\s]', '', text).split())
    ...
    return return (' '.join(words))

I need to have all the possible combinations of the 4 optional parameters in the function. The most difficult thing here (I think) is that if more than 2 optional parameters are set True, then the latter parameters need to work on the result of the former ones. I think the way I am doing it is sloppy. There must be a better way of doing it. Could anyone please share with me how? Thank you in advance.

1 Answers

The main problem you have run into here is repeating yourself. In this specific case you don't seem to need to repeat all specific ways the text could be altered instead for example create an if statement for each parameter instead of all possible combinations of the parameters.

if punct==True:
        # remove punctuation
        text = re.sub(r'[^\w\s]', '', text)
if lowercase==True:
        # convert all letters to lower case
        text = text.lower()
words = text.split()
if stopword==True:
        # remove stop words
        words = [w for w in words if not w in eng_stops]
if lemma==True:   
        # lemmatize all words
        words = [lemmatizer.lemmatize(word) for word in words]
Related