You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Content generation, SEO, marketing and advertising
Customer Service
importnltkimportrefromnltk.corpusimportstopwordsfromnltk.tokenizeimportword_tokenizenltk.download('punkt')
nltk.download('stopwords')
deftext_normalization(text):
# Convert text to lowercasetext=text.lower()
# Remove punctuation using regular expressionstext=re.sub(r'[^\w\s]', '', text)
# Tokenize the text into wordswords=word_tokenize(text)
# Remove stopwordsstop_words=set(stopwords.words('english'))
words= [wordforwordinwordsifwordnotinstop_words]
# Lemmatization using NLTK's WordNetLemmatizerlemmatizer=nltk.WordNetLemmatizer()
words= [lemmatizer.lemmatize(word) forwordinwords]
# Join the words back into a normalized sentencenormalized_text=' '.join(words)
returnnormalized_text# Test the functioninput_text="This is a sample sentence, showing off the stop words filtration."normalized_text=text_normalization(input_text)
print(normalized_text)
sample sentence showing stop word filtration
fromsklearn.feature_extraction.textimportTfidfVectorizerfromsklearn.svmimportSVCfromsklearn.pipelineimportmake_pipeline# Sample data for trainingpassword_queries= [
"How to reset my password?",
"I forgot my password.",
"What is my password?",
"My password is not working.",
]
non_password_queries= [
"How to create a new account?",
"New user registration.",
"How to delete my account?",
"Close my account.",
"I want to open a new account.",
]
# Create labels for the datapassword_labels= ["password inquiry"] *len(password_queries)
non_password_labels= ["non-password inquiry"] *len(non_password_queries)
# Combine the data and labelsdata=password_queries+non_password_querieslabels=password_labels+non_password_labels# Create the classifier pipelineclassifier=make_pipeline(TfidfVectorizer(), SVC(kernel='linear'))
# Train the classifierclassifier.fit(data, labels)
# Test the classifier with new queriesnew_queries= [
"I lost my password, please help.",
"Can I change my password?",
"I want to open two accounts. Is it possible?",
"I want to recover my account.",
]
# Predict the intent for new queriesforqueryinnew_queries:
prediction=classifier.predict([query])
print(f"Query: '{query}' => Prediction: {prediction[0]}")
Query: 'I lost my password, please help.' => Prediction: password inquiry
Query: 'Can I change my password?' => Prediction: password inquiry
Query: 'I want to open two accounts. Is it possible?' => Prediction: non-password inquiry
Query: 'I want to recover my account.' => Prediction: non-password inquiry