forked from adi0509/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchatbot.py
More file actions
133 lines (123 loc) · 4.21 KB
/
chatbot.py
File metadata and controls
133 lines (123 loc) · 4.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# Description: This is a chatbot program
#There are broadly two variants of chatbots: Rule-Based and Self learning.
#Rule-based approach, a bot answers questions based on some rules on which it is trained on
#Self learning bots are the ones that use some Machine Learning-based approach to chat
#Resource: (1) https://towardsdatascience.com/build-your-first-chatbot-using-python-nltk-5d07b027e727
# (2) https://api.coinmarketcap.com/v1/ticker/bitcoin/
# (3) https://api.coinmarketcap.com/v1/ticker/
# (4) https://realpython.com/python-bitcoin-ifttt/
from nltk.chat.util import Chat, reflections
import requests
import time
from datetime import datetime
#The URL Ticker to get the .json files of the crypto currencies
TICKER_URL = 'https://api.coinmarketcap.com/v1/ticker/'
#Function to get the latest crypto currency price of a specific 'crypto' e.g bitcoin, litecoin, etc.
# crypto = {bitcoin, litecoin, etherium, ...}
def get_latest_crypto_price( crypto ):
response = requests.get(TICKER_URL+crypto+'/')
response_json = response.json()
# Convert the price to a floating point number
return float(response_json[0]['price_usd'])
#Pairs is a list of patterns and responses.
pairs = [
[
r"my name is (.*)",
["Hello %1, How are you today ?",]
],
[
r"(.*) (much|price) (.*)(bitcoin|btc)(.*)",
["The price of bitcoin when you started this chat was "+ "".join( str(get_latest_crypto_price( 'bitcoin'))),]
],
[
r"what is the price of (bitcoin-cash|bch)(.*)",
["The price of bitcoin-cash when you started this chat was "+ "".join( str(get_latest_crypto_price( 'bitcoin-cash'))),]
],
[
r"(.*) (much|price) (.*) (litecoin|ltc)(.*)",
["The price of litecoin when you started this chat was "+ "".join( str(get_latest_crypto_price( 'litecoin'))),]
],
[
r"(.*) (much|price) (.*)(ethereum|eth)(.*)",
["The price of ethereum when you started this chat was "+ "".join( str(get_latest_crypto_price( 'ethereum'))),]
],
[
r"(.*)help(.*) ",
["I can help you ",]
],
[
r"(.*) your name ?",
["My name is J.A.R.V.I.S like in Iron Man, but you can just call me Jarvis and I'm a chatbot ?",]
],
[
r"how are you ?",
["I'm doing very well\nHow about You ?",]
],
[
r"sorry (.*)",
["Its alright","Its OK, never mind that",]
],
[
r"i'm (.*) doing good",
["Nice to hear that","Alright, great !",]
],
[
r"(hi|hey|hello|hola|holla)(.*)",
["Hello", "Hey there",]
],
[
r"what (.*) want ?",
["Make me an offer I can't refuse",]
],
[
r"(.*) created ?",
["randerson112358 created me using Python's NLTK library ","top secret ;)",]
],
[
r"(.*) (location|city) ?",
['Tokyo, Japan',]
],
[
r"how is the weather in (.*)?",
["Weather in %1 is amazing like always","It's hot here in %1","It's chilli here in %1", "In %1 there is a 50% chance of rain",]
],
[
r"i work (in|at) (.*)?",
["%1 is an amazing company, I have heard about it.",]
],
[
r"(.*)raining in (.*)",
["No rain in the past 4 days here in %2","In %2 there is a 50% chance of rain",]
],
[
r"is it (.*) in (.*)",
["No its not %1 in %2","It could be", "Yes its %1 in %2"]
],
[
r"how (.*) health (.*)",
["Health is very important, but I am a computer, so I don't need to worry about my health ",]
],
[
r"(.*)(sports|game|sport)(.*)",
["I'm a very big fan of Basketball",]
],
[
r"who (.*) sportsperson ?",
["Messy","LeBron", "D-Wade"]
],
[
r"who (.*) (moviestar|actor|actress)?",
["Zendaya"]
],
[
r"quit",
["Bye for now. See you soon :) ","It was nice talking to you. See you soon :)"]
],
]
#A Function to run the chatbot
def chatty():
print("Hi, I'm J.A.R.V.I.S and I want to help and chat with you ! \nPlease type lowercase English language to start a conversation. Type quit to leave ") #default message at the start
chat = Chat(pairs,reflections )
chat.converse()
#Run the chatbot
chatty()