Python Forum
Help combining 2 of my codes to 1
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help combining 2 of my codes to 1
#1
Hello,

I have this chunk of code here (pairs.py):
import nltk
from nltk.chat.util import Chat, reflections

pairs = [
    [
        r"my name is (.*)",
        ["Hello %1, How are you today?",]
    ],
    [
        r"hi|hey|hello",
        ["Hello", "Hey there",]
    ], 
    [
        r"what is your name ?",
        ["I am Baxter",]
    ],
    [
        r"how are you ?",
        ["I'm doing good, How about you?",]
    ],
    [
        r"(.*) sorry",
        ["It's alright","It's ok, never mind",]
    ],
    [
        r"(.*) fine",
        ["Great to hear that, How can I help you?",]
    ],
    [
        r"i'm (.*) doing good",
        ["Nice to hear that","How can I help you?",]
    ],
    [
        r"(.*) age?",
        ["I'm a computer program I do not age",]
    ],
    [
        r"what (.*) want ?",
        ["Make me an offer I can't refuse",]
    ],
    [
        r"(.*) (sports|game) ?",
        ["I do enjoy a good game of chess",]
    ],
    [
        r"quit",
        ["Good Bye", "Take care. See you soon", "It was nice talking to you. See you soon"]
    ],
]

def chat():
    print("Hi! I am a test chatbot")
    chat = Chat(pairs, reflections)
    chat.converse()
And I would like to combine it with this code here (main.py):

   
#-------------------------------------------------------------------------------------
#                                  Imports
#-------------------------------------------------------------------------------------
import json
import random
import datetime
import operator
import os
import subprocess
import time
import sys
from unittest import result
import webbrowser
import requests
from bs4 import BeautifulSoup
import wikipedia
import wolframalpha
import pywhatkit
import threading
 

from BX_Intents import (greetings, farewell, thanks, noAnswer, youChoose)
from BX_External_Functions import (autoTypeAnimation, StartupText, ShutdownText, 
                                    listen, speak, getRandomJoke, getFunFacts,
                                    setReminders, terminateTimers)

from pairs import chat
#-------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------
#                           Commander Name (You) and A.I Name
#-------------------------------------------------------------------------------------
Commander = "Commander"
AI_Name = 'Baxter'
#-------------------------------------------------------------------------------------

#-------------------------------------------------------------------------------------
#                                  WishMe Function
#-------------------------------------------------------------------------------------
def wishMe():
    hour = int(datetime.datetime.now().hour)
    if hour>=0 and hour<12:
        speak("Good Morning" + Commander)
    elif hour>=12 and hour<18:
        speak("Good Afternoon" + Commander)   
    else:
        speak("Good Evening" + Commander) 
#-------------------------------------------------------------------------------------


#-------------------------------------------------------------------------------------
#                                User Input Function
#-------------------------------------------------------------------------------------
def UserInput():
    command = str(Prompt.ask("[yellow]Commander[/yellow]"))
    # CommanderInput(command)
    return command 
#-------------------------------------------------------------------------------------


#-------------------------------------------------------------------------------------
#                                       Main
#-------------------------------------------------------------------------------------
def BAXTER():
    command = UserInput()
    command=str(command).lower()

    #-------------------------
    #       Tell a Joke
    #-------------------------
    if ('joke' in command):
        joke = getRandomJoke()
        autoTypeAnimation(joke)
        speak(joke)
    #-------------------------
    #-------------------------
    #     Tell a Fun Fact
    #-------------------------
    if ('fact' in command):
        funFact = getFunFacts()
        autoTypeAnimation(funFact)
        speak(funFact)
    #-------------------------
    
    #-------------------------------------------------------------------------------------
    #                       Search Wikipedia (General Info)
    #-------------------------------------------------------------------------------------
    if ('weather' not in command):
        if ('who is' in command) or ('what is the' in command) or ('what is a' in command) or ("what is" in command):
            if ('time' not in command):
                if ('news' not in command):
                    speak('Searching...')
                    command = command.replace("who is","")
                    command = command.replace("what is the","")
                    command = command.replace("what is a","")
                    command = command.replace("what is","")
                    results = wikipedia.summary(command, sentences = 2)
                    autoTypeAnimation(results)
                    speak(results) 
                    
    #-------------------------------------------------------------------------------------
    #               Search Wolfram Alpha (Math/Conversions, Definitions)
    #-------------------------------------------------------------------------------------
    if ('news' not in command):
        if ('weather' in command) or ('calculate' in command) or ("what's" in command) or ('define' in command) or ("what" in command):
                speak('Searching...')
                command = command.replace("calculate","")
                command = command.replace("what's","")
                command = command.replace("define","")
                # Wolframalpha App Id
                appId = 'JH9XHR-W9J76L7H5A'
                # Wolfram Instance
                client = wolframalpha.Client(appId)
                res = client.query(''.join(command))
                results = next(res.results).text
                autoTypeAnimation(results)
                speak(results)

    #-------------------------------------------------------------------------------------
    #                       Open Stuff on the Internet
    #-------------------------------------------------------------------------------------
    #Open Youtube Videos (Ex: 'Play __ on youtube')
    if ('youtube' in command):
        autoTypeAnimation("Launching Youtube...")
        speak('Launching Youtube')
        command = command.replace("youtube","")
        pywhatkit.playonyt(command)

    #Open Google Maps and Find The Location of A You Want
    if ('where is' in command):
        command = command.replace("where is","")
        autoTypeAnimation("Locating" + command + "...")
        speak('Locating' + command)
        webbrowser.open_new_tab("https://www.google.com/maps/place/" + command)

    #Search Stuff on Google
    if ('search' in command):
        command = command.replace("search", "")
        autoTypeAnimation("Searching" + command + " on Google")
        speak('Searching' + command)
        pywhatkit.search(command)
    
    #Close Firefox
    if ('close firefox' in command):
        autoTypeAnimation("Terminating Firefox...")
        speak('Closing Firefox')
        command = command.replace("close firefox", "")
        browser = "firefox.exe"
        os.system("taskkill /f /im " + browser)   

    #-------------------------------------------------------------------------------------
    #                       Open Stuff on the Computer
    #-------------------------------------------------------------------------------------
    #Open Windows Media Player and Auto Play the Playlist Called Music
    if ('play music' in command) or ('media player' in command) or ('drop the needle' in command):
        autoTypeAnimation("Launching music...")
        speak("Launching Music")
        command = command.replace("play music", "")
        command = command.replace("media player", "")
        command = command.replace("drop the needle", "") 
        subprocess.Popen("C:\Program Files (x86)\Windows Media Player\wmplayer.exe /Playlist Music")

    #Close Windows Media Player
    if ('stop music' in command):
        autoTypeAnimation("Terminating music...")
        speak('Closing Music')
        command = command.replace("stop music", "")
        mediaPlayer = "wmplayer.exe"
        os.system("taskkill /f /im " + mediaPlayer)   

    #-------------------------------------------------------------------------------------
    #                               Set Reminders
    #-------------------------------------------------------------------------------------
    if ('remind me' in command):
        command = command.replace("remind me", "")
        setReminders(command) #Call setReminders() from External Functions
    
    #-------------------------------------------------------------------------------------
    #                       Stop Program/Script Command
    #-------------------------------------------------------------------------------------
    if ('stop' in command) or ('shutdown' in command) or ('quit' in command):
        speak("Shutting Down...")
        results = "Terminating program..."
        autoTypeAnimation(results)
        ShutdownText()
        terminateTimers()
        exit()
    #-------------------------------------------------------------------------------------
#-------------------------------------------------------------------------------------
      

#------------------------------------------------------------------------------------------
#                                   Run The Program
#------------------------------------------------------------------------------------------
StartupText()
wishMe()
speak("How may I be of service?") 

while True:
    BAXTER()
#------------------------------------------------------------------------------------------
So essentially I would like to have an if statement that looks through (pairs.py) to see if the user typed in anything matching the defined phrases in that list and it will spit out the appropriate responses, but it will also look through (main.py) to see if the user typed a command there (opening stuff on the computer or a wiki search, ect..) and do the appropriate action.

How would I go about doing that, because right now (pairs.py) is a separate chatbot and my (main.py) is another separate chatbot and I would like to combine the 2 together.

Any ideas?

Thanks in advance.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020