Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- import os
- import json
- import time
- def greeting():
- responses = ["Halo! Ada yang bisa saya bantu?", "Hai, apa kabar?", "Selamat datang!","v*_*v"]
- return random.choice(responses)
- def farewell():
- responses = ["Sampai jumpa lagi!", "Selamat tinggal. Semoga harimu menyenangkan.", "Hati-hati.","v0_0v"]
- return random.choice(responses)
- def extract_entities(user_input):
- user_input_lower = user_input.lower()
- entities = []
- if "nama" in user_input_lower:
- entities.append("nama")
- # tambahkan entitas lain jika diperlukan
- return entities
- def respond(user_input, knowledge):
- if "kamu siapa" in user_input.lower():
- return "Saya Chatbot, senang bisa chating sama kamu!"
- elif "apa kabar" in user_input.lower():
- return "Saya ini cuma program, tapi makasih udah nanya. kalau kamu gimana?"
- elif "keluar" in user_input.lower():
- return farewell()
- elif "jawaban" in user_input.lower():
- return add_knowledge(user_input, knowledge)
- else:
- return knowledge.get(user_input, "Saya enggak gitu ngerti nih. Bisa kamu jelasin lebih lanjut?")
- def add_knowledge(user_input, knowledge):
- new_knowledge = input("Saya harus jawab apa ya pertanyaan ini?")
- question = user_input.replace("jawaban", "").strip()
- knowledge[question] = new_knowledge
- save_knowledge(knowledge)
- return "ok, nanti akan saya jawab seperti itu... q*_*p"
- def save_knowledge(knowledge):
- with open("knowledge.json", "w") as file:
- json.dump(knowledge, file)
- def load_knowledge():
- if os.path.exists("knowledge.json"):
- with open("knowledge.json", "r") as file:
- return json.load(file)
- else:
- return {}
- def manage_conversation(user_input, knowledge):
- # Extract entities from user input
- entities = extract_entities(user_input)
- # Check knowledge for answers to questions
- for entity in entities:
- if entity in knowledge:
- return knowledge[entity]
- # If no answers are found, ask user for more information
- return "enggak jelas maksudnya apa sih? q?_?p"
- def main():
- knowledge = load_knowledge()
- print(greeting())
- last_input = time.time()
- while True:
- user_input = input("d^_^b ")
- if user_input.lower() == "keluar":
- print(farewell())
- break
- else:
- response = respond(user_input, knowledge)
- print("q^_^p", response)
- last_input = time.time()
- if time.time() - last_input > 3:
- print("kok diem? mau ngomongin apa nih?")
- last_input = time.time()
- if __name__ == "__main__":
- main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement