I made chatbot in Python with a help of Keras lib. My chatbot works great but now I want to add a feature so that it can tell what day, time, weather is it, for example:
me: what time is it?
bot: its 12:35
Data that I used is csv format where features are questions and answers are labels.
My model is predicting answer based on user question input.
For example, for this set of questions:
import pandas as pd
list1 = ["what's the time", "whats the time?", "what time is it?",
"today is what day?", "what day is today", "what day is it?",
"what is the temperature", "how is the weather?"]
list2 = ["it is time.time()", "it is time.time()", "it is time.time()",
"today is get_day()", "today is get_day()", "today is get_day()",
"the weather is get_weather()", "the weather is get_weather()"]
df = pd.DataFrame()
df["question"] = list1
df["answer"] = list2
question answer
0 what's the time it is time.time()
1 whats the time? it is time.time()
2 what time is it? it is time.time()
3 today is what day? today is get_day()
4 what day is today today is get_day()
5 what day is it? today is get_day()
6 what is the temperature the weather is get_weather()
7 how is the weather? the weather is get_weather()
The answer / label should be: "It is " + time.time()
But I can't pass "It is " + time.time() as a label because its not string or numeric value.
I know that i can get time in Python like time.time() but how can I add that function to be my answer. My answers need to be numeric values (because I'm passing them into ML model) so I cant write "time is"+time.time(). Is there a way to do this, to store function in json file or csv file and make ML model. I can change my data to be JSON format if that helps.
PS
I'm planning to make a lot of functions that will be predicted so simple solutions such as:
if answer == 'time':
print(time.time())
Does not work for me. I would like to automate this
Related
import datetime
data = {'13:55':0,'13:56':1,'13:57':2, '13:58':3}
while True:
timee = datetime.datetime.now()
time = timee.strftime("%H:%M")
while time not in data:
timee = datetime.datetime.now()
time = timee.strftime("%H:%M")
if time in data:
print('Done')
print(data[time])
From the given code, I always get an output in case of the first or the last object in the dictionary, for example, if the current time is 13:55, I want it to display the output 'done', similarly if the time is 13:56, I want the same output and so on. Also, I don't want to break the loop since I want my program to run continuously. But it only gives me an output in case of 13:55 or 13:58 or it wouldn't even give me an output.
Whereas what I want is basically, I want it to give me an output every time the time is present in the dictionary. Please help me.
(I'm really sorry if you don't get it, I've tried my best to put this forward).
If any questions please let me know.
Thank you in advance.
an illustration of my comment; at the moment, both your while loops do the same thing; you can simplify to
import datetime
import time
data = {'13:51':0,'13:52':1,'13:53':2, '13:54':3,
'found':[]}
while True:
t = datetime.datetime.now().strftime("%H:%M")
if (t in data) & (t not in data['found']):
print(f'Done, found time {t} in data\n value: {data[t]}')
data['found'].append(t)
time.sleep(1)
I've added a little helper key to track which times were found.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
What would a piece of code look like which checks today's date, and if it's a particular date, it will print out a message?
The context is that I'm making a birthday cake for my son who codes and I want to write a themed Happy Birthday message on top correctly. (Sorry, not as clever or serious as a lot of the things on here!)
I'd like something which is basically:
johnsBirthday = 01/01/1998
johnsAge = todaysdate - johnsBirthday (in years)
if todays date == 01/01/XXXX then print("Happy Birthday John!" + johnsAge + " today!")
My knowledge of python is very limited (as I'm sure you can tell from the above) but I vaguely know how to do this in Excel, so I figure there must be a way to do it in python too?
I know could always just write out:
print("Happy Birthday, John!")
and he'd appreciate that, but I think it would really make him smile to go a little further than that!
# Import datetime class from datetime module
from datetime import datetime
birthday = "20/09/1998"
# Parses the string into a datetime object
birthday_obj = datetime.strptime(birthday, '%d/%m/%Y')
# Gets todays date
now = datetime.now()
# Checks the day and month to verify birthday status
if(birthday_obj.day == now.day and birthday_obj.month == now.month):
johns_age = str(now.year - birthday_obj.year)
print("Happy Birthday John! " + johns_age + " today!")
For your purpose, it might be easier to use regular expressions if you are familiar with them. You can search any patterns you like, after converting datetimes to string, or better yet if you already have datetimes in string formats.
For datetime to string format conversion codes, check out -
format-codes
Example code
import re
from datetime import datetime
pattern = re.compile(r'^01/01/')
today_date = datetime.now().strftime(r'%d/%m/%Y')
some_date = '01/01/2021'
print(re.match(pattern, some_date)) # <re.Match object; span=(0, 6), match='01/01/'>
print(today_date) # 20/09/2020
print(pattern.match(today_date)) # None
Edit - Had forgotten that age needs to be calculated!
import re
from datetime import datetime
johns_birthday = '01/01/1998'
if (re.match('^01/01/', johns_birthday)):
johns_age = datetime.now().year - datetime.strptime(johns_birthday, r'%d/%m/%Y').year
print("Happy Birthday John! " + str(johns_age) + " today!")
I'm trying to create a program that asks if there was school that day and if so, subtracts that from the total, (86 days left as of 1-22-18). It works, but the program ends after one subtraction, so my question is, is there any way for it to continue running and update itself, or maybe ask the user again in 24 hours (no clue how)?
Python 3.4.4
Windows 10
import time
localtime = time.asctime(time.localtime(time.time()))
day = localtime[0:3]
check = 0
daysLeft = 87 #As of 1-22-18
daysOfTheWeek = ["Mon", "Tue", "Wed", "Thu", "Fri"]
yesPossibilities = ["yes", "y", "yeah"]
print ("Did you have school today?")
schoolToday = input().lower()
if schoolToday in yesPossibilities:
if day in daysOfTheWeek:
daysLeft -= 1
print ("There are", daysLeft, "days of school left!")
I think what you're really trying to do is save the results each time you run your script (Ex: If you run it today, it tells you there are 86 days left, if you run it tomorrow, it tells you there are 85 days left, etc). You probably don't want to run the script forever because if you turn your computer off, the script is terminated, which means you'll lose all of your results. I would save the output to a text file in the following manner:
print("There are" daysLeft, "days of school left!")
with open("EnterNameOfFileHere.txt",'w') as f:
print(daysLeft,file=f)
This will save the daysLeft variable in a text file, which you can access at the start of your program in the following manner:
check = 0
with open("EnterNameOfFileHere.txt") as f:
daysLeft = int(f.readline().strip())
daysOfTheWeek = ....
In summary, implementing this will allow you to save your results each time you run your script so that you can start from that value the next time you run the script.
You need an infinite loop and a sleep timer
import time
time.sleep(86400) #this will make the code sleep for 1 day = 86400 seconds
Next, put the sleep into the infinite loop
while True:
#get input
if input meets condition:
reduce day count by 1
print number of days left
time.sleep(86400)
if days left meets some threshold:
print "school over"
break
I apologize if my question is a little confusing, because to be honest I am not 100% sure what I am trying to do either. I have a program that asks the user for a departure date and departure time, so I will end up with an input of something -like 2017-11-11 and 11:15 AM. The program then asks the user for an amount of miles they are going to travel and the speed in Miles Per Hour. So they might enter 300 for miles and 65 for MPH. I need to use this information to create an estimated arrival time, but I haven't the slightest clue how to do so. My program has no issue receiving this information, I just need to know how to work out the calculation.
Edit:
Here is some sample of my code so it might be easier to understand what I am doing.
def get_departure_time():
while True:
date_str = input("Enter departure time (HH:MM AM/PM): ")
try:
depart_time = datetime.strptime(date_str, "%H:%M %p")
except ValueError:
print("Invalid date format. Try again.")
continue
return depart_time
The function to get the departure date is the exact same, so using the user input for those two values, and the user input for miles and mph I have to create an arrival time.
num_hours = distance / mph
arrival_time = departure_time + num_hours
Not sure I understand your question, but is this what you're looking for?
This question already has answers here:
How do I get the current time?
(54 answers)
Closed 6 years ago.
I would like to know a way to time between two points in a program. In my situation I will ask the user 10 questions and after display the time it took for them to answer the question (example code below). How would i do this through something like import time ?
Example code:
timer.start
question1 = input("What is your favorite game ?")
timer.end
print(timer.time)
^
The timer.x thing is going to be replaced with your suggestions.
import time
s=time.time()
question1 = input("What is your favorite game ?")
e=time.time()
print(e-s)
time.time() Returns the time in seconds since the epoch as a floating point number.
How about this?
from datetime import datetime
start = datetime.now()
question1 = input("What is your favorite game ?")
end = datetime.now()
print(str(end - start))