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.