Creating a simple chatbot that uses a decision tree to determine if an object is recyclable can be accomplished by employing Python and lists. The chatbot will guide users through a series of questions and use their responses to determine if the item is recyclable or not.
Here is a sample implementation:
class RecyclabilityChatBot:
def __init__(self):
self.questions = [
"Is it made of plastic, metal, glass, paper, or cardboard? (Type 'plastic', 'metal', 'glass', 'paper', or 'cardboard')",
"Is it clean and dry? (Type 'yes' or 'no')",
"Is it a single-use item? (Type 'yes' or 'no')"
]
self.decisions = {
"plastic": self.plastic_recyclability,
"metal": self.metal_recyclability,
"glass": self.glass_recyclability,
"paper": self.paper_recyclability,
"cardboard": self.cardboard_recyclability
}
def run(self):
print("Welcome to the Recycling ChatBot! Let's determine if your item is recyclable.")
material = input(self.questions[0]).strip().lower()
if material not in self.decisions:
print("Sorry, I don't recognize that material.")
return
clean_dry = input(self.questions[1]).strip().lower()
single_use = input(self.questions[2]).strip().lower()
recyclable = self.decisions[material](clean_dry, single_use)
if recyclable:
print(f"{material.capitalize()} is recyclable!")
else:
print(f"{material.capitalize()} is not recyclable.")
def plastic_recyclability(self, clean_dry, single_use):
if clean_dry == 'yes':
if single_use == 'no':
return True # Recyclable
else:
return False # Not recyclable
else:
return False # Not recyclable
def metal_recyclability(self, clean_dry, single_use):
if clean_dry == 'yes':
return True # Always recyclable
else:
return False # Not recyclable
def glass_recyclability(self, clean_dry, single_use):
if clean_dry == 'yes':
return True # Always recyclable
else:
return False # Not recyclable
def paper_recyclability(self, clean_dry, single_use):
if clean_dry == 'yes':
return True # Always recyclable
else:
return False # Not recyclable
def cardboard_recyclability(self, clean_dry, single_use):
if clean_dry == 'yes':
return True # Always recyclable
else:
return False # Not recyclable
if __name__ == "__main__":
bot = RecyclabilityChatBot()
bot.run()
How the Chatbot Works:
-
Initialization: The chatbot starts with a predefined list of questions about the material, cleanliness, and whether it is a single-use item.
-
Questioning:
- The chatbot will ask the user about the material of the item.
- Then it will ask if the item is clean and dry.
- Finally, it will check whether the item is a single-use type.
-
Decision Making: Based on the user's responses, the chatbot will determine if the item is recyclable or not using specific decision functions for each material.
-
Output: The chatbot informs the user whether the item in question is recyclable.
Execution:
You can run this script in a Python environment. It will interactively ask for input and provide output based on the user's responses.