how to have a text base conversation python code example
Example 1: python chatbot speech recognition
import ioimport randomimport stringimport warningsimport numpy as npfrom sklearn.feature_extraction.text import TfidfVectorizerfrom sklearn.metrics.pairwise import cosine_similarityimport warningsfrom gtts import gTTSimport oswarnings.filterwarnings('ignore')import speech_recognition as sr import nltkfrom nltk.stem import WordNetLemmatizer#for downloading package files can be commented after First runnltk.download('popular', quiet=True)nltk.download('nps_chat',quiet=True)nltk.download('punkt') nltk.download('wordnet')
Example 2: python chatbot speech recognition
posts = nltk.corpus.nps_chat.xml_posts()[:10000]# To Recognise input type as QUES. def dialogue_act_features(post): features = {} for word in nltk.word_tokenize(post): features['contains({})'.format(word.lower())] = True return featuresfeaturesets = [(dialogue_act_features(post.text), post.get('class')) for post in posts]size = int(len(featuresets) * 0.1)train_set, test_set = featuresets[size:], featuresets[:size]classifier = nltk.NaiveBayesClassifier.train(train_set)