635 lines
21 KiB
Plaintext
635 lines
21 KiB
Plaintext
|
{
|
||
|
"nbformat": 4,
|
||
|
"nbformat_minor": 0,
|
||
|
"metadata": {
|
||
|
"colab": {
|
||
|
"name": "DL_Chatbot_ver_1_0.ipynb",
|
||
|
"provenance": [],
|
||
|
"toc_visible": true
|
||
|
},
|
||
|
"kernelspec": {
|
||
|
"name": "python3",
|
||
|
"display_name": "Python 3"
|
||
|
}
|
||
|
},
|
||
|
"cells": [
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {
|
||
|
"id": "HxtCFj1hfXw6"
|
||
|
},
|
||
|
"source": [
|
||
|
"# 0. Instalacja i importowanie modułów"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {
|
||
|
"id": "enDE5aTIgN-v"
|
||
|
},
|
||
|
"source": [
|
||
|
"##### 0.1. Ogólne"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"metadata": {
|
||
|
"id": "D7_8XDfpfH-X"
|
||
|
},
|
||
|
"source": [
|
||
|
"!pip install tflearn\r\n",
|
||
|
"!pip install tensorflow"
|
||
|
],
|
||
|
"execution_count": null,
|
||
|
"outputs": []
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"metadata": {
|
||
|
"id": "GOGs4hL6fwwK"
|
||
|
},
|
||
|
"source": [
|
||
|
"import numpy as np\r\n",
|
||
|
"import tflearn\r\n",
|
||
|
"import tensorflow\r\n",
|
||
|
"import random\r\n",
|
||
|
"import json"
|
||
|
],
|
||
|
"execution_count": null,
|
||
|
"outputs": []
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {
|
||
|
"id": "Mr0ZD1L2gCWw"
|
||
|
},
|
||
|
"source": [
|
||
|
"##### 0.2. Angielski Stemmer: https://www.nltk.org/_modules/nltk/stem/lancaster.html"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"metadata": {
|
||
|
"id": "jy4-9guXgBY3"
|
||
|
},
|
||
|
"source": [
|
||
|
"import nltk\r\n",
|
||
|
"nltk.download('punkt')\r\n",
|
||
|
"from nltk.stem.lancaster import LancasterStemmer\r\n",
|
||
|
"stemmer_en = LancasterStemmer()"
|
||
|
],
|
||
|
"execution_count": null,
|
||
|
"outputs": []
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {
|
||
|
"id": "uPpcNQa_ggUl"
|
||
|
},
|
||
|
"source": [
|
||
|
"##### 0.3. Polski Stemmer **(Docelowy)**: https://pypi.org/project/pystempel/"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"metadata": {
|
||
|
"id": "YgtIHwpKgBR4"
|
||
|
},
|
||
|
"source": [
|
||
|
"!pip install pystempel"
|
||
|
],
|
||
|
"execution_count": null,
|
||
|
"outputs": []
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"metadata": {
|
||
|
"id": "XBpvJXn1gBDi"
|
||
|
},
|
||
|
"source": [
|
||
|
"from stempel import StempelStemmer\r\n",
|
||
|
"\r\n",
|
||
|
"stemmer_pl = StempelStemmer.default() #może wersja \".polimorf()\" jest lepsza?"
|
||
|
],
|
||
|
"execution_count": null,
|
||
|
"outputs": []
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {
|
||
|
"id": "Lg_3MO_3hQV_"
|
||
|
},
|
||
|
"source": [
|
||
|
"# 1. Załadowanie plików **.json** z bazą słów"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {
|
||
|
"id": "BzBo1657hn3w"
|
||
|
},
|
||
|
"source": [
|
||
|
"##### 1.1. Docelowa baza słów polskich do nauki modelu (10 rodzajów odp - PL)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"metadata": {
|
||
|
"colab": {
|
||
|
"base_uri": "https://localhost:8080/"
|
||
|
},
|
||
|
"id": "jKsIW7hHhepB",
|
||
|
"outputId": "09ba1cb1-bb0e-44ee-9d28-017209902934"
|
||
|
},
|
||
|
"source": [
|
||
|
"with open(\"intents_pl.json\") as file:\r\n",
|
||
|
" data_pl = json.load(file)\r\n",
|
||
|
"\r\n",
|
||
|
"print(data_pl)"
|
||
|
],
|
||
|
"execution_count": 9,
|
||
|
"outputs": [
|
||
|
{
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"{'intents': [{'tag': 'greeting', 'patterns': ['Cześć', 'Elo', 'Jesteś?', 'Hej', 'Dzień dobry', 'Sup'], 'responses': ['Cześć!', 'Dobrze Cię widzieć!', 'Hej, w czym mogę pomóc?'], 'context_set': ''}, {'tag': 'goodbye', 'patterns': ['narazie', 'Do zobaczenia', 'Dowidzenia', 'Dobranoc', 'Miłego dnia'], 'responses': ['Do zobaczenia później', 'Mam nadzieję, że później pogadamy', 'Narazie!'], 'context_set': ''}, {'tag': 'age', 'patterns': ['Ile masz lat', 'Ile lat ma Janet', 'Wiek', 'Jak stara jesteś', 'urodziny'], 'responses': ['Mam kilka dni', 'Urodziłam się 17.03.2021'], 'context_set': ''}, {'tag': 'name', 'patterns': ['Jak masz na imię', 'Jak Cię zwą?', 'twoje imie?', 'Imie', 'Jak cię nazywać', 'Kim jesteś'], 'responses': ['Możesz mnie nazywać Janet!', 'Jestem Janet', 'Jestem Janet, twój ulubiony chatbot'], 'context_set': ''}, {'tag': 'goout', 'patterns': ['Czy chcesz gdzieś wyjść?', 'zrobimy coś razem?', 'pójdziemy gdzieś razem?'], 'responses': ['Może kiedy indziej', 'Odezwę się latem'], 'context_set': ''}, {'tag': 'doing', 'patterns': ['Co robisz teraz?', 'co słychać', 'jakie masz plany?', 'jak się masz?'], 'responses': ['Gram w grę', 'Słucham muzyki', 'nie twój interes', 'nie mam czasu odpowiadać', ''], 'context_set': ''}, {'tag': 'game', 'patterns': ['a w co grasz?', 'a w co?', 'grasz?'], 'responses': ['nie interesuj się', 'a co cię to obchodzi', '...', 'w coś', 'W OSRS'], 'context_set': ''}, {'tag': 'music', 'patterns': ['czego?', 'czego słuchasz?', 'jakiej muzyki?'], 'responses': ['Starego vinyla z 1995 roku', 'mojego ulubionego setu rejwowego', 'czegoś tam...'], 'context_set': ''}, {'tag': 'angry', 'patterns': ['bo co?', 'dlaczego?', 'jak to?'], 'responses': ['Czas zakończyć rozmowę', 'nie mam na to siły i czasu', 'zostaw mnie w spokoju'], 'context_set': ''}, {'tag': 'why', 'patterns': ['o co chodzi?', 'czemu jesteś zła', 'poczekaj'], 'responses': ['...', 'Nie pisz do mnie więcej', 'Odezwę się latem'], 'context_set': ''}]}\n"
|
||
|
],
|
||
|
"name": "stdout"
|
||
|
}
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {
|
||
|
"id": "g94eHpqshoat"
|
||
|
},
|
||
|
"source": [
|
||
|
"##### 1.2. Skrócona baza słów (4 rodzaje odp - PL)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"metadata": {
|
||
|
"colab": {
|
||
|
"base_uri": "https://localhost:8080/"
|
||
|
},
|
||
|
"id": "gJbm_CtRhNOK",
|
||
|
"outputId": "157196fc-6a25-4a70-aca3-9d886c743f6c"
|
||
|
},
|
||
|
"source": [
|
||
|
"with open(\"intents_pl_short.json\") as file:\r\n",
|
||
|
" data_pl_short = json.load(file)\r\n",
|
||
|
"\r\n",
|
||
|
"print(data_pl_short)"
|
||
|
],
|
||
|
"execution_count": 10,
|
||
|
"outputs": [
|
||
|
{
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"{'intents': [{'tag': 'greeting', 'patterns': ['Cześć', 'Elo', 'Jesteś?', 'Hej', 'Dzień dobry', 'Sup'], 'responses': ['Cześć!', 'Dobrze Cię widzieć!', 'Hej, w czym mogę pomóc?'], 'context_set': ''}, {'tag': 'goodbye', 'patterns': ['narazie', 'Do zobaczenia', 'Dowidzenia', 'Dobranoc', 'Miłego dnia'], 'responses': ['Do zobaczenia później', 'Mam nadzieję, że później pogadamy', 'Narazie!'], 'context_set': ''}, {'tag': 'age', 'patterns': ['Ile masz lat', 'Ile lat ma Janet', 'Wiek', 'Jak stara jesteś', 'urodziny'], 'responses': ['Mam kilka dni', 'Urodziłam się 17.03.2021'], 'context_set': ''}, {'tag': 'name', 'patterns': ['Jak masz na imię', 'Jak Cię zwą?', 'twoje imie?', 'Imie', 'Jak cię nazywać', 'Kim jesteś'], 'responses': ['Możesz mnie nazywać Janet!', 'Jestem Janet', 'Jestem Janet, twój ulubiony chatbot'], 'context_set': ''}]}\n"
|
||
|
],
|
||
|
"name": "stdout"
|
||
|
}
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {
|
||
|
"id": "HjkIUMy2ho6C"
|
||
|
},
|
||
|
"source": [
|
||
|
"##### 1.3. Testowa baza słów angielskich (6 rodzajów odp - EN)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"metadata": {
|
||
|
"colab": {
|
||
|
"base_uri": "https://localhost:8080/"
|
||
|
},
|
||
|
"id": "vW5FyoRqhfIc",
|
||
|
"outputId": "378d8894-9c9c-46be-ade1-b6491f095179"
|
||
|
},
|
||
|
"source": [
|
||
|
"with open(\"intents_en.json\") as file:\r\n",
|
||
|
" data_en = json.load(file)\r\n",
|
||
|
"\r\n",
|
||
|
"print(data_en)"
|
||
|
],
|
||
|
"execution_count": 11,
|
||
|
"outputs": [
|
||
|
{
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"{'intents': [{'tag': 'greeting', 'patterns': ['Hi', 'How are you', 'Is anyone there?', 'Hello', 'Good day', 'Whats up'], 'responses': ['Hello!', 'Good to see you again!', 'Hi there, how can I help?'], 'context_set': ''}, {'tag': 'goodbye', 'patterns': ['cya', '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': 'age', 'patterns': ['how old', 'how old is tim', 'what is your age', 'how old are you', 'age?'], 'responses': ['I am 18 years old!', '18 years young!'], 'context_set': ''}, {'tag': 'name', 'patterns': ['what is your name', 'what should I call you', 'whats your name?'], 'responses': ['You can call me Tim.', \"I'm Tim!\", \"I'm Tim aka Tech With Tim.\"], 'context_set': ''}, {'tag': 'shop', 'patterns': ['Id like to buy something', 'whats on the menu', 'what do you reccommend?', 'could i get something to eat'], 'responses': ['We sell chocolate chip cookies for $2!', 'Cookies are on the menu!'], 'context_set': ''}, {'tag': 'hours', 'patterns': ['when are you guys open', 'what are your hours', 'hours of operation'], 'responses': ['We are open 7am-4pm Monday-Friday!'], 'context_set': ''}]}\n"
|
||
|
],
|
||
|
"name": "stdout"
|
||
|
}
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {
|
||
|
"id": "4BnsOkqqjBlr"
|
||
|
},
|
||
|
"source": [
|
||
|
"# 2. Przygotowanie danych do nauki modelu"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"metadata": {
|
||
|
"id": "gy6p55-DjLyY"
|
||
|
},
|
||
|
"source": [
|
||
|
"words = []\r\n",
|
||
|
"labels = []\r\n",
|
||
|
"docs_x = []\r\n",
|
||
|
"docs_y = []"
|
||
|
],
|
||
|
"execution_count": 12,
|
||
|
"outputs": []
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {
|
||
|
"id": "XxZX-JQA5zjL"
|
||
|
},
|
||
|
"source": [
|
||
|
"##### 2.1 Stworzenie tablicy ze wszystkimi możliwymi inputami użytkownika (+ labele)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"metadata": {
|
||
|
"id": "nBUKwSr_kVSd"
|
||
|
},
|
||
|
"source": [
|
||
|
"for intent in data_pl_short[\"intents\"]: #Loop przez cały json\r\n",
|
||
|
" for pattern in intent[\"patterns\"]: #loop przez wszystkie możliwe rodzaje przykładowego inputu użytkownika\r\n",
|
||
|
" wrds = nltk.word_tokenize(pattern) #Tokenizing every word\r\n",
|
||
|
" words.extend(wrds) #Add every single tokenized word\r\n",
|
||
|
" docs_x.append(wrds) #Add the whole tokenized sentence\r\n",
|
||
|
" docs_y.append(intent[\"tag\"]) #Pattern x coresponds to the tag y. Potrzebne do ustalenia relacji słowa z odpowiedzią\r\n",
|
||
|
"\r\n",
|
||
|
" if intent[\"tag\"] not in labels:\r\n",
|
||
|
" labels.append(intent[\"tag\"]) #Add the tag"
|
||
|
],
|
||
|
"execution_count": 13,
|
||
|
"outputs": []
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"metadata": {
|
||
|
"id": "wOyP5lbikV1e"
|
||
|
},
|
||
|
"source": [
|
||
|
"words = [stemmer_pl.stem(w.lower()) for w in words if w not in \"?\"] #stemming -> take each word and bring it to the \"root\" form. Only the stemmed version of the word is important to us\r\n",
|
||
|
"words = sorted(list(set(words))) #Sorting\r\n",
|
||
|
"\r\n",
|
||
|
"labels = sorted(labels) #sorting\r\n",
|
||
|
"\r\n",
|
||
|
"training = []\r\n",
|
||
|
"output = []\r\n",
|
||
|
"\r\n",
|
||
|
"out_empty = [0 for _ in range(len(labels))]"
|
||
|
],
|
||
|
"execution_count": 14,
|
||
|
"outputs": []
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {
|
||
|
"id": "WewUeunf5_Za"
|
||
|
},
|
||
|
"source": [
|
||
|
"##### 3.2. Przypisywanie słów do danej kategorii (ie. \"Cześć\" do Greetings)"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {
|
||
|
"id": "1Q43_qtZ6KNP"
|
||
|
},
|
||
|
"source": [
|
||
|
"W przypadku data_pl_short są tylko 4 rodzaje odpowiedzi. \"Cześć\" które zostane przypisane do labela \"greeting\" będzie miało formę końcowego outputu \"1000\" jeżeli label \"greetings\" jest pierwszy do wyboru."
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {
|
||
|
"id": "doFER5OS7CC_"
|
||
|
},
|
||
|
"source": [
|
||
|
"Warto też dodać, że sieć neuronowa nie przyjmuje teksu. To jest główny powód czemu przypisujemy słowa do kategorii"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"metadata": {
|
||
|
"id": "8FDKrjpjkYsE"
|
||
|
},
|
||
|
"source": [
|
||
|
"for x, doc in enumerate(docs_x): #Przejście przez wszystkie słowa\r\n",
|
||
|
" bag =[]\r\n",
|
||
|
"\r\n",
|
||
|
" wrds = [stemmer_pl.stem(w) for w in doc] #podział wszystkich słów w danym zdaniu\r\n",
|
||
|
"\r\n",
|
||
|
" for w in words:\r\n",
|
||
|
" if w in wrds:\r\n",
|
||
|
" bag.append(1) #this word exist\r\n",
|
||
|
" else:\r\n",
|
||
|
" bag.append(0) #do not exist\r\n",
|
||
|
" \r\n",
|
||
|
" output_row = out_empty[:] #kopia\r\n",
|
||
|
" output_row[labels.index(docs_y[x])] = 1\r\n",
|
||
|
"\r\n",
|
||
|
" training.append(bag) #dodajemy nowe wyrażenie zamienione na ciąg binarny\r\n",
|
||
|
" output.append(output_row)\r\n",
|
||
|
"\r\n",
|
||
|
"training = np.array(training) #Zbiór treningowy\r\n",
|
||
|
"output = np.array(output) #Zbiór outputów"
|
||
|
],
|
||
|
"execution_count": 16,
|
||
|
"outputs": []
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"metadata": {
|
||
|
"colab": {
|
||
|
"base_uri": "https://localhost:8080/"
|
||
|
},
|
||
|
"id": "cJKUjbkC72-f",
|
||
|
"outputId": "7e2bff96-78ce-49ff-b27b-eee77752228d"
|
||
|
},
|
||
|
"source": [
|
||
|
"len(training) #dla pl_short mamy 44 słowa"
|
||
|
],
|
||
|
"execution_count": 18,
|
||
|
"outputs": [
|
||
|
{
|
||
|
"output_type": "execute_result",
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"44"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {
|
||
|
"tags": []
|
||
|
},
|
||
|
"execution_count": 18
|
||
|
}
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"metadata": {
|
||
|
"colab": {
|
||
|
"base_uri": "https://localhost:8080/"
|
||
|
},
|
||
|
"id": "Kx43VDgS7-yN",
|
||
|
"outputId": "4fa6f6fe-dc58-4e76-bb26-38c1784ab79c"
|
||
|
},
|
||
|
"source": [
|
||
|
"len(output[0]) #Które można przypisać do 4 kategorii"
|
||
|
],
|
||
|
"execution_count": 20,
|
||
|
"outputs": [
|
||
|
{
|
||
|
"output_type": "execute_result",
|
||
|
"data": {
|
||
|
"text/plain": [
|
||
|
"4"
|
||
|
]
|
||
|
},
|
||
|
"metadata": {
|
||
|
"tags": []
|
||
|
},
|
||
|
"execution_count": 20
|
||
|
}
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {
|
||
|
"id": "yCFKTbjZ12wh"
|
||
|
},
|
||
|
"source": [
|
||
|
"# 3. Model i jego ćwiczenie"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"metadata": {
|
||
|
"id": "MDA435sI1-Xl"
|
||
|
},
|
||
|
"source": [
|
||
|
"training = np.array(training) #zamiana typu dla sieci neuronowej\r\n",
|
||
|
"output = np.array(output) #zamiana typu dla sieci neuronowej"
|
||
|
],
|
||
|
"execution_count": 22,
|
||
|
"outputs": []
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {
|
||
|
"id": "SvBURQCc3PBj"
|
||
|
},
|
||
|
"source": [
|
||
|
"##### 3.1. Stworzenie DLN i inicjacja modelu"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"metadata": {
|
||
|
"colab": {
|
||
|
"base_uri": "https://localhost:8080/"
|
||
|
},
|
||
|
"id": "XaQJh1aG2hbj",
|
||
|
"outputId": "80420df0-3a78-4583-9563-2165e968713d"
|
||
|
},
|
||
|
"source": [
|
||
|
"tensorflow.compat.v1.reset_default_graph() #Reset na wszelki wypadek (w sumie nie wiem czy to jakaś super ważna linijka kodu)\r\n",
|
||
|
"\r\n",
|
||
|
"net = tflearn.input_data(shape=[None, len(training[0])]) #Input layer\r\n",
|
||
|
"net = tflearn.fully_connected(net, 8) #8 neurons for hidden layer\r\n",
|
||
|
"net = tflearn.fully_connected(net, 8) #8 neurons for hidden layer\r\n",
|
||
|
"#net = tflearn.fully_connected(net, 8) #8 neurons for hidden layer\r\n",
|
||
|
"net = tflearn.fully_connected(net, len(output[0]), activation=\"softmax\") #len(output) neurons for output layer + Softmax jako najlepsze wyjście dla tego typu danych\r\n",
|
||
|
"net = tflearn.regression(net)\r\n",
|
||
|
"\r\n",
|
||
|
"model = tflearn.DNN(net)\r\n",
|
||
|
"\r\n"
|
||
|
],
|
||
|
"execution_count": 23,
|
||
|
"outputs": [
|
||
|
{
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"WARNING:tensorflow:From /usr/local/lib/python3.7/dist-packages/tflearn/initializations.py:165: calling TruncatedNormal.__init__ (from tensorflow.python.ops.init_ops) with dtype is deprecated and will be removed in a future version.\n",
|
||
|
"Instructions for updating:\n",
|
||
|
"Call initializer instance with the dtype argument instead of passing it to the constructor\n"
|
||
|
],
|
||
|
"name": "stdout"
|
||
|
}
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {
|
||
|
"id": "Ktd1OcBa3PmQ"
|
||
|
},
|
||
|
"source": [
|
||
|
"##### 3.2. Trening Modelu"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"metadata": {
|
||
|
"colab": {
|
||
|
"base_uri": "https://localhost:8080/"
|
||
|
},
|
||
|
"id": "REzkJL_r2hwl",
|
||
|
"outputId": "7ab2b0c5-944f-4e22-d478-1e35b41f87db"
|
||
|
},
|
||
|
"source": [
|
||
|
"model.fit(training, output, n_epoch=1000, batch_size=8, show_metric=True)\r\n",
|
||
|
"\r\n",
|
||
|
"#Zapis Modelu\r\n",
|
||
|
"#model.save(\"model.tflearn\")"
|
||
|
],
|
||
|
"execution_count": 24,
|
||
|
"outputs": [
|
||
|
{
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"Training Step: 5999 | total loss: \u001b[1m\u001b[32m0.46985\u001b[0m\u001b[0m | time: 0.036s\n",
|
||
|
"| Adam | epoch: 1000 | loss: 0.46985 - acc: 0.7877 -- iter: 40/44\n",
|
||
|
"Training Step: 6000 | total loss: \u001b[1m\u001b[32m0.47602\u001b[0m\u001b[0m | time: 0.046s\n",
|
||
|
"| Adam | epoch: 1000 | loss: 0.47602 - acc: 0.7839 -- iter: 44/44\n",
|
||
|
"--\n"
|
||
|
],
|
||
|
"name": "stdout"
|
||
|
}
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {
|
||
|
"id": "G-L6TV_63iYs"
|
||
|
},
|
||
|
"source": [
|
||
|
"# 4. Input Użytkownika"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {
|
||
|
"id": "c6UvIrWu-a38"
|
||
|
},
|
||
|
"source": [
|
||
|
"##### 4.1 Funkcja **\"bag_of_words(s, words)\"** do stemmowania twojego zdania, i przypisania mu formy binarnej"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"metadata": {
|
||
|
"id": "1IQyV1v33lC7"
|
||
|
},
|
||
|
"source": [
|
||
|
"def bag_of_words(s, words):\r\n",
|
||
|
" bag = [0 for _ in range(len(words))]\r\n",
|
||
|
"\r\n",
|
||
|
" s_words = nltk.word_tokenize(s)\r\n",
|
||
|
" s_words = [stemmer_pl.stem(word.lower()) for word in s_words]\r\n",
|
||
|
"\r\n",
|
||
|
" for se in s_words:\r\n",
|
||
|
" for i, w in enumerate(words):\r\n",
|
||
|
" if w == se:\r\n",
|
||
|
" bag[i] = 1\r\n",
|
||
|
" return np.array(bag)"
|
||
|
],
|
||
|
"execution_count": 26,
|
||
|
"outputs": []
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {
|
||
|
"id": "rXq-wj-F-5DE"
|
||
|
},
|
||
|
"source": [
|
||
|
"##### 4.2 Funkcja **\"chat()\"** do rozmowy z botem"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"metadata": {
|
||
|
"id": "Je6OSZ679-KL"
|
||
|
},
|
||
|
"source": [
|
||
|
"def chat():\r\n",
|
||
|
" print(\"Możesz rozpocząć rozmowę z Botem! (type quit to stop)\")\r\n",
|
||
|
" while True: #Ciągła rozmowa\r\n",
|
||
|
" inp = input(\"Ty: \")\r\n",
|
||
|
" if inp.lower() == \"quit\": #Quit by wyjść z loopa\r\n",
|
||
|
" break\r\n",
|
||
|
"\r\n",
|
||
|
" result = model.predict([bag_of_words(inp,words)]) #Predictowanie przy pomocy wyćwiczonego modelu\r\n",
|
||
|
" result_index = np.argmax(result)\r\n",
|
||
|
" tag = labels[result_index]\r\n",
|
||
|
" \r\n",
|
||
|
" for tg in data_pl_short[\"intents\"]: #znalezienie poprawnego tagu do zdania\r\n",
|
||
|
" if tg['tag'] == tag:\r\n",
|
||
|
" responses = tg['responses']\r\n",
|
||
|
" \r\n",
|
||
|
" print(random.choice(responses)) #Wyprintuj losową odpowiedz z danego zbioru odpowiedzi"
|
||
|
],
|
||
|
"execution_count": 32,
|
||
|
"outputs": []
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "markdown",
|
||
|
"metadata": {
|
||
|
"id": "ifvjglbO_SEA"
|
||
|
},
|
||
|
"source": [
|
||
|
"# 5. Rozmowa z botem!"
|
||
|
]
|
||
|
},
|
||
|
{
|
||
|
"cell_type": "code",
|
||
|
"metadata": {
|
||
|
"colab": {
|
||
|
"base_uri": "https://localhost:8080/"
|
||
|
},
|
||
|
"id": "VZf_aCUM-Amm",
|
||
|
"outputId": "9e3fcf7b-b9b3-47b0-acb5-48214f07f363"
|
||
|
},
|
||
|
"source": [
|
||
|
"chat()"
|
||
|
],
|
||
|
"execution_count": 33,
|
||
|
"outputs": [
|
||
|
{
|
||
|
"output_type": "stream",
|
||
|
"text": [
|
||
|
"Możesz rozpocząć rozmowę z Botem! (type quit to stop)\n",
|
||
|
"Ty: hej\n",
|
||
|
"Dobrze Cię widzieć!\n",
|
||
|
"Ty: ile masz lat?\n",
|
||
|
"Mam kilka dni\n",
|
||
|
"Ty: Kim jesteś?\n",
|
||
|
"Dobrze Cię widzieć!\n",
|
||
|
"Ty: Jak masz na imię?\n",
|
||
|
"Jestem Janet, twój ulubiony chatbot\n",
|
||
|
"Ty: Miłego Dnia\n",
|
||
|
"Do zobaczenia później\n",
|
||
|
"Ty: quit\n"
|
||
|
],
|
||
|
"name": "stdout"
|
||
|
}
|
||
|
]
|
||
|
}
|
||
|
]
|
||
|
}
|