concept stopword in category dask

This is an excerpt from Manning's book Data Science with Python and Dask.
To find out more of what the reviewers in this dataset are writing about, we will perform a bigram analysis of the review text. Bigrams are pairs of adjacent words in text. The reason bigrams tend to be more useful than simply counting the frequency of individual words is they typically add more context. For example, we might expect positive reviews to contain the word “good” very frequently, but that doesn’t really help us understand what is good. The bigram “good flavor” or “good packaging” tells us a lot more about what the reviewers find positive about the products. Another thing that we need to do in order to better understand the true subject or sentiment of the reviews is to remove words that don’t help convey that information. Many words in the English language add structure to a sentence but don’t convey information. For example, articles like “the,” “a,” and “an” do not provide any context or information. Because these words are so common (and necessary for proper sentence formation), we’re just as likely to find these words in positive reviews as we are negative reviews. Since they don’t add any information, we will remove them. These are known as stopwords, and one of the most important data preparation tasks when doing text analysis is detecting and removing stopwords. Figure 9.9 shows examples of a few common stopwords.
Figure 9.9 Example of stopwords
![]()
Listing 9.26 Extract and filter functions
from nltk.corpus import stopwords from nltk.tokenize import RegexpTokenizer from functools import partial tokenizer = RegexpTokenizer(r'\w+') #1 def extract_reviews(element): #2 return element['review/text'].lower() def filter_stopword(word, stopwords): #3 return word not in stopwords def filter_stopwords(tokens, stopwords): #4 return list(filter(partial(filter_stopword, stopwords=stopwords), tokens)) stopword_set = set(stopwords.words('english')) #5 #1 Create a tokenizer using a regex expression that will extract only words; this means punctuation, numbers, and so forth will be discarded. #2 This function takes an element from the Bag, gets the review text, and changes all letters to lowercase; this is important because Python is case-sensitive. #3 This function returns True if the word is not in the list of stopwords. #4 This function uses the filter function above it to check every word in a list of words (tokens) and discard the word if it is a stopword. #5 Get the list of English stopwords from NLTK and cast it from a list to a set; using a set is faster than a list in this comparison.In listing 9.26, we’re defining a few functions to help grab the review text from the original Bag and filter out the stopwords. One thing to point out is the use of the
partialfunction inside thefilter_stopwordsfunction. Usingpartialallows us to freeze the value of thestopwordsargument while keeping the value of thewordargument dynamic. Since we want to compare every word to the same list of stopwords, the value of thestopwordsargument should remain static. With our data preparation functions defined, we’ll now map over the Bag of reviews to extract and clean the review text.Listing 9.27 Extracting, tokenizing, and cleaning the review text
review_text = reviews.map(extract_reviews) #1 review_text_tokens = review_text.map(tokenizer.tokenize) #2 review_text_clean = review_text_tokens.map(partial(filter_stopwords, stopwords=stopword_set)) #3 review_text_clean.take(1) # Produces the following output: ''' (['bought', 'several', 'vitality', 'canned', 'dog', 'food', 'products', 'found', 'good', 'quality', 'product', 'looks', 'like', 'stew', 'processed', 'meat', 'smells', 'better', 'labrador', 'finicky', 'appreciates', 'product', 'better'],) ''' #1 Transforms the Bag of review objects to a Bag of review strings #2 Transforms the Bag of review strings to a Bag of lists of tokens #3 Removes the stopwords from each list of tokens in the BagThe code in listing 9.27 should be pretty straightforward. We simply use the
mapfunction to apply the extracting, tokenizing, and filtering functions to the Bag of reviews. As you can see, we’re left with a Bag of lists, and each list contains all the unique nonstopwords found in the text of each review. If we take one element from this new Bag, we’re returned a list of all words in the first review (except for stopwords, that is). This is important to note: currently our Bag is a nested collection. We’ll come back to that momentarily. However, now that we have the cleaned list of words for each review, we’ll transform our Bag of lists of tokens into a Bag of lists of bigrams.