-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatbot-nlp
More file actions
110 lines (70 loc) · 2.37 KB
/
chatbot-nlp
File metadata and controls
110 lines (70 loc) · 2.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# 1. Text Preprocessing
import nltk
from nltk.corpus import stopwords
from nltk.tokenize import word_tokenize
import string
nltk.download('punkt')
nltk.download('stopwords')
def preprocess_text(text):
# Convert to lowercase
text = text.lower()
# Remove punctuation
text = text.translate(str.maketrans('', '', string.punctuation))
# Tokenize
words = word_tokenize(text)
# Remove stop words
stop_words = set(stopwords.words('english'))
filtered_words = [word for word in words if word not in stop_words]
return filtered_words
text = "Natural Language Processing with Python is fun!"
processed_text = preprocess_text(text)
print(processed_text)
#2. Tokenization and Named Entity Recognition (NER) using spaCy
import spacy
# Load a pre-trained spaCy model
nlp = spacy.load('en_core_web_sm')
# Process the text
doc = nlp("Apple is looking at buying U.K. startup for $1 billion")
# Tokenization
tokens = [token.text for token in doc]
print("Tokens:", tokens)
# Named Entity Recognition (NER)
for ent in doc.ents:
print(ent.text, ent.label_)
#3. Sentiment Analysis using transformers
from transformers import pipeline
# Initialize sentiment analysis pipeline
sentiment_analysis = pipeline('sentiment-analysis')
# Analyze sentiment
result = sentiment_analysis("I love programming with Python!")
print(result)
#4. Text Classification using Pre-trained Models
from transformers import pipeline
# Initialize text classification pipeline
classifier = pipeline('zero-shot-classification')
# Text to classify
text = "The company's quarterly earnings have increased significantly."
# Possible labels
labels = ["economy", "business", "technology"]
# Classify text
result = classifier(text, candidate_labels=labels)
print(result)
#5. Building a Basic NLP Chatbot
import random
def respond_to_query(query):
responses = {
'hello': 'Hi there! How can I assist you today?',
'bye': 'Goodbye! Have a great day!',
'thanks': 'You’re welcome! Let me know if you need anything else.'
}
for key in responses:
if key in query.lower():
return responses[key]
return "I'm sorry, I don't understand your question."
# Example conversation
query = "Hello"
response = respond_to_query(query)
print("Chatbot:", response)
query = "Thanks for your help!"
response = respond_to_query(query)
print("Chatbot:", response)