Commit a306db5d authored by Arunaudayanga's avatar Arunaudayanga

chatbot commit v1

parents
Pipeline #6960 failed with stages
# Default ignored files
/shelf/
/workspace.xml
<?xml version="1.0" encoding="UTF-8"?>
<module type="PYTHON_MODULE" version="4">
<component name="NewModuleRootManager">
<content url="file://$MODULE_DIR$">
<excludeFolder url="file://$MODULE_DIR$/venv" />
</content>
<orderEntry type="jdk" jdkName="Python 3.9 (Child Flask App)" jdkType="Python SDK" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
\ No newline at end of file
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="PyPackageRequirementsInspection" enabled="true" level="WARNING" enabled_by_default="true">
<option name="ignoredPackages">
<value>
<list size="1">
<item index="0" class="java.lang.String" itemvalue="Keras" />
</list>
</value>
</option>
</inspection_tool>
<inspection_tool class="PyPep8NamingInspection" enabled="true" level="WEAK WARNING" enabled_by_default="true">
<option name="ignoredErrors">
<list>
<option value="N806" />
</list>
</option>
</inspection_tool>
</profile>
</component>
\ No newline at end of file
<component name="InspectionProjectProfileManager">
<settings>
<option name="USE_PROJECT_PROFILE" value="false" />
<version value="1.0" />
</settings>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.9 (Child Flask App)" project-jdk-type="Python SDK" />
<component name="PyCharmProfessionalAdvertiser">
<option name="shown" value="true" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/AI Chatbot.iml" filepath="$PROJECT_DIR$/.idea/AI Chatbot.iml" />
</modules>
</component>
</project>
\ No newline at end of file
import webbrowser
import nltk
import requests
nltk.download('popular')
from nltk.stem import WordNetLemmatizer
lemmatizer = WordNetLemmatizer()
import pickle
import numpy as np
from keras.models import load_model
import json
import random
model = load_model('model.h5')
intents = json.loads(open('data.json').read())
words = pickle.load(open('texts.pkl', 'rb'))
classes = pickle.load(open('labels.pkl', 'rb'))
def clean_up_sentence(sentence):
# tokenize the pattern - split words into array
sentence_words = nltk.word_tokenize(sentence)
# stem each word - create short form for word
sentence_words = [lemmatizer.lemmatize(word.lower()) for word in sentence_words]
return sentence_words
# return bag of words array: 0 or 1 for each word in the bag that exists in the sentence
def bow(sentence, words, show_details=True):
# tokenize the pattern
sentence_words = clean_up_sentence(sentence)
# bag of words - matrix of N words, vocabulary matrix
bag = [0] * len(words)
for s in sentence_words:
for i, w in enumerate(words):
if w == s:
# assign 1 if current word is in the vocabulary position
bag[i] = 1
if show_details:
print("found in bag: %s" % w)
return (np.array(bag))
def predict_class(sentence, model):
# filter out predictions below a threshold
p = bow(sentence, words, show_details=False)
res = model.predict(np.array([p]))[0]
ERROR_THRESHOLD = 0.25
results = [[i, r] for i, r in enumerate(res) if r > ERROR_THRESHOLD]
# sort by strength of probability
results.sort(key=lambda x: x[1], reverse=True)
return_list = []
for r in results:
return_list.append({"intent": classes[r[0]], "probability": str(r[1])})
return return_list
def getResponse(ints, intents_json):
tag = ints[0]['intent']
list_of_intents = intents_json['intents']
for i in list_of_intents:
if (i['tag'] == tag):
result = random.choice(i['responses'])
break
return result
def chatbot_response(msg):
ints = predict_class(msg, model)
res = getResponse(ints, intents)
return res
from flask import Flask, render_template, request
from flask import Flask, request, jsonify
import joblib
import pandas as pd
app = Flask(__name__)
app.static_folder = 'static'
# Load the saved model
loaded_model = joblib.load('rf_model_learning_style.pkl')
@app.route("/")
def home():
return render_template("index.html")
@app.route("/learning_style")
def learningStyle():
return render_template("learningStyle.html")
@app.route("/teaching_style")
def teachingStyle():
return render_template("teachingStyle.html")
@app.route("/behavior")
def behavior():
return render_template("behavior.html")
@app.route("/get")
def get_bot_response():
userText = request.args.get('msg')
return chatbot_response(userText)
@app.route('/predict_learning_style', methods=['POST'])
def predict_learning_style():
try:
# Get the input data from the POST request
data = request.json
input_list = data['input']
# Prepare the input data for prediction
input_df = pd.DataFrame([input_list])
# Make predictions using the loaded model
predictions = loaded_model.predict(input_df)
# Define a label mapping
label_mapping = {0: 'Auditory', 1: 'Kinesthetic', 2: 'Read/Write', 3: 'Read/Write', 4: 'Visual'}
# Map the predictions to labels
predicted_labels = [label_mapping[prediction] for prediction in predictions]
print(predicted_labels)
# Return the predicted labels as JSON response
return jsonify({'predicted_labels': predicted_labels})
except Exception as e:
return jsonify({'error': str(e)}), 400
loaded_modelT = joblib.load('predicting_teaching_style.pkl')
@app.route('/predict_teaching_style', methods=['POST'])
def predict_teaching_style():
try:
# Get the input data from the POST request
data = request.json
input_list = data['input']
# Prepare the input data for prediction
input_df = pd.DataFrame([input_list])
# Make predictions using the loaded model
predictions = loaded_modelT.predict(input_df)
# Define a label mapping
label_mapping = {
0: 'HANDS ON EXPERIENCE',
1: 'INDIVIDUALIZED SUPPORT',
2: 'MULTISENSORY INSTRUCTION',
3: 'PLAY BASED LEARNING',
4: 'WHOLE GROUP AND SMALL GROUP INSTRUCTION'
}
# Map the predictions to labels
predicted_labels = [label_mapping[prediction] for prediction in predictions]
# Return the predicted labels as JSON response
return jsonify({'predicted_labels': predicted_labels})
except Exception as e:
return jsonify({'error': str(e)}), 400
loaded_modelB = joblib.load('behaviour_prediction.pkl')
@app.route('/predict_behavior', methods=['POST'])
def predict_behavior():
try:
# Get the input data from the POST request
data = request.json
input_list = data['input']
# Prepare the input data for prediction
input_df = pd.DataFrame([input_list])
# Make predictions using the loaded model
predictions = loaded_modelB.predict(input_df)
# Define a label mapping
label_mapping = {
0: 'Aggressive',
1: 'Hyperactive',
2: 'Oppositional',
3: 'Tantrum',
4: 'Withdrawal'
}
# Map the predictions to labels
predicted_labels = [label_mapping[prediction] for prediction in predictions]
# Return the predicted labels as JSON response
return jsonify({'predicted_labels': predicted_labels})
except Exception as e:
return jsonify({'error': str(e)}), 400
if __name__ == "__main__":
app.run()
{"intents": [
{
"tag": "greeting",
"patterns": ["Hi", "Hello", "Good day", "Hey there"],
"responses": ["Hello! Welcome to the Child Advisor Chatbot. How can I assist you today?",
"Hi there! How can I help you?",
"Good day! What questions do you have for me?"],
"context_set": ""
},
{
"tag": "greetingMore",
"patterns": [ "How are you", "Whats up"],
"responses": ["I am fine!, How about you?", "I am good, you?"],
"context_set": ""
},
{
"tag": "goodbye",
"patterns": ["cya", "Bye", "See you later", "Goodbye", "I am Leaving", "Have a Good day"],
"responses": ["Sad to see you go :(", "Talk to you later", "Goodbye!"],
"context_set": ""
},
{
"tag": "childBehavior",
"patterns": [
"How do I handle tantrums?",
"Dealing with a stubborn child",
"How to discipline a child?",
"Help with child's aggression",
"Managing child's hyperactivity",
"Teaching manners to a child"
],
"responses": [
"Dealing with tantrums can be challenging. It's important to stay calm and offer comfort.",
"Stubbornness is common in children. Try offering choices to give them a sense of control.",
"Discipline should be consistent and focused on teaching rather than punishment.",
"Aggression in children might stem from frustration. Teach them healthy ways to express themselves.",
"Hyperactivity can be managed through regular physical activity and setting clear routines.",
"Teaching manners is an ongoing process. Lead by example and use positive reinforcement."
],
"context_set": ""
},
{
"tag": "childDevelopment",
"patterns": [
"What are typical milestones for a toddler?",
"Child development stages",
"When do children start talking?",
"Signs of a learning disability in kids",
"Helping kids with social skills",
"Encouraging creativity in children"
],
"responses": [
"Toddlers typically start walking around 12-15 months and saying words around 18-24 months.",
"Child development involves physical, cognitive, and emotional growth. Each child is unique.",
"Children usually start talking by the age of 2, but it varies. Encourage language through interaction.",
"Learning disabilities might show in difficulties with reading, writing, or math. Early intervention is key.",
"Social skills can be nurtured through playdates, group activities, and modeling positive interactions.",
"Encourage creativity by providing art supplies, allowing imaginative play, and praising their efforts."
],
"context_set": ""
},
{
"tag": "confidence",
"patterns": [
"How confident are you in identifying and understanding different behavior patterns in nursery students?",
"How well do you think you can identify and understand different behavior patterns in nursery students?",
"What is your level of confidence in identifying and understanding different behavior patterns in nursery students?"
],
"responses": [
"I am very confident in identifying and understanding different behavior patterns in nursery students.",
"I am moderately confident in identifying and understanding different behavior patterns in nursery students.",
"I am not very confident in identifying and understanding different behavior patterns in nursery students."
],
"context_set": ""
},
{
"tag": "importantBehaviors",
"patterns": [
"What are the most important behaviors to address in nursery students?",
"What behaviors do you think are the most important to address in nursery students?",
"What are some of the most important behaviors to address in nursery students?"
],
"responses": [
"The most important behaviors to address in nursery students are aggression, tantrums, withdrawal, hyperactivity, and oppositional behavior.",
"Other important behaviors to address in nursery students include attention deficit, anxiety, and depression.",
"It is important to address all of these behaviors in a timely and effective manner to ensure the healthy development of nursery students."
],
"context_set": ""
},
{
"tag": "behaviorManagement",
"patterns": [
"What are some effective ways to manage behavior in nursery students?",
"How can I manage behavior in nursery students?",
"What are the best practices for managing behavior in nursery students?"
],
"responses": [
"There are many effective ways to manage behavior in nursery students. Some of the most common methods include positive reinforcement, time-out, and redirection.",
"It is important to choose the right method for each individual student and to be consistent with the implementation of the method.",
"It is also important to be patient and understanding when managing behavior in nursery students. They are still learning how to control their emotions and behavior."
],
"context_set": ""
},
{
"tag": "parentalInvolvement",
"patterns": [
"How important is parental involvement in behavior management?",
"How can parents help to manage behavior in nursery students?",
"What can parents do to support behavior management in nursery students?"
],
"responses": [
"Parental involvement is essential for effective behavior management. Parents can help to set clear expectations, provide positive reinforcement, and consistently implement discipline.",
"They can also teach their children coping skills and help them to develop self-control.",
"Parental involvement is one of the most important factors in the success of behavior management."
],
"context_set": ""
},
{
"tag": "activityAggression",
"patterns": [
"What are some activities that can help to reduce aggression in nursery students?",
"How can I help nursery students to express their aggression in a healthy way?",
"What are some games that can help to teach nursery students about conflict resolution?"
],
"responses": [
"Some activities that can help to reduce aggression in nursery students include: \r\n * Building a tower together as a cooperative activity. \r\n * Playing games that involve turn-taking and sharing. \r\n * Teaching children how to express their emotions in a healthy way.\r\n * Providing children with opportunities to exercise and burn off energy. \r\n * Setting clear expectations and consequences for aggressive behavior. \r\n * Ignoring minor aggressive behaviors. \r\n * Providing positive reinforcement for prosocial behavior."
],
"context_set": ""
},
{
"tag": "activityTantrums",
"patterns": [
"What are some activities that can help to calm down nursery students during tantrums?",
"How can I help nursery students to learn how to manage their tantrums?",
"What are some things that I should avoid doing when a nursery student is having a tantrum?"
],
"responses": [
"Some activities that can help to calm down nursery students during tantrums include: \r\n* Taking the child to a quiet place. \r\n * Talking to the child in a calm voice.\r\n * Offering the child a hug or other physical comfort.\r\n * Practicing deep breathing exercises or simple yoga poses.\r\n * Singing a calming song.\r\n * Reading a soothing story. \r\n * Setting clear expectations and consequences for tantrums.",
"It is important to help nursery students to learn how to manage their tantrums. Some ways to do this include: \r\n * Setting clear expectations and consequences for tantrums. \r\n * Ignoring minor tantrums. \r\n * Providing positive reinforcement for prosocial behavior. \r\n * Teaching children how to express their emotions in a healthy way. \r\n * Teaching children how to calm themselves down when they are feeling angry or upset.",
"There are some things that you should avoid doing when a nursery student is having a tantrum. These include: * Yelling at the child. \r\n* Ignoring the child.\r\n * Arguing with the child.\r\n * Trying to reason with the child."
],
"context_set": ""
},
{
"tag": "activityWithdrawal",
"patterns": [
"What are some activities that can help to encourage withdrawn children to engage with their peers?",
"How can I help nursery students who are withdrawn to make friends?",
"What are some games that can be helpful for withdrawn children?"
],
"responses": [
"Some activities that can help to encourage withdrawn children to engage with their peers include: \r\n * Offering one-on-one activities with a trusted teacher or mentor.\r\n * Providing opportunities for children to work together on projects or activities.\r\n * Playing games that involve turn-taking and cooperation.\r\n * Encouraging children to join clubs or groups that interest them.\r\n * Praising children when they interact with their peers in a positive way.\r\n * Being patient and understanding, as it may take time for withdrawn children to warm up to others.",
"It is important to be patient and understanding when working with withdrawn children. It may take time for them to warm up to others and start to engage in social activities. It is also important to provide them with opportunities to interact with their peers in a safe and supportive environment."
],
"context_set": ""
},
{
"tag": "activitySuggestion",
"patterns": [
"Please provide an activity that you find effective in addressing aggression in nursery students.",
"Please provide an activity that you find effective in helping children calm down during tantrums.",
"Please provide an activity that you find effective in encouraging withdrawn children to engage with their peers.",
"Please provide an activity that you find effective in managing hyperactive children and promoting their focus.",
"Please provide an activity that you find effective in addressing oppositional behavior and encouraging compliance with rules."
],
"responses": ["Thank you for your input! Your suggested activity will be considered for promoting positive behavior in nursery students."],
"context_set": ""
},
{
"tag": "durationExperience",
"patterns": [
"How long have you been working with nursery students?",
"What is your level of experience with nursery students?",
"Can you share your background in working with young children?"
],
"responses": [
"I have been working with nursery students for less than 1 year.",
"My experience with nursery students spans less than a year, but I'm continuously learning and growing in this field.",
"I'm relatively new to working with nursery students, and I'm enthusiastic about gaining more experience."
],
"context_set": ""
},
{
"tag": "activityEffective",
"patterns": [
"Which of the provided activities have you found to be the most effective?",
"Can you recommend an activity that has consistently yielded positive results?",
"What activity has shown the best outcomes in your experience?"
],
"responses": [
"I have found the activity of introducing collaborative storytelling or group art projects to be particularly effective.",
"In my experience, activities like collaborative storytelling and group art projects have consistently shown positive outcomes with nursery students.",
"The activity of engaging nursery students in collaborative storytelling or group art projects has proven to be highly effective in promoting their creativity and social skills."
],
"context_set": ""
},
{
"tag": "activityRecommendation",
"patterns": [
"Can you recommend an activity to improve social skills in nursery students?",
"What are some activities that can help nursery students develop better social skills?",
"How can I encourage teamwork and social interaction among nursery students?"
],
"responses": [
"Certainly! Introducing cooperative games or role-playing scenarios can help improve social skills in nursery students.",
"To enhance social skills in nursery students, consider activities like group storytelling or collaborative art projects, which encourage interaction and teamwork.",
"You can promote teamwork and social interaction among nursery students by organizing activities such as group puzzles, team-building games, or group art projects."
],
"context_set": ""
},
{
"tag": "challengingBehaviors",
"patterns": [
"What strategies would you suggest for addressing challenging behaviors in nursery students?",
"How can I handle aggressive behavior in nursery students?",
"What should I do when a nursery student exhibits defiant behavior?"
],
"responses": [
"Addressing challenging behaviors requires patience and consistency. Using positive reinforcement, clear communication, and providing alternatives can be effective strategies.",
"When dealing with aggressive behavior in nursery students, it's important to remain calm, set clear boundaries, and teach them alternative ways to express their emotions.",
"In cases of defiant behavior, consider providing choices within limits, offering positive incentives for cooperation, and maintaining a predictable daily routine to help manage and reduce defiance."
],
"context_set": ""
},
{
"tag": "parentCommunication",
"patterns": [
"How do you recommend communicating with parents about their child's behavior?",
"What are some effective ways to discuss behavioral concerns with parents?",
"Can you provide tips for maintaining a positive and constructive dialogue with parents regarding their child's behavior?"
],
"responses": [
"Open and respectful communication is key when discussing a child's behavior with parents. Start by sharing your observations and concerns in a non-judgmental manner.",
"To maintain a positive dialogue with parents about their child's behavior, actively listen to their perspective, ask for their input, and collaborate on strategies for improvement.",
"It's important to involve parents in the process of addressing their child's behavior. Encourage them to share their insights and work together to develop effective solutions."
],
"context_set": ""
}
]
}
\ No newline at end of file
File added
File added
:root {
--body-bg: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
--msger-bg: #fff;
--border: 2px solid #ddd;
--left-msg-bg: #ececec;
--right-msg-bg: #579ffb;
}
html {
box-sizing: border-box;
}
*,
*:before,
*:after {
margin: 0;
padding: 0;
box-sizing: inherit;
}
body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-image: var(--body-bg);
font-family: Helvetica, sans-serif;
}
.msger {
display: flex;
flex-flow: column wrap;
justify-content: space-between;
width: 100%;
max-width: 867px;
margin: 25px 10px;
height: calc(100% - 50px);
border: var(--border);
border-radius: 5px;
background: var(--msger-bg);
box-shadow: 0 15px 15px -5px rgba(0, 0, 0, 0.2);
}
.msger-header {
/* display: flex; */
font-size: medium;
justify-content: space-between;
padding: 10px;
text-align: center;
border-bottom: var(--border);
background: #eee;
color: #666;
}
.msger-chat {
flex: 1;
overflow-y: auto;
padding: 10px;
}
.msger-chat::-webkit-scrollbar {
width: 6px;
}
.msger-chat::-webkit-scrollbar-track {
background: #ddd;
}
.msger-chat::-webkit-scrollbar-thumb {
background: #bdbdbd;
}
.msg {
display: flex;
align-items: flex-end;
margin-bottom: 10px;
}
.msg-img {
width: 50px;
height: 50px;
margin-right: 10px;
background: #ddd;
background-repeat: no-repeat;
background-position: center;
background-size: cover;
border-radius: 50%;
}
.msg-bubble {
max-width: 450px;
padding: 15px;
border-radius: 15px;
background: var(--left-msg-bg);
}
.msg-info {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 10px;
}
.msg-info-name {
margin-right: 10px;
font-weight: bold;
}
.msg-info-time {
font-size: 0.85em;
}
.left-msg .msg-bubble {
border-bottom-left-radius: 0;
}
.right-msg {
flex-direction: row-reverse;
}
.right-msg .msg-bubble {
background: var(--right-msg-bg);
color: #fff;
border-bottom-right-radius: 0;
}
.right-msg .msg-img {
margin: 0 0 0 10px;
}
.msger-inputarea {
display: flex;
padding: 10px;
border-top: var(--border);
background: #eee;
}
.msger-inputarea * {
padding: 10px;
border: none;
border-radius: 3px;
font-size: 1em;
}
.msger-input {
flex: 1;
background: #ddd;
}
.msger-send-btn {
margin-left: 10px;
background-color: red;
color: #fff;
font-weight: bold;
cursor: pointer;
transition: background 0.23s;
}
.msger-send-btn:hover {
background: rgb(0, 180, 50);
}
.msger-chat {
background-color: #fcfcfe;
}
\ No newline at end of file
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Chatbot</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="{{ url_for('static', filename='styles/style.css') }}">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
</head>
<body>
<!-- partial:index.partial.html -->
<section class="msger">
<header class="msger-header">
<div class="msger-header-title">
<i class="fas fa-bug"></i> Chatbot <i class="fas fa-bug"></i>
</div>
</header>
<main class="msger-chat">
<div class="msg left-msg">
<div class="msg-img"></div>
<div class="msg-bubble">
<div class="msg-info">
<div class="msg-info-name">Chatbot</div>
<div class="msg-info-time">12:45</div>
</div>
<div class="msg-text">
Hi, welcome to ChatBot! Go ahead and send me a message. 😄
</div>
</div>
</div>
</main>
<form class="msger-inputarea">
<input type="text" class="msger-input" id="textInput" placeholder="Enter your message...">
<button type="submit" class="msger-send-btn">Send</button>
</form>
</section>
<!-- partial -->
<script src='https://use.fontawesome.com/releases/v5.0.13/js/all.js'></script>
<script>
const msgerForm = get(".msger-inputarea");
const msgerInput = get(".msger-input");
const msgerChat = get(".msger-chat");
// Icons made by Freepik from www.flaticon.com
const BOT_IMG = "https://image.flaticon.com/icons/svg/327/327779.svg";
const PERSON_IMG = "https://image.flaticon.com/icons/svg/145/145867.svg";
const BOT_NAME = " ChatBot";
const PERSON_NAME = "You";
msgerForm.addEventListener("submit", event => {
event.preventDefault();
const msgText = msgerInput.value;
if (!msgText) return;
appendMessage(PERSON_NAME, PERSON_IMG, "right", msgText);
msgerInput.value = "";
botResponse(msgText);
});
function appendMessage(name, img, side, text) {
// Simple solution for small apps
const msgHTML = `
<div class="msg ${side}-msg">
<div class="msg-img" style="background-image: url(${img})"></div>
<div class="msg-bubble">
<div class="msg-info">
<div class="msg-info-name">${name}</div>
<div class="msg-info-time">${formatDate(new Date())}</div>
</div>
<div class="msg-text">${text}</div>
</div>
</div>
`;
msgerChat.insertAdjacentHTML("beforeend", msgHTML);
msgerChat.scrollTop += 500;
}
function botResponse(rawText) {
// Bot Response
$.get("/get", { msg: rawText }).done(function (data) {
console.log(rawText);
console.log(data);
const msgText = data;
appendMessage(BOT_NAME, BOT_IMG, "left", msgText);
});
}
// Utils
function get(selector, root = document) {
return root.querySelector(selector);
}
function formatDate(date) {
const h = "0" + date.getHours();
const m = "0" + date.getMinutes();
return `${h.slice(-2)}:${m.slice(-2)}`;
}
</script>
</body>
</html>
\ No newline at end of file
File added
import nltk
from nltk.stem import WordNetLemmatizer
lemmatizer = WordNetLemmatizer()
import json
import pickle
import numpy as np
from keras.models import Sequential
from keras.layers import Dense, Activation, Dropout
from keras.optimizers import SGD
import random
words = []
classes = []
documents = []
ignore_words = ['?', '!']
data_file = open('data.json').read()
intents = json.loads(data_file)
for intent in intents['intents']:
for pattern in intent['patterns']:
# tokenize each word
w = nltk.word_tokenize(pattern)
words.extend(w)
# add documents in the corpus
documents.append((w, intent['tag']))
# add to our classes list
if intent['tag'] not in classes:
classes.append(intent['tag'])
# lemmaztize and lower each word and remove duplicates
words = [lemmatizer.lemmatize(w.lower()) for w in words if w not in ignore_words]
words = sorted(list(set(words)))
# sort classes
classes = sorted(list(set(classes)))
# documents = combination between patterns and intents
print(len(documents), "documents")
# classes = intents
print(len(classes), "classes", classes)
# words = all words, vocabulary
print(len(words), "unique lemmatized words", words)
pickle.dump(words, open('texts.pkl', 'wb'))
pickle.dump(classes, open('labels.pkl', 'wb'))
# create our training data
training = []
# create an empty array for our output
output_empty = [0] * len(classes)
# training set, bag of words for each sentence
for doc in documents:
# initialize our bag of words
bag = []
# list of tokenized words for the pattern
pattern_words = doc[0]
# lemmatize each word - create base word, in attempt to represent related words
pattern_words = [lemmatizer.lemmatize(word.lower()) for word in pattern_words]
# create our bag of words array with 1, if word match found in current pattern
for w in words:
bag.append(1) if w in pattern_words else bag.append(0)
# output is a '0' for each tag and '1' for current tag (for each pattern)
output_row = list(output_empty)
output_row[classes.index(doc[1])] = 1
training.append([bag, output_row])
# shuffle our features and turn into np.array
random.shuffle(training)
training = np.array(training)
# create train and test lists. X - patterns, Y - intents
train_x = list(training[:, 0])
train_y = list(training[:, 1])
print("Training data created")
# Create model - 3 layers. First layer 128 neurons, second layer 64 neurons and 3rd output layer contains number of neurons
# equal to number of intents to predict output intent with softmax
model = Sequential()
model.add(Dense(128, input_shape=(len(train_x[0]),), activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(64, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(len(train_y[0]), activation='softmax'))
# Compile model. Stochastic gradient descent with Nesterov accelerated gradient gives good results for this model
sgd = SGD(lr=0.01, decay=1e-6, momentum=0.9, nesterov=True)
model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
# fitting and saving the model
hist = model.fit(np.array(train_x), np.array(train_y), epochs=200, batch_size=5, verbose=1)
model.save('model.h5', hist)
print("model created")
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment