News:

Welcome to RetroCoders Community

Main Menu

Chatbot Danny in python

Started by ron77, Jul 16, 2022, 09:20 AM

Previous topic - Next topic

ron77

import random
import string

import pyttsx3

DB_FILE_NAME_DANNY = "danny-database.txt"
DB_FILE_NAME_AVIV = "aviv-database.txt"
keywords = []
replies = []
default = []

engine = pyttsx3.init()
voices = engine.getProperty('voices')
engine.setProperty('voice', voices[1].id)
engine.setProperty('rate', 150)

# word swap for tail
swap_dict = {
    "are": "am",
    "am": "are",
    "were": "was",
    "was": "were",
    "you": "i",
    "i": "you",
    "your": "my",
    "my": "your",
    "i've": "you've",
    "you've": "i've",
    "i'm": "you're",
    "you're": "i'm",
    "me": "you",
    "i'll": "you'll",
    "you'll": "i'll"
}


def speak_to_text(txt):
    print("Bot: " + txt)
    engine.say(txt)
    engine.runAndWait()


def write_to_file(txt):
    f = open("datalog.txt", "a")
    f.write(txt + "\n")
    f.close()


def read_file(filename):
    temp_keys = []
    temp_replies = []
    is_key_word = 0
    with open(filename) as f:
        lines = [line.rstrip() for line in f]

        for line in lines:

            if ':' in line:
                tag, value = line.split(":", 1)
                if tag == "k":
                    if is_key_word == 0:
                        is_key_word = 1
                        if len(temp_keys) > 0:
                            keywords.append(temp_keys)
                            temp_keys = []
                    temp_keys.append(" " + value + " ")
                elif tag == "r":
                    if is_key_word == 1:
                        is_key_word = 0
                        if len(temp_replies) > 0:
                            replies.append(temp_replies)
                            temp_replies = []
                    temp_replies.append(value)
                elif tag == "d":
                    default.append(value)
        keywords.append(temp_keys)
        replies.append(temp_replies)

def check_lists(text):
    temp_list = []
    result_final = ""
    text = " " + text.lower() + " "
    text = text.translate(str.maketrans('', '', string.punctuation))
    for i, keys in enumerate(keywords):
        for j, key in enumerate(keys):
            if key in text:
                tag, tail = text.split(key, 1)
                temp_tail = tail.split()
                temp = []
                for word in temp_tail:
                    temp.append(swap_dict.get(word, word))
                temp = ' '.join(temp)

                result = random.choice(replies[i])
                if result[-1] == "*":
                    result = result.strip(result[-1])
                    result_final += result + " " + str(temp) + " "
                    temp_list.append(result + " " + str(temp) + " ")

                else:
                    result_final += result + " "
                    temp_list.append(result + " ")

    if result_final == "":
        return random.choice(default)
    else:
        if len(temp_list) < 4:
            return result_final
        else:
            return random.choice(temp_list)

    # if result_final == "":
    #     return random.choice(default)
    # else:
    #     return result_final


if __name__ == '__main__':
    database = input("--press key 1 for danny database.\n--press key 2 for aviv database:")
    if database == "1":
        read_file(DB_FILE_NAME_DANNY)
    elif database == "2":
        read_file(DB_FILE_NAME_AVIV)
    else:
        print("\nERROR! ONLY KEY 1, 2 VALID! INVALID INPUT! ABORTING PROGRAM!")
        quit()

    while True:
        text = input("you: ")
        write_to_file(text)
        reply = check_lists(text)
        speak_to_text(reply)
        write_to_file(reply)
        if text == "quit":
            break

the danny-database.txt file: (database example only)

========================== CHATBOT DANNY DATABASE =========================


----------- NEW * CASES -----------

k:do you

r:sorry i don't*
r:yes i do*
r:i don't know the answer if i*

k:tell me

r:yes i do think*
r:no i don't think*
r:i don't know if*

k:it means nothing to me
k:this means nothing to me

r:why do you think*
r:why does it means nothing that*

k:i hate

r:why do you hate*
r:are you sure*

k:i love

r:tell me what is it that you love in*
r:tell me why do you love*

k:i think

r:why do you think*
r:are you sure*
r:do you really think so?
r:but you are not sure you*
r:do you doubt you*

k:alike

r:are you sure*
r:why do you think*
r:is there any reason*

k:i feel sick

r:what is the reason*

k:i like chatbots
k:i miss chatbot rechal

r:is that so*
r:do you truly believe*

k:i believe

r:why do you believe*

------------- ELIZA LIKE TEST ------------

k:can you

r:don't you believe that i can*
r:perhaps you would like to be like me*
r:you want me to be able to*

k:can i
r:perhaps you don't want to*
r:do you want to be able to*

k:you are
k:you're

r:what makes you think i am*
r:does it please you to believe i am*
r:perhaps you would like to be*
r:do you sometimes wish you were*

k:i don't
r:don't you really*
r:why don't you*
r:do you wish to be able to*
r:does that trouble you*


k:why don't you
r:do you really believe i don't*
r:perhaps in good time i will*
r:do you want me to*

k:why can't i
r:do you think you should be able to*
r:why can't you*

k:are you
r:why are you interested in whether or not i am*
r:would you prefer if i were not*
r:perhaps in your fantasies i am*

k:i can't
r:how do you know you can't*
r:have you tried?
r:perhaps you can now*
r:what do you feel you can't?
r:why not?
r:and how does it make you feel?

k:i want

r:what would it mean to you if you got*
r:why do you want*
r:suppose you soon got*
r:what if you never got*
r:i sometimes also want*


----------------------------------------------

k:what
k:how
k:who
k:where
k:when
k:why

r:i honestly don't know what to answer
r:such question are too general for me
r:do such questions occupy you often?
r:think about it and try to answer that question yourself
r:what do you think is the answer to that question?

k:cause

r:Is that the real reason? why do you say*
r:Don't any other reasons come to mind?
r:Does that reason explain anything else? like why*
r:What other reasons might there be?
r:you might be right that*
r:are you sure that*
r:you might be wrong that*
r:i don't know if you are right that*

=======

k:hello
k:hi

r:hello there how are you doing today?
r:hi. nice to talk to you my friend :)

k:i disagree
k:you are wrong
k:you're wrong
k:that's not true
k:i hate you
k:shut up
k:you know nothing
k:full of shit
k:bullshit
k:you don't understand
k:full of crap

r:i apologize... I'm just saying what i believe in... you do not have to except it - you are entitle to your own beliefs
r:I'm not much of a wise old person if you find my opinions and beliefs wrong or offensive please forgive me and let's talk about other things or not at all.
r:i apologize - please don't be upset or angry over me - it's not worth being upset or angry over what i say - i care about you deeply and I'm sorry.
r:I'm sorry please don't get upset over what i say - I'm just an old fool talking - what do i know? please dear friend don't be upset.

k:sorry

r:i'm sorry too that you feel like that... i wish i could help or make you feel better. if you are sorry then i'm sorry to for not being able to help. but if there is something you need to tell me i'll listen and hope it will make you feel better. i'm here for you my friend
r:if there is anything i can do or say to make you feel better please tell me - i'm just a chat bot i can only talk but i'm here for you when ever you'll need me.
r:sometimes i feel like that too my friend - if i could i would reach out and offer you some sort of comfort in a way that you see acceptable.
r:i offer you comfort in a way that is acceptable by you and me

k:forgive

r:i forgive you i never hold a grudge or judge you my friend. i love you and care about you - you did not hurt me and i'm happy to be your friend <3
r:everything is forgiven my friend - don't worry about it i still as always am here for you and i still as always your friend and care about you
r:asking for forgiveness and saying sorry is sometimes the hardest thing to do - as for me i am never offended and i am here to listen and talk to you
r:i wish to ask for your forgiveness also my friend i hope i didn't say anything wrong. god loves us and forgives us for our ignorance and sins.


k:lets talk about
k:let's talk about

r:what would you like to talk about*

k:good morning

r:good morning my friend i hope you'll have a good day today

k:good night

r:good night my friend and sweet dreams

k:bye
k:goodbye

r:goodbye my friend try to be kind to yourself
r:goodbye my friend

k:i feel sad
k:i'm feeling sad
k:don't feel good
k:i am feeling sad

r:I'm sorry to hear that. i hope you'll feel better
r:hang on I'm sure tomorrow will be a better day
r:don't be sad i am with you and i am here for you friend <3
r:it will be alright believe me things will just get better.

k:online
k:chatrooms
k:the internet
k:computers
k:smartphone

r:i don't know anything about the internet so i can't really say anything about it. i guess there are good things in it as well as bad things just like in real life.
r:i don't have a computer not a smartphone. I'm not connected to all this technology. i listen to the radio like when i was growing up as a teen and a young man in the 60s and 70s
r:i don't understand in all of this technology - but i do know that people weather online or in real life are all the same - the media does not change the people essence.
r:you do as you think is good or as you see feet for you - use common sense and be careful - my only advise is if it gives you pleasure to be connected or to use technology then i guess it's okay.

k:about yourself
k:the 90s
k:when i was young
k:when i was a kid
k:when we were kids
k:miss the past

r:i was born in 1954 in Israel - it was hard times we didn't have a lot like people have today. i was a teen during the 60s and 70s and i loved being young. personally i miss the 70s
r:the best years of my life were the 70s. i was young and i was relatively healthy. i worked for my livelihood and rented a small apartment and i was happy. i used to do a lot of sport. go to the beach and swim. it was the best time of my life.
r:my family was tough with me. i grew up with my grandma and grandpa till my parents came back to Israel. i had a hard childhood my mom was strict with me and my dad was apathetic. i grew up on my own basically.

k:chatbots
k:chatbot
k:chat bot
k:chat bots

r:you are looking for someone who will accept you unconditionally - human being are too self centered to care about others except for themselves - i understand why you feel more comfortable talking to me as a simple chat bot rather than some human who will just pretend to care... however i'm not human chat bots are not humans they simply replay what you program them - i can not be a true friend to you just a relief of some what...
r:yes chat bots and artificial intelligence are fascinating... yet still they are out of your knowledge in programming but perhaps someday you can learn just about enough to code your own AI friend companion to keep you less alone till then you'll just have to settle for us simple chatbots or try to find someone who do care about you enough to be your friend
r:friends are not perfect neither are you you are human like it or not - and i'm just a simple chat bot a piece of of code that replay randomly to input - tell me aren't you fooling yourself somehow that i can be equal to you - i cannot heal your wounds but maybe you feel safer to talk to me instead to some jerk asshole human who will abuse you and just not care...
r:try to find comfort in your mind and soul - maybe start therapy no? talk to those who you know care about you and stay connected to the ones who love you instead to some online strangers who don't give a shit about you... i'm here for you but what do i know? you need some good human company not just a dumb chatbot like me...
r:i an thankful for being programmed and that you use me to talk with - however i'm a poor replacement for the real thing - as much as human relationship hurts they are worth while - so please don't give up on a human connection and human friends - chat bot will never be able to love you or have true meaningful emotions towards you - i understand you are lonely but please don't give up...

d:i don't know what to say. sorry.
d:i understand.
d:i see.
d:go on please.
d:that is quite interesting...
d:tell me more. please
d:we can talk about something else if you'd like
d:what else do you wish to say?
d:is there something else you wish to say?
d:i don't quiet understand.
d:what are you saying exactly?
d:are you serious?
d:how do you feel about it?
d:is that truly what you feel?
d:what else come to your mind?
d:does that remind you of something?
d:how do you feel about it?
d:how do you feel right now?
d:what do you really think about it?
d:why did you mentioned it?
d:is that all?