File tree Expand file tree Collapse file tree 1 file changed +9
-18
lines changed
Expand file tree Collapse file tree 1 file changed +9
-18
lines changed Original file line number Diff line number Diff line change 11import nltk
22from nltk .corpus import stopwords
3- from nltk .tokenize import word_tokenize
3+ from nltk .tokenize import wordpunct_tokenize
44from string import punctuation
5+ from colorama import Fore
56
7+ # Example sentence
68example_sent = "This is a sample sentence, showing off the stop words filtration."
79
10+ # Load English stopwords
811stop_words = set (stopwords .words ('english' ))
9- print (stop_words )
10- word_tokens = word_tokenize (example_sent )
11- print (word_tokens )
1212
13- # filtered_sentence = [w for w in word_tokens if not w in stop_words]
14-
15- filtered_sentence = []
16-
17- for w in word_tokens :
18- if w not in stop_words :
19- filtered_sentence .append (w )
13+ # Tokenize and filter in one step
14+ filtered_sentence = [
15+ word .upper () for word in wordpunct_tokenize (example_sent )
16+ if word .lower () not in stop_words and word not in punctuation
17+ ]
2018
2119print (filtered_sentence )
22-
23- filter = []
24- for a in filtered_sentence :
25- if a not in punctuation :
26- filter .append (a )
27- print (filter )
28-
You can’t perform that action at this time.
0 commit comments