def removestopwords(text): stopwords = set(stopwords.words('english')) # set of…
- User
def remove_stopwords(text): stop_words = set(stopwords.words('english')) # set of stopwords in English tokens = word_tokenize(text) # tokenize the string into words filtered_tokens = [word for word in tokens if word.lower() not in stop_words and word.lower() not in ['none', 'nan', 'n/a']] # filter out stopwords cleaned_text = ' '.join(filtered_tokens).replace(',', '')
codereturn ' '.join(filtered_tokens) # join the remaining words back into a stringdf['summary_ksm_no_stop'] = df['summary_ksm'].apply(remove_stopwords)
remove unneded puncations
- Assistant
Sure! Here is the modified code with the removal of unnecessary punctuations:
import string
def remove_stopwords(text): stop_words = set(stopwords.words('english')) # set of stopwords in English tokens = word_tokenize(text) # tokenize the string into words filtered_tokens = [word for word in tokens if word.lower() not in stop_words and word.lower() not in ['none', 'nan', 'n/a']] # filter out stopwords cleaned_tokens = [word for word in filtered_tokens if word not in string.punctuation] # remove punctuation from filtered tokens cleaned_text = ' '.join(cleaned_tokens)
codereturn cleaned_text # join the remaining words back into a stringdf['summary_ksm_no_stop'] = df['summary_ksm'].apply(remove_stopwords)