Part 2: some working code for python, nltk, sql chatbox experiment

Update: 9-10-19 Life has been hectic, but I am constantly stirring this project in my brain pan.  I hope to do the part 3 soon, but it's a ton to learn.  I will come back to it when life gives me time.


I started to tackle this today, and if you haven't looked at part 1 of this experiment, and you want to it's mostly just me figuring out what I need to setup and psuedocode.  Here's the link:

https://camelcasenoodles.blogspot.com/2019/08/psuedo-for-nltk-sql-python-chatbot-part1.html


I decided since my SQL skillz are very light and soft, The SQL part will need to be a part 3 while I take the time to learn more as I go.    For part 2 I have for you some working code.   I had some issues with the python enchant import, and it may have to be excluded from the experiment for the time being.  If all else faills I can run it on my Linux machine, but that may not be an option for you. None of these windows issues happen on my chatbot at the website because the server is SET UP for python.

I don't think enchant will be necessary as we are returning a default response for anything that isn't in the trigger words lists.  The txt file is just ten lines of things about me.

"She is a learner and a dreamer."
"There is sillyness but there is also seriousness."
"She is a Michigander through and through, a love of fresh water, and the smell of Fall."
"Art is also a passion, she paints and likes to do portraits."
"Although she didn't finish college, she is making another go at Montcalm Community College."
"Interesting factoid, Nellie was a registered working CNA for about 2 months."
"She made me, and I'm awesome, so is she."
"She's worked in jobs that most people would walk away from on their first day.  Many had."
"The machine at the paper mill she worked with broke numerous records for 'Days without a break'."
"My creator, Nellie, loves a good brain teaser. (Professor Layton(NDS) is great for puzzles!)."
"Her favorite way to decide to do something with code is to ask, 'Why not?' There is rarely a reason not to try something new."

*BONUS- To get blogger to format your code better:  add this too the HTML and then paste in the empty spot between tags:
-----------------

<pre><code>
*************<br />
*************</code></pre>

-----------------
I've added comments and such to the code. 
Here it is:

********************
# VS code:   back-shift = Shift + Tab
import random # I took this out. It's not necessary for this experiment
import nltk 



#  pyenchant would not install on my windows 10 machine, some C library was missing.  I found a solution on StackOverflow:
# https://stackoverflow.com/questions/29381919/importerror-the-enchant-c-library-was-not-found-please-install-it-via-your-o
#   install happened, but enchant is still not running because of missing Windows 10 C wheel for pyenchant.
# 'Art' is the user answer for: 
#   Resolved: On Win7-64 I ran
#   pip3 install pyenchant==1.6.6
#  python enchant removed from experiment.

#----------------#
#  text file     #
#----------------#

#path_to_about_me = "C:\Users\sarah\Desktop\Experiment_201\aboutme.txt"
trigger_WIWA = ["you", "you've", "you'd", "you're", "wiwa", "whispering wall", "your"]
trigger_ME = ["nellie", "creator", "tobey", "nell", "nelly"]



###   Lets make a class to handle all the number operations for indexes used.
### I think this will have a minimum too at some point.
class NOTGLOBAL(object):
    def __init__(self, max_num):
        self.line_num = 0
        self.max_num = max_num
    def increment(self):
        self.line_num += 1
        if self.line_num > self.max_num:
            self.line_num = 0

lineNUM = NOTGLOBAL(10)
carrot = "I was created by Nellie Tobey, while I do not talk about myself yet, I can tell you about her."

# When SQL gets involved, this will not get script lines from a txt file, but from an SQL table.

def get_script_line():
    """get a script line for user. current script for this has 11 lines"""
    LINE_GET = lineNUM.line_num
    
    try:
        #---------- Thanks again stackOverflow!----------
        #  https://stackoverflow.com/questions/40594914/open-a-file-on-windows-with-python-3-5
        # because of window's paths having those forward slashes, python needs that file path as a raw bytestring
        # Need to find a solution so that the 'with' block can be used.  
        with open(r"C:\Users\sarah\Desktop\Experiment_201\aboutme.txt") as f:
            lines = f.readlines()
            return lines[LINE_GET]
      
    except:
        print("try block fail in get_script_line()")
    finally:
        f.close
        lineNUM.increment()

#------ the triggers -----#
# loop through our trigger list,
# if it's in wiwa's list, return default.
# if it's in ME list, return a script line.
def look_for_triggers(astring):
    if type(astring) != str:
         return "DEFAULT RESPONSE SENT"
    else:
        astring = astring.lower()
    for words in trigger_WIWA:
        if words in astring:
            return carrot
    for words in trigger_ME:
        if words in astring:
            line = get_script_line()
            return line
    return "DEFAULT RESPONSE SENT"

def test_get_script_line():
    count = 0;
    while count < 25:
        retrieved = get_script_line()
        print(retrieved)
        count += 1

def test_look_for_triggers():
    Me_strings = ["Nellie is silly", "neLL does not like mushrooms", "The creator of this website is not available at the moment."]
    Non_triggers = ["ellie macdoooougal.", "create a chatbot app.", "I like cookies.", "This is incorrect: Wiwwa", "This is also incorrect: nallie", 89, lineNUM]
    WIWA_strings = ["Hello wiwa", "Where do you sleep WiWa?"]

    seperator = "~*~*~*~*~*~*~*~*~*~**~*~**~*~**~*~*~*~"

    print(seperator)
    print("testing Me strings: quantity= 3")
    print("These should return lines from the txt file.\n")
    for stringss in Me_strings:
        x = look_for_triggers(stringss)
        print(x)
    
    print(seperator)
    print("testing NON triggers: quantity= 7")
    print("these should return: DEFAULT RESPONSE SENT\n")
    for stringss in Non_triggers:
        x = look_for_triggers(stringss)
        print(x)

    print(seperator)
    print("testing WIWA triggers: quantity= 2")
    print("these should return the default carrot.\n")
    for stringss in WIWA_strings:
        x = look_for_triggers(stringss)
        print(x)

def tests():
    print("------  TEST 1 ---------")
    test_get_script_line()
    print("*" * 10)
    print("----- TEST 2 -----------")
    test_look_for_triggers()

tests()






******************************

Comments

Popular posts from this blog

playing with color in powershell python

JavaScript Ascii animation with while loops and console.log

playing with trigonometry sin in pygame