Natural Language Processing
Fundamental Concepts
- Tokenization
- Stemming
- Lemmatization
- POS Tagging
- Named Entity Recognition
- Stopword Removal
- Syntax
- Dependency Parsing
- Parsing
- Chunking
Text Processing & Cleaning
- Text Normalization
- Bag of Words
- TF-IDF
- N-grams
- Word Embeddings
- Sentence Embeddings
- Document Similarity
- Cosine Similarity
- Text Vectorization
- Noise Removal
Tools, Libraries & APIs
- NLTK
- spaCy
- TextBlob
- Hugging Face Transformers
- Gensim
- OpenAI
- CoreNLP
- FastText
- Flair NLP
- ElasticSearch + NLP
Program(s)
- Build a Chatbot Using NLP
- Extracting Meaning from Text Using NLP in Python
- Extracting Email Addresses Using NLP in Python
- Extracting Names of People, Cities, and Countries Using NLP
- Format Email Messages Using NLP
- N-gram program
- Resume Skill Extraction Using NLP
- Sentiment Analysis in NLP
- Optimizing Travel Routes Using NLP & TSP Algorithm in Python
Program: Format Email Messages Using NLP
Below is a Python program that uses Natural Language Processing (NLP) techniques to format email messages. The program cleans and structures the email text to improve readability and effectiveness.
import reimport nltkfrom nltk.tokenize import sent_tokenize, word_tokenizefrom nltk.corpus import stopwordsfrom nltk.stem import WordNetLemmatizer
# Download necessary NLTK resourcesnltk.download('punkt')nltk.download('stopwords')nltk.download('wordnet')
def preprocess_email(email_text): """ Preprocess the email text by cleaning, tokenizing, and formatting it. """ # Convert text to lowercase email_text = email_text.lower()
# Remove special characters and numbers email_text = re.sub(r'[^a-zA-Z\s]', '', email_text)
# Tokenize sentences sentences = sent_tokenize(email_text)
# Tokenize words and remove stopwords stop_words = set(stopwords.words('english')) lemmatizer = WordNetLemmatizer() formatted_sentences = []
for sentence in sentences: words = word_tokenize(sentence) filtered_words = [lemmatizer.lemmatize(word) for word in words if word not in stop_words] formatted_sentence = ' '.join(filtered_words) formatted_sentences.append(formatted_sentence)
# Join sentences into a formatted email formatted_email = '\n'.join(formatted_sentences) return formatted_email
def format_email_subject(subject): """ Format the email subject by capitalizing the first letter of each word. """ return subject.title()
def format_email(sender, recipient, subject, body): """ Format the entire email with sender, recipient, subject, and body. """ formatted_subject = format_email_subject(subject) formatted_body = preprocess_email(body)
email_message = f""" From: {sender} To: {recipient} Subject: {formatted_subject}
{formatted_body} """ return email_message
# Example usagesender = "john.doe@example.com"recipient = "jane.smith@example.com"subject = "meeting reminder"body = """Hello Jane, I hope you are doing well. This is a reminder about our meeting tomorrow at 10 AM.Please bring the project report. Thank you!"""
formatted_email = format_email(sender, recipient, subject, body)print(formatted_email)
Explanation of the Program
-
Preprocessing Email Text:
- The
preprocess_email
function cleans the email body by converting it to lowercase, removing special characters and numbers, and tokenizing sentences and words. - Stopwords (e.g., “the”, “is”, “and”) are removed to focus on meaningful words.
- Words are lemmatized (e.g., “running” → “run”) to normalize the text.
- The
-
Formatting Email Subject:
- The
format_email_subject
function capitalizes the first letter of each word in the subject to make it more professional.
- The
-
Formatting the Entire Email:
- The
format_email
function combines the sender, recipient, formatted subject, and preprocessed body into a structured email message.
- The
-
Example Usage:
- The program demonstrates how to format an email with a sender, recipient, subject, and body.
Output Example
From: john.doe@example.com To: jane.smith@example.com Subject: Meeting Reminder
hello jane hope well reminder meeting tomorrow 10 am please bring project report thank
Benefits of Using NLP for Email Formatting
- Improved Readability: By removing unnecessary words and normalizing text, the email becomes easier to read.
- Consistency: NLP ensures that emails follow a standardized format, making them more professional.
- Automation: This program can be integrated into email automation workflows to save time and effort.
This program is a simple yet effective way to format email messages using NLP techniques.