Advertisement
iqhtyar2k

Active Learn Chat Bot

Jan 20th, 2024
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.73 KB | Source Code | 0 0
  1. import random
  2. import os
  3. import json
  4. import time
  5.  
  6. def greeting():
  7.     responses = ["Halo! Ada yang bisa saya bantu?", "Hai, apa kabar?", "Selamat datang!","v*_*v"]
  8.     return random.choice(responses)
  9.  
  10. def farewell():
  11.     responses = ["Sampai jumpa lagi!", "Selamat tinggal. Semoga harimu menyenangkan.", "Hati-hati.","v0_0v"]
  12.     return random.choice(responses)
  13.      
  14. def extract_entities(user_input):
  15.     user_input_lower = user_input.lower()
  16.     entities = []
  17.     if "nama" in user_input_lower:
  18.         entities.append("nama")
  19.  # tambahkan entitas lain jika diperlukan
  20.     return entities
  21.    
  22. def respond(user_input, knowledge):
  23.     if "kamu siapa" in user_input.lower():
  24.         return "Saya Chatbot, senang bisa chating sama kamu!"
  25.     elif "apa kabar" in user_input.lower():
  26.         return "Saya ini cuma program, tapi makasih udah nanya. kalau kamu gimana?"
  27.     elif "keluar" in user_input.lower():
  28.         return farewell()
  29.     elif "jawaban" in user_input.lower():
  30.         return add_knowledge(user_input, knowledge)
  31.     else:
  32.         return knowledge.get(user_input, "Saya enggak gitu ngerti nih. Bisa kamu jelasin lebih lanjut?")
  33.        
  34. def add_knowledge(user_input, knowledge):
  35.     new_knowledge = input("Saya harus jawab apa ya pertanyaan ini?")
  36.     question = user_input.replace("jawaban", "").strip()
  37.    
  38.     knowledge[question] = new_knowledge
  39.     save_knowledge(knowledge)
  40.    
  41.     return "ok, nanti akan saya jawab seperti itu... q*_*p"
  42.  
  43. def save_knowledge(knowledge):
  44.     with open("knowledge.json", "w") as file:
  45.         json.dump(knowledge, file)
  46.  
  47. def load_knowledge():
  48.     if os.path.exists("knowledge.json"):
  49.         with open("knowledge.json", "r") as file:
  50.             return json.load(file)
  51.     else:
  52.         return {}
  53.        
  54. def manage_conversation(user_input, knowledge):
  55.     # Extract entities from user input
  56.     entities = extract_entities(user_input)
  57.  
  58.     # Check knowledge for answers to questions
  59.     for entity in entities:
  60.         if entity in knowledge:
  61.             return knowledge[entity]
  62.  
  63.     # If no answers are found, ask user for more information
  64.     return "enggak jelas maksudnya apa sih? q?_?p"
  65.  
  66. def main():
  67.     knowledge = load_knowledge()
  68.     print(greeting())
  69.     last_input = time.time()
  70.    
  71.     while True:
  72.         user_input = input("d^_^b  ")
  73.         if user_input.lower() == "keluar":
  74.             print(farewell())
  75.             break
  76.         else:
  77.             response = respond(user_input, knowledge)
  78.             print("q^_^p", response)
  79.             last_input = time.time()
  80.  
  81.         if time.time() - last_input > 3:
  82.             print("kok diem? mau ngomongin apa nih?")
  83.             last_input = time.time()
  84.  
  85. if __name__ == "__main__":
  86.     main()
  87.  
Tags: Ai Chat Bot
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement