Practical Applications of AI and NLP: A Complete Guide
Discover real-world applications of AI and NLP across different domains, with code examples.
I
InSkillCoach
· min
Practical Applications of AI and NLP: A Complete Guide
Artificial Intelligence and Natural Language Processing are used in many fields. This guide presents real-world use cases with code examples.
1. Text Analysis
Document Analysis
# Document analysis example with spaCy
import spacy
from collections import Counter
def analyze_document(text):
# Load the French model
nlp = spacy.load('fr_core_news_lg')
# Process the text
doc = nlp(text)
# Extract information
entities = [(ent.text, ent.label_) for ent in doc.ents]
keywords = [token.text for token in doc if not token.is_stop and token.is_alpha]
key_phrases = [chunk.text for chunk in doc.noun_chunks]
return {
"entités": entities,
"mots_clés": Counter(keywords).most_common(10),
"phrases_clés": key_phrases[:5]
}
Automatic Summarization
# Automatic summarization example with Transformers
from transformers import pipeline
def generate_summary(text, max_length=130, min_length=30):
# Initialize the model
summarizer = pipeline("summarization", model="facebook/bart-large-cnn")
# Generate the summary
summary = summarizer(
text,
max_length=max_length,
min_length=min_length,
do_sample=False
)
return summary[0]['summary_text']
2. Text Classification
Document Categorization
# Document classification example
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.naive_bayes import MultinomialNB
from sklearn.pipeline import Pipeline
def create_document_classifier():
# Create the pipeline
classifier = Pipeline([
('tfidf', TfidfVectorizer()),
('clf', MultinomialNB())
])
return classifier
def train_classifier(classifier, texts, labels):
# Train the classifier
classifier.fit(texts, labels)
return classifier
def predict_category(classifier, text):
# Predict the category
prediction = classifier.predict([text])
probabilities = classifier.predict_proba([text])
return prediction[0], probabilities[0]
Spam Detection
# Spam detection example
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.ensemble import RandomForestClassifier
def create_spam_detector():
# Create the detector
detector = Pipeline([
('vectorizer', CountVectorizer()),
('classifier', RandomForestClassifier())
])
return detector
def analyze_spam_probability(detector, email_text):
# Analyze the spam probability
probability = detector.predict_proba([email_text])[0]
return {
"spam_probability": probability[1],
"is_spam": probability[1] > 0.5
}
3. Content Generation
Article Generation
# Article generation example
from transformers import GPT2LMHeadModel, GPT2Tokenizer
def generate_article(topic, max_length=500):
# Load the model
model = GPT2LMHeadModel.from_pretrained('gpt2')
tokenizer = GPT2Tokenizer.from_pretrained('gpt2')
# Prepare the prompt
prompt = f"Article sur {topic}:"
inputs = tokenizer.encode(prompt, return_tensors='pt')
# Generate the text
outputs = model.generate(
inputs,
max_length=max_length,
num_return_sequences=1,
temperature=0.7,
top_p=0.9,
do_sample=True
)
return tokenizer.decode(outputs[0], skip_special_tokens=True)
Creating Automated Responses
# Response generation example
from transformers import pipeline
def generate_response(context, question):
# Initialize the model
qa_model = pipeline("question-answering", model="deepset/bert-base-cased-spqa")
# Generate the answer
result = qa_model(question=question, context=context)
return {
"réponse": result['answer'],
"confiance": result['score']
}
4. Sentiment Analysis
Comment Analysis
# Advanced sentiment analysis example
from transformers import pipeline
import pandas as pd
def analyze_comments(comments):
# Initialize the model
sentiment_analyzer = pipeline(
"sentiment-analysis",
model="nlptown/bert-base-multilingual-uncased-sentiment"
)
# Analyze the comments
results = []
for comment in comments:
sentiment = sentiment_analyzer(comment)[0]
results.append({
"commentaire": comment,
"sentiment": sentiment['label'],
"score": sentiment['score']
})
return pd.DataFrame(results)
Trend Tracking
# Trend tracking example
def track_sentiment_trends(comments, time_periods):
# Analyze sentiment by period
trends = {}
for period in time_periods:
period_comments = comments[period]
sentiment_scores = analyze_comments(period_comments)
trends[period] = {
"moyenne_sentiment": sentiment_scores['score'].mean(),
"distribution": sentiment_scores['sentiment'].value_counts().to_dict()
}
return trends
5. Business Applications
Automated Customer Service
# Customer service chatbot example
from transformers import AutoModelForCausalLM, AutoTokenizer
def create_customer_service_bot():
# Initialize the chatbot
model = AutoModelForCausalLM.from_pretrained("microsoft/DialoGPT-medium")
tokenizer = AutoTokenizer.from_pretrained("microsoft/DialoGPT-medium")
return model, tokenizer
def generate_bot_response(model, tokenizer, user_input, chat_history):
# Generate the response
inputs = tokenizer.encode(user_input + tokenizer.eos_token, return_tensors='pt')
outputs = model.generate(
inputs,
max_length=1000,
pad_token_id=tokenizer.eos_token_id,
no_repeat_ngram_size=3,
do_sample=True,
top_k=100,
top_p=0.7,
temperature=0.8
)
return tokenizer.decode(outputs[0], skip_special_tokens=True)
Legal Document Analysis
# Legal document analysis example
def analyze_legal_document(document_text):
# Load the model
nlp = spacy.load('fr_core_news_lg')
# Process the document
doc = nlp(document_text)
# Extract legal information
legal_entities = [(ent.text, ent.label_) for ent in doc.ents if ent.label_ in ['ORG', 'LAW']]
dates = [ent.text for ent in doc.ents if ent.label_ == 'DATE']
obligations = [sent.text for sent in doc.sents if any(word in sent.text.lower() for word in ['doit', 'obligation', 'nécessaire'])]
return {
"entités_légales": legal_entities,
"dates": dates,
"obligations": obligations
}
Conclusion
The applications of AI and NLP are vast:
- Text analysis and processing
- Classification and categorization
- Content generation
- Sentiment analysis
- Specific business applications
Key takeaways:
- Choose the right models
- Tailor solutions to your needs
- Evaluate performance
- Maintain and improve your systems
- Follow best practices
About InSkillCoach
Training and technology expert
Coach specializing in advanced technologies and AI, backed by GNeurone Inc.
Certifications:
- AWS Certified Solutions Architect – Professional
- Google Cloud certifications
- Microsoft Certified: DevOps Engineer Expert
- Certified Kubernetes Administrator (CKA)
- CompTIA Security+
1.2k
131
Comments
Comments are powered by GitHub Discussions
Sign in with GitHub to join the discussion