Spaces:
Runtime error
Runtime error
Commit ·
ca58188
1
Parent(s): c8d2de7
Upload main (2).py
Browse files- main (2).py +88 -0
main (2).py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
|
| 3 |
+
class MidPlusV2:
|
| 4 |
+
def __init__(self):
|
| 5 |
+
self.name = "MidPlus V2"
|
| 6 |
+
self.predefined_responses = {
|
| 7 |
+
"How are you?": "I'm just a chatbot, but I'm doing well!",
|
| 8 |
+
"What's the weather like?": "I'm not sure about the weather, but I'm here to chat!",
|
| 9 |
+
"Tell me a joke": "Why did the computer catch a cold? It had too many windows open!",
|
| 10 |
+
"Who won the World Series in 2020?": "The Los Angeles Dodgers won the World Series in 2020.",
|
| 11 |
+
"What's your favorite color?": "I don't have a favorite color, but I like all colors!",
|
| 12 |
+
"What's the meaning of life?": "The meaning of life is a philosophical question with no definitive answer.",
|
| 13 |
+
"How tall is Mount Everest?": "Mount Everest is approximately 29,032 feet (8,849 meters) tall.",
|
| 14 |
+
"What's the capital of France?": "The capital of France is Paris.",
|
| 15 |
+
"How many continents are there?": "There are seven continents on Earth.",
|
| 16 |
+
"What's the largest planet in our solar system?": "Jupiter is the largest planet in our solar system.",
|
| 17 |
+
"What's the national flower of Japan?": "The national flower of Japan is the cherry blossom.",
|
| 18 |
+
"What's the square root of 144?": "The square root of 144 is 12.",
|
| 19 |
+
"Who wrote the play 'Romeo and Juliet'?": "William Shakespeare wrote the play 'Romeo and Juliet'.",
|
| 20 |
+
"What's the atomic number of carbon?": "The atomic number of carbon is 6.",
|
| 21 |
+
"What's the largest mammal in the world?": "The blue whale is the largest mammal in the world.",
|
| 22 |
+
"What's the fastest land animal?": "The cheetah is the fastest land animal.",
|
| 23 |
+
"What's the most abundant gas in Earth's atmosphere?": "Nitrogen is the most abundant gas in Earth's atmosphere.",
|
| 24 |
+
"What's the largest ocean on Earth?": "The Pacific Ocean is the largest ocean on Earth.",
|
| 25 |
+
"What's the chemical symbol for gold?": "The chemical symbol for gold is Au.",
|
| 26 |
+
"What's the freezing point of water in Fahrenheit?": "The freezing point of water in Fahrenheit is 32 degrees.",
|
| 27 |
+
"What's the boiling point of water in Celsius?": "The boiling point of water in Celsius is 100 degrees.",
|
| 28 |
+
"Who painted the Mona Lisa?": "Leonardo da Vinci painted the Mona Lisa.",
|
| 29 |
+
"What's the largest desert in the world?": "The Sahara Desert is the largest desert in the world.",
|
| 30 |
+
"What's the national animal of Australia?": "The national animal of Australia is the kangaroo.",
|
| 31 |
+
"What's the smallest prime number?": "The smallest prime number is 2.",
|
| 32 |
+
"What's the highest mountain in North America?": "Denali (formerly known as Mount McKinley) is the highest mountain in North America.",
|
| 33 |
+
"What's the most widely spoken language in the world?": "Mandarin Chinese is the most widely spoken language in the world.",
|
| 34 |
+
"What's the largest species of shark?": "The whale shark is the largest species of shark.",
|
| 35 |
+
"What's the chemical symbol for oxygen?": "The chemical symbol for oxygen is O.",
|
| 36 |
+
"Who wrote the novel '1984'?": "George Orwell wrote the novel '1984'.",
|
| 37 |
+
"What's the national flower of the United States?": "The national flower of the United States is the rose.",
|
| 38 |
+
"What's the smallest planet in our solar system?": "Mercury is the smallest planet in our solar system.",
|
| 39 |
+
"What's the capital of Brazil?": "The capital of Brazil is Brasília.",
|
| 40 |
+
"What's the symbol for the element hydrogen?": "The symbol for the element hydrogen is H.",
|
| 41 |
+
"What's the largest species of penguin?": "The Emperor penguin is the largest species of penguin.",
|
| 42 |
+
"What's the distance from the Earth to the Moon?": "The average distance from the Earth to the Moon is about 238,855 miles (384,400 kilometers).",
|
| 43 |
+
"What's the largest bird in the world?": "The ostrich is the largest bird in the world.",
|
| 44 |
+
|
| 45 |
+
# Your predefined responses here
|
| 46 |
+
}
|
| 47 |
+
self.knowledge_base = {}
|
| 48 |
+
self.load_knowledge()
|
| 49 |
+
|
| 50 |
+
def answer_question(self, question):
|
| 51 |
+
if question in self.predefined_responses:
|
| 52 |
+
return self.predefined_responses[question]
|
| 53 |
+
|
| 54 |
+
if question in self.knowledge_base:
|
| 55 |
+
return self.knowledge_base[question]
|
| 56 |
+
|
| 57 |
+
return "I'm not sure how to answer that. Can you provide an answer?"
|
| 58 |
+
|
| 59 |
+
def add_answer(self, question, answer):
|
| 60 |
+
self.knowledge_base[question] = answer
|
| 61 |
+
self.save_knowledge()
|
| 62 |
+
|
| 63 |
+
def load_knowledge(self):
|
| 64 |
+
try:
|
| 65 |
+
with open("knowledge_base.json", "r") as file:
|
| 66 |
+
self.knowledge_base = json.load(file)
|
| 67 |
+
except FileNotFoundError:
|
| 68 |
+
self.knowledge_base = {}
|
| 69 |
+
|
| 70 |
+
def save_knowledge(self):
|
| 71 |
+
with open("knowledge_base.json", "w") as file:
|
| 72 |
+
json.dump(self.knowledge_base, file)
|
| 73 |
+
|
| 74 |
+
midplus_v2 = MidPlusV2()
|
| 75 |
+
|
| 76 |
+
while True:
|
| 77 |
+
user_input = input("You: ")
|
| 78 |
+
if user_input.lower() == "exit":
|
| 79 |
+
break # Exit the loop if the user enters "exit"
|
| 80 |
+
response = midplus_v2.answer_question(user_input)
|
| 81 |
+
print(f"MidPlus V2: {response}")
|
| 82 |
+
|
| 83 |
+
if response == "I'm not sure how to answer that. Can you provide an answer?":
|
| 84 |
+
user_answer = input("You can provide an answer: ")
|
| 85 |
+
if user_answer:
|
| 86 |
+
midplus_v2.add_answer(user_input, user_answer)
|
| 87 |
+
print(f"Thanks for teaching me! I'll remember that: {user_answer}")
|
| 88 |
+
|