Docsity

Text Preprocessing Complete Notes | NLP | Tokenization, Stemming & Lemmatization, Slides of Artificial Intelligence

Toggle sidebar
bg1
Text Preprocessing
Raw text data is often unstructured, noisy and inconsistent,
containing typos, punctuation, stopwords and irrelevant
information.
Text preprocessing converts this data into a clean, structured and
standardized format, enabling effective feature extraction and
improving model performance.
•Improves feature representation, helping NLP models achieve
higher accuracy and robustness.
•Simplifies text data, reducing computational overhead and
accelerating model training.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13

Partial preview of the text

Download Text Preprocessing Complete Notes | NLP | Tokenization, Stemming & Lemmatization and more Slides Artificial Intelligence in PDF only on Docsity!

Text Preprocessing

 (^) Raw text data is often unstructured, noisy and inconsistent, containing typos, punctuation, stopwords and irrelevant information.  (^) Text preprocessing converts this data into a clean, structured and standardized format, enabling effective feature extraction and improving model performance.

  • (^) Improves feature representation, helping NLP models achieve higher accuracy and robustness.
  • (^) Simplifies text data, reducing computational overhead and

Why Preprocess Text?

Improves Feature Representation Helps NLP models achieve higher accuracy and robustness by providing cleaner input data. Reduces Computational Overhead Simplifies text data, reducing noise and accelerating model training.

Step 1: Preparing the Sample

Corpus

• Here we define a sample corpus containing

a variety of text examples, including HTML

tags, emojis, URLs, numbers, punctuation

and typos.

• This corpus will be used to demonstrate

each preprocessing step in detail.

Step 2: Text Cleaning and Regular Expressions  (^) Text cleaning is the process of removing noise and unwanted elements from raw text to make it structured and easier for NLP models to analyze.  (^) Regular expressions(regex) is a useful tool in text preprocessing that allow you to find, match and manipulate patterns in text efficiently.

  • (^) Converts all text to lowercase to maintain consistency.
  • (^) Removes HTML tags using Beautiful Soup to extract only meaningful text.
  • (^) Eliminates numbers and punctuation to reduce noise.
  • (^) Uses regex (\W+ and \s+) to remove special characters and extra spaces.

Step 4: Stopword Removal

 Stopwords are common words in a language (like “is”,
“the”, “and”) that usually do not add significant
meaning to text analysis.
 Removing them helps NLP models focus on the more
meaningful words in the text.
  • (^) Loads the list of English stopwords from NLTK.
  • (^) Loops through each word in every document and
removes any word that is in the stopword list.
  • (^) Creates a new corpus (filtered_corpus) that contains

Step 5: Stemming

  • (^) Stemming is the process of reducing words to their

root or base form.

  • (^) It helps in normalizing text by treating different forms

of a word (e.g., “running”, “runs”) as the same word

(“run”).

  • (^) Initializes the PorterStemmer from NLTK to perform

stemming.

  • (^) Loops through each word in every document of the

filtered corpus.

  • (^) Converts each word to its stemmed form and

Step 7: Contractions Expansion  (^) Contractions expansion is the process of converting shortened forms of words (like “can’t”, “won’t”) into their full forms (“cannot”, “will not”).  (^) This helps NLP models better understand the meaning of the text.

  • (^) Imports the contractions library to handle contraction expansion.
  • (^) Iterates through each document in the original corpus.
  • (^) Replaces all contractions in the text with their full forms and stores the result in expanded_corpus.

Advanced Preprocessing Steps

Step 8: Spell Correction

Spell correction is the process of identifying and correcting misspelled words in text. This ensures that NLP models receive accurate and meaningful words for analysis.

  • (^) Imports the SpellChecker library to detect and correct spelling errors.
  • (^) Initializes the spell checker object using SpellChecker().
  • (^) Iterates through each token in every document of the tokenized corpus, replacing misspelled words with their correct forms, and stores the result in corrected_corpus.

Advanced Preprocessing Steps

Step 10: Emoji Conversion

Emoji conversion is the process of converting emojis in text into descriptive text labels. This allows NLP models to understand the meaning conveyed by emojis.

  • (^) Imports the emoji library to handle emoji processing.
  • (^) Iterates through each document in the original corpus.
  • (^) Replaces all emojis with their descriptive text equivalents and stores the result in emoji_corpus.

 (^) NLTK is an open-source Python library used for text preprocessing, analysis, and NLP applications such as tokenization, stemming, lemmatization, POS tagging, and sentiment analysis.  (^) NLTK Resources (or NLTK Corpora and Models): These are pre-trained datasets, lexical resources, and language models that NLTK downloads and uses for various NLP tasks.

  • (^) punkt → Used for tokenization.
  • (^) stopwords → Used to remove common words.
  • (^) wordnet → Used for lemmatization.
  • (^) averaged_perceptron_tagger → Used for Part-of-Speech tagging.

Applications & Advantages

Real-World Applications

  • (^) Sentiment analysis on reviews and social media
  • (^) Spam detection, news categorization, topic labeling
  • (^) Search engines and recommendation systems
  • (^) Chatbots, translation, summarization, and entity detection Key Advantages - (^) Removes noise and inconsistencies for clean, standardized text - (^) Improves model accuracy and classification results - (^) Reduces data size, speeding up training - (^) Enables extraction of n-grams, embeddings, and semantic features

Limitations to Keep in Mind

Information Loss Removing stopwords or punctuation can discard contextually important information. Over-Processing Risk Aggressive cleaning reduces context and may hurt model performance. Language Specificity Rules vary by language, making generalization across languages difficult. Computational Cost Preprocessing adds time and resource overhead; errors in stemming or spell correction propagate to final results.