|
|
import json |
|
|
import random |
|
|
from typing import Dict, List, Optional |
|
|
|
|
|
class KiswahiliKnowledgeBase: |
|
|
"""Knowledge base for Kiswahili language and East African culture""" |
|
|
|
|
|
def __init__(self): |
|
|
self.proverbs = [ |
|
|
{ |
|
|
"swahili": "Mwacha mila ni mtumwa.", |
|
|
"english": "He who abandons his culture is a slave.", |
|
|
"meaning": "One should cherish and preserve their cultural heritage." |
|
|
}, |
|
|
{ |
|
|
"swahili": "Haraka haraka haina baraka.", |
|
|
"english": "Hurry hurry has no blessing.", |
|
|
"meaning": "Rushing leads to mistakes; patience yields better results." |
|
|
}, |
|
|
{ |
|
|
"swahili": "Asiyesikia la mkuu huvunjika guu.", |
|
|
"english": "He who does not listen to elders breaks a leg.", |
|
|
"meaning": "Respect and learn from those with experience." |
|
|
}, |
|
|
{ |
|
|
"swahili": "Mwenye pupa hadiriki kula tamu.", |
|
|
"english": "The impatient one misses out on sweetness.", |
|
|
"meaning": "Patience is rewarded with better outcomes." |
|
|
}, |
|
|
{ |
|
|
"swahili": "Ukiona vyaelea, vimeundwa.", |
|
|
"english": "If you see things floating, they were made to float.", |
|
|
"meaning": "Everything happens for a reason; nothing is accidental." |
|
|
} |
|
|
] |
|
|
|
|
|
self.greetings = { |
|
|
"morning": { |
|
|
"swahili": "Habari za asubuhi", |
|
|
"english": "Good morning", |
|
|
"response": "Nzuri, asante" |
|
|
}, |
|
|
"afternoon": { |
|
|
"swahili": "Habari za mchana", |
|
|
"english": "Good afternoon", |
|
|
"response": "Nzuri, na wewe" |
|
|
}, |
|
|
"evening": { |
|
|
"swahili": "Habari za jioni", |
|
|
"english": "Good evening", |
|
|
"response": "Salama, asante" |
|
|
}, |
|
|
"night": { |
|
|
"swahili": "Usiku mwema", |
|
|
"english": "Good night", |
|
|
"response": "Na wewe pia" |
|
|
} |
|
|
} |
|
|
|
|
|
self.cultural_facts = [ |
|
|
"Swahili is a Bantu language with significant Arabic influence", |
|
|
"Kiswahili is spoken by over 200 million people across East Africa", |
|
|
"The word 'Safari' comes from Swahili, meaning 'journey'", |
|
|
"Hakuna Matata means 'no worries' in Swahili", |
|
|
"Swahili culture is a blend of African, Arab, and Indian influences", |
|
|
"The Swahili coast has been a trading hub for centuries", |
|
|
"Swahili is an official language in Kenya, Tanzania, and Uganda", |
|
|
"Traditional Swahili architecture features coral stone and intricate wood carvings", |
|
|
"Swahili poetry (Utenzi) has a rich literary tradition", |
|
|
"The Swahili calendar is based on the Islamic lunar calendar" |
|
|
] |
|
|
|
|
|
self.lion_king_lore = [ |
|
|
"Simba means 'lion' in Swahili", |
|
|
"Rafiki means 'friend' in Swahili", |
|
|
"Hakuna Matata is a Swahili phrase popularized by The Lion King", |
|
|
"Pumbaa means 'simpleton' or 'foolish' in Swahili", |
|
|
"Shenzi means 'uncouth' or 'barbaric' in Swahili", |
|
|
"The Lion King draws inspiration from African landscapes and wildlife", |
|
|
"Many character names in The Lion King are Swahili words", |
|
|
"The Circle of Life concept reflects African philosophical views", |
|
|
"The story incorporates elements from African folklore and Shakespeare's Hamlet" |
|
|
] |
|
|
|
|
|
self.video_themes = { |
|
|
"safari": { |
|
|
"description": "African wildlife and landscapes", |
|
|
"keywords": ["elephant", "lion", "giraffe", "savanna", "sunset", "acacia"], |
|
|
"swahili_terms": ["safari", "wanyama", "nyika", "machozi ya jua"] |
|
|
}, |
|
|
"dance": { |
|
|
"description": "Traditional African dance and celebration", |
|
|
"keywords": ["maasai", "warriors", "jumping", "drums", "ceremony"], |
|
|
"swahili_terms": ["ngoma", "kichwa", "mchezo", "sherehe"] |
|
|
}, |
|
|
"market": { |
|
|
"description": "Vibrant African market scenes", |
|
|
"keywords": ["market", "vendors", "colorful", "spices", "fabrics"], |
|
|
"swahili_terms": ["soko", "biashara", "rangi", "bidhaa"] |
|
|
}, |
|
|
"coastal": { |
|
|
"description": "Swahili coast and ocean life", |
|
|
"keywords": ["beach", "dhow", "fishing", "coral", "indian ocean"], |
|
|
"swahili_terms": ["pwani", "jahazi", "samaki", "bahari"] |
|
|
} |
|
|
} |
|
|
|
|
|
def get_random_proverb(self) -> str: |
|
|
"""Get a random Swahili proverb with translation""" |
|
|
proverb = random.choice(self.proverbs) |
|
|
return f"{proverb['swahili']} - '{proverb['english']}' ({proverb['meaning']})" |
|
|
|
|
|
def get_cultural_fact(self) -> str: |
|
|
"""Get a random cultural fact""" |
|
|
return random.choice(self.cultural_facts) |
|
|
|
|
|
def get_lion_king_fact(self) -> str: |
|
|
"""Get a random Lion King fact""" |
|
|
return random.choice(self.lion_king_lore) |
|
|
|
|
|
def generate_kiswahili_response(self, base_response: str) -> str: |
|
|
"""Enhance a response with Kiswahili elements""" |
|
|
|
|
|
|
|
|
if any(word in base_response.lower() for word in ['hello', 'hi', 'greetings']): |
|
|
greeting = random.choice(list(self.greetings.values())) |
|
|
base_response = f"{greeting['swahili']}! {base_response}" |
|
|
|
|
|
|
|
|
if random.random() < 0.3: |
|
|
proverb = self.get_random_proverb() |
|
|
base_response += f"\n\n🌍 **Dhana ya Kiafrika**: {proverb}" |
|
|
|
|
|
|
|
|
if random.random() < 0.2: |
|
|
fact = self.get_cultural_fact() |
|
|
base_response += f"\n\n📚 **Ujuzi wa Kitamaduni**: {fact}" |
|
|
|
|
|
return base_response |
|
|
|
|
|
def get_video_theme_suggestions(self) -> List[Dict]: |
|
|
"""Get suggestions for cultural video themes""" |
|
|
suggestions = [] |
|
|
for theme, info in self.video_themes.items(): |
|
|
suggestions.append({ |
|
|
"theme": theme, |
|
|
"description": info["description"], |
|
|
"swahili_terms": info["swahili_terms"], |
|
|
"prompt_suggestion": f"{info['description']} with {', '.join(info['keywords'][:3])}" |
|
|
}) |
|
|
return suggestions |
|
|
|
|
|
def enhance_image_prompt(self, prompt: str) -> str: |
|
|
"""Enhance image prompts with cultural context""" |
|
|
prompt_lower = prompt.lower() |
|
|
|
|
|
|
|
|
for theme, info in self.video_themes.items(): |
|
|
for keyword in info["keywords"]: |
|
|
if keyword in prompt_lower: |
|
|
|
|
|
swahili_term = random.choice(info["swahili_terms"]) |
|
|
return f"{prompt}, {info['description']}, {swahili_term}, East African style" |
|
|
|
|
|
return prompt |
|
|
|
|
|
def get_learning_lesson(self, level: str = "beginner") -> Dict: |
|
|
"""Get a Kiswahili learning lesson""" |
|
|
lessons = { |
|
|
"beginner": { |
|
|
"topic": "Basic Greetings", |
|
|
"phrases": [ |
|
|
{"swahili": "Jambo", "english": "Hello", "pronunciation": "Jahm-boh"}, |
|
|
{"swahili": "Habari", "english": "How are you?", "pronunciation": "Hah-bah-ree"}, |
|
|
{"swahili": "Nzuri", "english": "Good/Fine", "pronunciation": "N-zoo-ree"}, |
|
|
{"swahili": "Asante", "english": "Thank you", "pronunciation": "Ah-sahn-teh"}, |
|
|
{"swahili": "Karibu", "english": "Welcome", "pronunciation": "Kah-ree-boo"} |
|
|
], |
|
|
"practice": "Try greeting someone with 'Jambo' and 'Habari yako?'" |
|
|
}, |
|
|
"intermediate": { |
|
|
"topic": "Common Phrases", |
|
|
"phrases": [ |
|
|
{"swahili": "Tafadhali", "english": "Please", "pronunciation": "Tah-fah-dah-lee"}, |
|
|
{"swahili": "Samahani", "english": "Excuse me/Sorry", "pronunciation": "Sah-mah-hah-nee"}, |
|
|
{"swahili": "Ndio", "english": "Yes", "pronunciation": "N-dee-oh"}, |
|
|
{"swahili": "Hapana", "english": "No", "pronunciation": "Hah-pah-nah"}, |
|
|
{"swahili": "Sawa", "english": "Okay", "pronunciation": "Sah-wah"} |
|
|
], |
|
|
"practice": "Practice using these phrases in simple conversations" |
|
|
}, |
|
|
"advanced": { |
|
|
"topic": "Cultural Expressions", |
|
|
"phrases": [ |
|
|
{"swahili": "Hakuna Matata", "english": "No worries", "pronunciation": "Hah-koo-nah Mah-tah-tah"}, |
|
|
{"swahili": "Pole pole", "english": "Slowly slowly", "pronunciation": "Poh-leh poh-leh"}, |
|
|
{"swahili": "Haraka haraka haina baraka", "english": "Hurry hurry has no blessing", "pronunciation": "Hah-rah-kah hah-rah-kah hai-nah bah-rah-kah"} |
|
|
], |
|
|
"practice": "Use these proverbs to sound like a native speaker" |
|
|
} |
|
|
} |
|
|
|
|
|
return lessons.get(level, lessons["beginner"]) |
|
|
|
|
|
def enhance_with_kiswahili(text: str, knowledge_base: KiswahiliKnowledgeBase = None) -> str: |
|
|
"""Enhanced version with video support""" |
|
|
if knowledge_base is None: |
|
|
knowledge_base = KiswahiliKnowledgeBase() |
|
|
|
|
|
enhanced = knowledge_base.generate_kiswahili_response(text) |
|
|
|
|
|
|
|
|
video_triggers = ['see', 'watch', 'view', 'visual', 'picture', 'image', 'video'] |
|
|
if any(trigger in text.lower() for trigger in video_triggers): |
|
|
video_suggestion = "🎬 **Kipaji cha Video**: Ninaweza kutengeneza video ya kitamaduni kwa ajili yako!" |
|
|
enhanced += f"\n\n{video_suggestion}" |
|
|
|
|
|
return enhanced |
|
|
|