Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 41 additions & 8 deletions examples/Example.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,56 @@
from chatbot import Chat, register_call
import wikipedia
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Enhanced Chatbot using Wikipedia and Template-based NLP
#
#
# Enhanced by: Muhannad - github.com/muhannad-iz-a-tech-nerd
#
# Features:
# - Handles "who is" questions via Wikipedia.
# - Uses template-based conversation.
# - Improved error handling and logging.
# - Simple CLI interface.

import os
import warnings
import wikipedia
from chatbot import Chat, register_call

# Suppress unnecessary warnings
warnings.filterwarnings("ignore")

# Optional but setting the language to English.
wikipedia.set_lang("en")

@register_call("whoIs")
def who_is(session, query):
"""Fetches a Wikipedia summary for a given query."""
try:
return wikipedia.summary(query)
except Exception:
except Exception as e:
# Fallback: search for similar topics
for new_query in wikipedia.search(query):
try:
return wikipedia.summary(new_query)
except Exception:
pass
return "I don't know about "+query
continue
return f"Sorry, I couldn't find anything about '{query}'."

def main():
# Initial question for the chatbot as a beginning
first_question = "Hi, how are you?"


template_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "Example.template")

if not os.path.exists(template_path):
print(f"Error: Template file not found at {template_path}")
return
Comment on lines +47 to +49

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Early return on missing template prevents user feedback in CLI.

Recommend replacing 'return' with 'sys.exit(1)' to provide a standard error exit code for CLI tools, aiding automation and scripting.

Suggested change
if not os.path.exists(template_path):
print(f"Error: Template file not found at {template_path}")
return
if not os.path.exists(template_path):
print(f"Error: Template file not found at {template_path}")
import sys
sys.exit(1)



chat = Chat(template_path)
chat.converse(first_question)

first_question = "Hi, how are you?"
chat = Chat(os.path.join(os.path.dirname(os.path.abspath(__file__)), "Example.template"))
chat.converse(first_question)
if __name__ == "__main__":
main()