<jupyter_start><jupyter_text>Students Do: Crude StopwordsFor this activity, create a function that takes in an article and outputs a list of words that is free of stopwords and any non-letter characters. After looking at the results, define your own list of stopwords to add to the NLTK default set.<jupyter_code># Imports
from nltk.corpus import reuters, stopwords
from nltk.tokenize import word_tokenize
import re

# Code to download corpora
import nltk
nltk.download('reuters')
nltk.download('punkt')
nltk.download('stopwords')
# Store an article to work with
crude_article = reuters.raw(fileids=reuters.fileids(categories='crude')[2])
# Print the stored article
print(crude_article)
# Complete the `clean_text` function
def clean_text(article):
    
    # Define a set of stopwords using `stopwords.words()`
    sw = set(stopwords.words('english'))

    # Define the regex parameters
    regex = re.compile("[^a-zA-Z ]")

    # Apply regex parameters to article
    re_clean = regex.sub('', article)

    # Apply `word_tokenize` to the regex scrubbed text
    words = word_tokenize(re_clean)

    # Create list of lower-case words that are not in the stopword set
    output = [word.lower() for word in words if word.lower() not in sw]
    
    # Return the final list
    return output
# Pass article into `clean_text` and store the result
result = clean_text(crude_article)
# Print out unique words
print(set(result))
# Second iteration, with custom stopwords
def clean_text(article):
    
    # Define a set of stopwords using `stopwords.words()`
    sw = set(stopwords.words('english'))
    
    # Create custom stopwords
    sw_addons = {'said', 'sent', 'found', 'including', 'today', 'announced', 'week', 'basically', 'also'}

    # Define the regex parameters
    regex = re.compile("[^a-zA-Z ]")

    # Apply regex parameters to article
    re_clean = regex.sub('', article)

    # Apply `word_tokenize` to the regex scrubbed text
    words = word_tokenize(re_clean)

    # Create list of lower-case words that are not in the stopword set
    output = [word.lower() for word in words if word.lower() not in sw.union(sw_addons)]
    
    # Return the final list
    return output
# Pass article into `clean_text` and examine new results
result2 = clean_text(crude_article)
print(set(result2))
print(f'Without Custom Storpwords: {len(set(result))}\nWith Custom Storpwords: {len(set(result2))}')<jupyter_output>Without Custom Storpwords: 87
With Custom Storpwords: 79
{'fraction_non_alphanumeric': 0.09733441033925687, 'fraction_numerical': 0.0032310177705977385, 'mean_word_length': 4.225738396624473, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '483686', 'license': 'MIT', 'n_tokens_mistral': 774, 'n_tokens_neox': 743, 'n_words': 269, 'path': 'PythonJupyterNotebooks/Week12-Day1-Activity4-crude_stopwords.ipynb', 'repo_name': 'nomadic-me/CodingCheatSheets', 'size': '2476'}