သတင်းကုထုံး ဗဟုတက္ကသိုလ် စာကြည့်တိုက်
သင်္ချာ၊ ကွန်ပျူတာသိပ္ပံ၊ ဉာဏ်ရည်တုပညာ နှင့် ဥပဒေ ဆိုင်ရာ သုတ ရသ ဆောင်းပါးများ
Thursday, January 29, 2026
အေအိုင် နှင့် ပညာမျိုးကန်းမည့်အန္တရာယ်
Zebra Puzzle as CSP
နည်းသစ်ဉာဏ်ရည်တုပညာမိတ်ဆက်၊ အခန်း (၅)၊ ကန့်သတ်ချက်ပြေလည်စေရသော ပြဿနာများ၊ လေ့ကျင့်ခန်းများ (မေးခွန်း နှင့် အဖြေများ)၊ ပုစ္ဆာ အမှတ် ၅.၃၊ စာ-၂၆၀ ကိုကြည့်ပါ။
ဤပရိုဂရမ်အလုပ်လုပ်နိုင်စေရန် python-constraint package လိုအပ်ပါသည်။
Windows 11 ပေါ်ရှိ Spyder IDE တွင် python-constraint package ကို install လုပ်ရန် အလွယ်ကူဆုံး နည်းလမ်း။
နည်းလမ်း (၁) - Spyder အတွင်းမှ တိုက်ရိုက် Install ပြုလုပ်နည်း (အလွယ်ဆုံးနည်းလမ်း)
Spyder ဖွင့်ထားရင်းနဲ့ ပရိုဂရမ်အပြင်ထွက်စရာမလိုဘဲ Console ကနေ တိုက်ရိုက်သွင်းနိုင်ပါတယ်။
Spyder ကို ဖွင့်ပါ။
မျက်နှာပြင်ရဲ့ ညာဘက်အောက်ထောင့်နားမှာရှိတဲ့ IPython Console (အမည်းရောင် သို့မဟုတ် အဖြူရောင် စာရိုက်ကွက်) ကို ကြည့်ပါ။
ထို Console တွင် အောက်ပါ Command ကို ရိုက်ထည့်ပြီး Enter ခေါက်ပါ။ (ရှေ့ဆုံးက
!သင်္ကေတ ပါရပါမယ်)!pip install python-constraintကွန်ပျူတာက Internet ချိတ်ထားရင် download ဆွဲပြီး install လုပ်သွားပါလိမ့်မယ်။
Successfully installed python-constraint...ဆိုပြီး စာပေါ်လာရင် ရပါပြီ။
နည်းလမ်း (၂) - Anaconda Prompt မှတဆင့် Install ပြုလုပ်နည်း (ပိုမိုသေချာသောနည်းလမ်း)
ပထမနည်းလမ်းနဲ့ အဆင်မပြေရင် (သို့မဟုတ်) Error တက်နေရင် ဒီနည်းလမ်းကို သုံးပါ။
ကွန်ပျူတာရဲ့ Start Menu (Windows Key) ကို နှိပ်ပါ။
Search box မှာ "Anaconda Prompt" လို့ ရိုက်ရှာပြီး ဖွင့်လိုက်ပါ။ (အမည်းရောင် box ပေါ်လာပါမယ်)။
အဲဒီ Box ထဲမှာ အောက်ပါစာကို ရိုက်ထည့်ပြီး Enter ခေါက်ပါ။
pip install python-constraintInstall လုပ်ပြီးသွားရင် Box ကို ပိတ်လိုက်ပြီး Spyder ကို ပြန်ဖွင့် (Restart) လိုက်ပါ။
Install ဖြစ်မဖြစ် စမ်းသပ်ခြင်း
Install လုပ်ပြီးသွားရင် သေချာမသေချာ စစ်ဆေးဖို့အတွက် Spyder မှာ အောက်ပါ ကုဒ်တိုလေးကို ရိုက်ပြီး Run ကြည့်လိုက်ပါ။
import constraint
print("Installation Successful!")
Error မတက်ဘဲ "Installation Successful!" လို့ ပေါ်လာရင်တော့ python-constraint ကို စတင်အသုံးပြုပြီး မြင်းကျားပဟေဠိကို ဖြေရှင်းနိုင်ပါပြီ။
def solve_zebra_puzzle():
# ၁။ Knowledge Base အဖြစ် Problem ကို စတင်တည်ဆောက်ခြင်း
problem = Problem()
# ၂။ Variables (ကိန်းရှင်များ) သတ်မှတ်ခြင်း
# အိမ်နံပါတ် ၁ မှ ၅ ထိ (Domain)
houses = [1, 2, 3, 4, 5]
# အရည်အသွေးများ (Attributes)
nationalities = ["English", "Spaniard", "Irish", "Nigerian", "Japanese"]
colors = ["Red", "Green", "Ivory", "Yellow", "Blue"]
drinks = ["Coffee", "Tea", "Milk", "Orange Juice", "Water"]
pets = ["Dog", "Snails", "Fox", "Horse", "Zebra"]
sports = ["Cricket", "Go", "Judo", "Poker", "Polo"] # မူရင်းပဟေဠိ၏ ဆေးလိပ်အစား အားကစားဖြင့် အစားထိုးထားသည်
all_variables = nationalities + colors + drinks + pets + sports
# Variable တစ်ခုစီသည် အိမ်နံပါတ် ၁ မှ ၅ အတွင်းသာ ရှိရမည်
problem.addVariables(all_variables, houses)
# ၃။ Constraints (ကန့်သတ်ချက်များ) ထည့်သွင်းခြင်း
# အိမ်တစ်အိမ်စီတွင် အရောင်၊ လူမျိုး၊ အချိုရည်၊ တိရစ္ဆာန်၊ အားကစား တစ်မျိုးစီသာ ရှိရမည် (AllDifferent)
problem.addConstraint(AllDifferentConstraint(), nationalities)
problem.addConstraint(AllDifferentConstraint(), colors)
problem.addConstraint(AllDifferentConstraint(), drinks)
problem.addConstraint(AllDifferentConstraint(), pets)
problem.addConstraint(AllDifferentConstraint(), sports)
# ပေးထားသော အချက်အလက်များ (Facts) ကို ထည့်သွင်းခြင်း
# ၁။ အင်္ဂလိပ်လူမျိုးသည် အနီရောင်အိမ်တွင် နေထိုင်သည်။
problem.addConstraint(lambda eng, red: eng == red, ("English", "Red"))
# ၂။ စပိန်လူမျိုးသည် ခွေးတစ်ကောင် ပိုင်ဆိုင်သည်။
problem.addConstraint(lambda span, dog: span == dog, ("Spaniard", "Dog"))
# ၃။ အစိမ်းရောင်အိမ်ရှိ လူသည် ကော်ဖီ သောက်သည်။
problem.addConstraint(lambda green, coffee: green == coffee, ("Green", "Coffee"))
# ၄။ အိုင်းရစ်လူမျိုးသည် လက်ဖက်ရည် သောက်သည်။
problem.addConstraint(lambda irish, tea: irish == tea, ("Irish", "Tea"))
# ၅။ အစိမ်းရောင်အိမ်သည် ဆင်စွယ်ရောင်အိမ်၏ ညာဘက်တွင် တည်ရှိသည်။ (Green = Ivory + 1)
problem.addConstraint(lambda green, ivory: green == ivory + 1, ("Green", "Ivory"))
# ၆။ ဂိုး-GO ကစားသမားသည် ခရုများ ပိုင်ဆိုင်သည်။
problem.addConstraint(lambda go, snails: go == snails, ("Go", "Snails"))
# ၇။ အဝါရောင်အိမ်ရှိ လူသည် ကရစ်ကက် ကစားသည်။
problem.addConstraint(lambda yellow, cricket: yellow == cricket, ("Yellow", "Cricket"))
# ၈။ အလယ်အိမ် (တတိယအိမ်) တွင် နေထိုင်သူသည် နွားနို့ သောက်သည်။
problem.addConstraint(lambda milk: milk == 3, ("Milk",))
# ၉။ နိုင်ဂျီးရီးယားလူမျိုးသည် ပထမအိမ် (အိမ်နံပါတ် ၁) တွင် နေထိုင်သည်။
problem.addConstraint(lambda nigerian: nigerian == 1, ("Nigerian",))
# ၁၀။ ဂျူဒိုကစားသမားသည် မြေခွေးပိုင်ဆိုင်သူ၏ အိမ်ဘေးကပ်လျက် နေထိုင်သည်။ (Next to means abs(a-b) == 1)
problem.addConstraint(lambda judo, fox: abs(judo - fox) == 1, ("Judo", "Fox"))
# ၁၁။ ကရစ်ကတ်ကစားသမားသည် မြင်းပိုင်ဆိုင်သူ၏ အိမ်ဘေးကပ်လျက်တွင် နေထိုင်သည်။
problem.addConstraint(lambda cricket, horse: abs(cricket - horse) == 1, ("Cricket", "Horse"))
# ၁၂။ ပိုကာကစားသမားသည် လိမ္မော်ရည် သောက်သည်။
problem.addConstraint(lambda poker, oj: poker == oj, ("Poker", "Orange Juice"))
# ၁၃။ ဂျပန်လူမျိုးသည် ပိုလို ကစားသည်။
problem.addConstraint(lambda japanese, polo: japanese == polo, ("Japanese", "Polo"))
# ၁၄။ နိုင်ဂျီးရီးယားလူမျိုးသည် အပြာရောင်အိမ်၏ အိမ်ဘေးကပ်လျက်တွင် နေထိုင်သည်။
problem.addConstraint(lambda nigerian, blue: abs(nigerian - blue) == 1, ("Nigerian", "Blue"))
# ၄။ အဖြေရှာခြင်း (Get Solution)
solutions = problem.getSolutions()
# ၅။ ရလာသော အဖြေကို ရှင်းလင်းစွာ ဖော်ပြခြင်း
if not solutions:
print("No solution found!")
else:
# အဖြေတစ်ခုတည်းသာ ထွက်လေ့ရှိသည်
sol = solutions[0]
print(f"{'House':<10} {'Color':<10} {'Nation':<12} {'Drink':<15} {'Sport':<12} {'Pet':<10}")
print("-" * 70)
# အိမ်နံပါတ် ၁ မှ ၅ အထိ Loop ပတ်ပြီး စီခြင်း
for i in range(1, 6):
# လက်ရှိအိမ်နံပါတ် i နှင့် ကိုက်ညီသော variable များကို ရှာခြင်း
c = [k for k, v in sol.items() if v == i and k in colors][0]
n = [k for k, v in sol.items() if v == i and k in nationalities][0]
d = [k for k, v in sol.items() if v == i and k in drinks][0]
s = [k for k, v in sol.items() if v == i and k in sports][0]
p = [k for k, v in sol.items() if v == i and k in pets][0]
print(f"{i:<10} {c:<10} {n:<12} {d:<15} {s:<12} {p:<10}")
print("\n--- Final Answer ---")
# Zebra နှင့် Water ရှာခြင်း
zebra_owner = [k for k, v in sol.items() if k in nationalities and sol[k] == sol["Zebra"]][0]
water_drinker = [k for k, v in sol.items() if k in nationalities and sol[k] == sol["Water"]][0] # Water is implied as the remaining drink
print(f"Zebra lives in the house of the: {zebra_owner}")
print(f"The person who drinks Water is the: {water_drinker}")
if __name__ == "__main__":
solve_zebra_puzzle()
Sunday, January 25, 2026
Building A Justice Agent Part-05
ဒီအမှားဖြစ်ရတဲ့ အဓိကအကြောင်းရင်းကတော့ - AI ကိုပေးထားတဲ့ "သေဆုံးခြင်းဆိုင်ရာ စကားလုံးများ" (Keywords) စာရင်းထဲမှာ "death" (နမ်) ကျန်နေခဲ့ပြီး "dead" (နာမဝိသေသန) နဲ့ "died" (ကြိယာ) ပဲ ပါနေလို့ ဖြစ်ပါတယ်။
ကွန်ပျူတာက "dead" နဲ့ "death" ကို တူညီတယ်လို့ မသိပါဘူး။ စာလုံးပေါင်းမတူရင် မတူဘူးလို့ပဲ ယူဆပါတယ်။ မိတ်ဆွေပေးလိုက်တဲ့ စာသားမှာ "stabbed to death" လို့ပါပေမယ့်၊ AI ရဲ့ စာရင်းထဲမှာ "death" မပါတော့ "သေဆုံးခြင်းမရှိ" (No death occurred) လို့ မှားယွင်းဆုံးဖြတ်လိုက်တာ ဖြစ်ပါတယ်။
ဒါကြောင့် Keywords စာရင်းထဲမှာ "death" ကို ထပ်ဖြည့်ပြီး၊ Logic မှန်ကန်သွားအောင် ပြင်ဆင်ထားတဲ့ ကုဒ်အပြည့်အစုံက အောက်ပါအတိုင်း ဖြစ်နိုင်ပါတယ်။
Corrected Python Code
import re
class AdvancedLegalAI:
def __init__(self):
print("--- AI Legal Inference System Initialized ---")
print("System loaded with: Penal Code Section 300 & Private Defense Logic")
# အဆင့် (၁) - NLU (စာသားမှ အချက်အလက် ထုတ်နုတ်ခြင်း)
def extract_facts(self, text):
text = text.lower()
facts = {
"death_occurred": False, # လူသေမှု
"intent_to_kill": False, # သေစေလိုသောစိတ်
"intent_injury": False, # ဒဏ်ရာရစေလိုသောစိတ်
"sufficient_injury": False, # သေစေလောက်သော ဒဏ်ရာ
# For Provocation (Section 300 Exception 1)
"is_provoked": False,
"sudden_and_grave": False,
"loss_of_control": False,
# For Self-Defense (Section 96)
"under_imminent_threat": False, # ဘေးအန္တရာယ် ကျရောက်နေခြင်း
"act_was_defensive": False # ကာကွယ်ရန် ပြုလုပ်ခြင်း
}
# --- FIX APPLIED HERE (ပြင်ဆင်ချက်) ---
# "death" ကို keywords စာရင်းထဲ ထည့်လိုက်ပါသည်
death_keywords = ["died", "dead", "killed", "fatal", "death"]
# Regular Expression သုံးပြီး ရှာဖွေခြင်း (ပိုမိုတိကျစေရန်)
# \b သည် စကားလုံး၏ အစနှင့်အဆုံး (Word Boundary) ကို ဆိုလိုသည်
for word in death_keywords:
if re.search(r"\b" + re.escape(word) + r"\b", text):
facts["death_occurred"] = True
break
# Keywords for Intent (Mens Rea)
if any(w in text for w in ["intentionally", "purpose", "plan", "murdered"]):
facts["intent_to_kill"] = True
# "stabbed" ပါရင် ဒဏ်ရာရစေလိုမှုရော၊ သေစေလောက်တဲ့ဒဏ်ရာပါ True ပေးလိုက်မည်
if any(w in text for w in ["stabbed", "shot", "hit", "cut"]):
facts["intent_injury"] = True
facts["sufficient_injury"] = True
# Keywords for Provocation
if "provoked" in text or "insulted" in text:
facts["is_provoked"] = True
if "sudden" in text and "grave" in text:
facts["sudden_and_grave"] = True
if "lost control" in text or "anger" in text:
facts["loss_of_control"] = True
# Keywords for Self-Defense
if any(w in text for w in ["attacked", "danger", "threatened", "gunpoint"]):
facts["under_imminent_threat"] = True
if any(w in text for w in ["defend", "protect", "saved himself"]):
facts["act_was_defensive"] = True
return facts
# အဆင့် (၂) - Inference Engine (ဥပဒေ ယုတ္တိဗေဒ အဆုံးအဖြတ်)
def determine_verdict(self, facts):
# LOGIC LEVEL 1: Actus Reus (အပြုအမူ)
if not facts["death_occurred"]:
return "CASE DISMISSED: No death occurred. (လူသေဆုံးခြင်း မရှိပါ)"
# LOGIC LEVEL 2: General Exceptions (Self-Defense)
is_self_defense = facts["under_imminent_threat"] and facts["act_was_defensive"]
if is_self_defense:
return "NOT GUILTY: Act done in Private Defense (Section 96). (ကာကွယ်ခုခံပိုင်ခွင့်အရ အပြစ်မရှိပါ)"
# LOGIC LEVEL 3: Establish Murder Prima Facie (Mens Rea)
has_intent = facts["intent_to_kill"] or (facts["intent_injury"] and facts["sufficient_injury"])
if not has_intent:
return "NOT MURDER: Accidental or Negligent. (ရည်ရွယ်ချက် မရှိသဖြင့် လူသတ်မှု မမြောက်ပါ)"
# LOGIC LEVEL 4: Special Exceptions (Provocation)
valid_provocation = (facts["is_provoked"] and
facts["sudden_and_grave"] and
facts["loss_of_control"])
if valid_provocation:
return "REDUCED CHARGE: Culpable Homicide not amounting to Murder. (Provocation ကြောင့် လျော့ပေါ့)"
# LOGIC LEVEL 5: Default Verdict
return "GUILTY: Murder under Section 300. (လူသတ်မှု ထင်ရှားသည်)"
# Main Processing Function
def process_case(self, scenario_text):
print(f"\n[Input Scenario]: \"{scenario_text}\"")
# 1. Understand Facts
facts = self.extract_facts(scenario_text)
# 2. Print AI's Understanding (Debug View)
print(f"[AI Reasoning] Death Detected? -> {facts['death_occurred']}")
print(f"[AI Reasoning] Provocation Detected? -> {facts['is_provoked'] and facts['sudden_and_grave'] and facts['loss_of_control']}")
# 3. Final Judgment
verdict = self.determine_verdict(facts)
print(f"[FINAL VERDICT]: {verdict}")
print("-" * 60)
# --- စမ်းသပ်ခြင်း ---
ai = AdvancedLegalAI()
# Scenario: Provocation (ယခင်က အမှားပြခဲ့သော စာသား)
scenario = "The victim provoked John with sudden and grave insults. John lost control of his anger and stabbed the victim to death."
ai.process_case(scenario)
ဘာတွေ ပြင်လိုက်သလဲ?
Keyword ထပ်ဖြည့်ခြင်း:
death_keywordsစာရင်းထဲမှာ "death" ကို ထည့်လိုက်ပါတယ်။ ဒါကြောင့် "stabbed to death" ဆိုတဲ့ စာသားကို AI က ဖတ်မိပြီးdeath_occurred = Trueလို့ မှန်ကန်စွာ သတ်မှတ်ပေးနိုင်ပါပြီ။re(Regular Expression) အသုံးပြုခြင်း: ယခင်incommand အစားre.searchကို ပြောင်းသုံးထားပါတယ်။ ဒါက ပိုတိကျသလို၊import reကိုလည်း လက်တွေ့ အကျိုးရှိရှိ သုံးပြထားခြင်း ဖြစ်ပါတယ်။
ဒီကုဒ်ကို Run လိုက်ရင် အဖြေမှန်ဖြစ်တဲ့ "REDUCED CHARGE: Culpable Homicide not amounting to Murder" ကို ရရှိမှာ ဖြစ်ပါတယ်။
နောက်ဆက်တွဲ။
import re ဆိုတာ ဘာလဲ? re က ဘာကို ရည်ညွှန်းတာလဲ?
reဆိုသည်မှာ Regular Expression (အတိုကောက် RegEx) ကို ဆိုလိုခြင်း ဖြစ်ပါသည်။Regular Expression ဆိုသည်မှာ စာသားများ (Strings) အတွင်းတွင် မိမိလိုချင်သော ပုံစံ (Pattern) များကို ရှာဖွေရာတွင် အလွန်စွမ်းအားကြီးမားသော နည်းပညာတစ်ခု ဖြစ်ပါသည်။
ဥပမာ ရှင်းပြချက် -
သာမန် Python ကုဒ် (if "kill" in text) သည် "kill" ဆိုသည့် စာလုံးပါရင် အမှန်ပြပါလိမ့်မည်။ သို့သော် ပြဿနာမှာ "skilled" (ကျွမ်းကျင်သော) ဆိုသည့် စကားလုံးထဲတွင်လည်း "kill" ပါနေသဖြင့် AI က လူသတ်မှုဟု မှားယွင်း ကောက်ချက်ချသွားနိုင်ပါသည်။
re (Regular Expression) ကို သုံးလိုက်လျှင် -
re ကိုသုံးပြီး "kill" ဆိုသည့် စကားလုံး အတိအကျ (Whole Word) ကိုသာ ရှာပါ၊ ရှေ့နောက်တွင် အခြားစာလုံးများ မတွဲရ ဟု ခိုင်းနိုင်ပါသည်။ ၎င်းသည် ဥပဒေရေးရာ စကားလုံးများ ရှာဖွေရာတွင် အလွန်အရေးကြီးပါသည်။
**********************
Regular Expression (Regex) သည် စကားလုံးအတိအကျ (Word Boundary) ကို စစ်ဆေးပေးနိုင်သောကြောင့်၊ သာမန် if "word" in text နည်းလမ်းဖြင့် ရှာလျှင် အဓိပ္ပာယ်လွဲသွားနိုင်သည့် "လှည့်ကွက်" (Tricky) ဖြစ်စေမည့် Scenario (၃) ခုကို ရွေးထုတ်ပေးလိုက်ပါသည်။
ဒီ Scenario တွေကို လက်ရှိရေးထားတဲ့ ကုဒ်နဲ့ စမ်းသပ်ကြည့်ပါ။
Scenario 1: "The Dieting Soldier" (သေဆုံးမှု မှားယွင်းစစ်ဆေးခြင်း)
Input Text:
"The soldier is on a strict diet."သာမန် ရှာဖွေမှု (Simple Search) အမှား:
ကွန်ပျူတာက ဒီစာကြောင်းထဲမှာ "die" ဆိုတဲ့ စာလုံးပါမပါ ရှာပါလိမ့်မယ်။
Soldier (စစ်သား) ထဲမှာ "die" ပါနေပါတယ်။
Diet (အစားရှောင်ခြင်း) ထဲမှာလည်း "die" ပါနေပါတယ်။
ရလဒ်: "Death Occurred = True" (လူသေသွားတယ်) လို့ အဖြေမှား ထွက်လာပါလိမ့်မယ်။
Regex ရလဒ်:
\bdie\b(die သီးသန့်) ကို ရှာတဲ့အတွက် soldier နဲ့ diet ကို ကျော်သွားပြီး "False" (လူမသေပါ) လို့ အဖြေမှန် ထုတ်ပေးပါလိမ့်မယ်။
Scenario 2: "The Skilled Carpenter" (လူသတ်လိုစိတ် မှားယွင်းစစ်ဆေးခြင်း)
Input Text:
"John is a skilled carpenter."သာမန် ရှာဖွေမှု (Simple Search) အမှား:
ကွန်ပျူတာက "kill" (သတ်သည်) ဆိုတဲ့ စာလုံးကို ရှာပါမယ်။
Skilled (ကျွမ်းကျင်သော) ဆိုတဲ့ စကားလုံးထဲမှာ "kill" ပါနေပါတယ်။
ရလဒ်: "Intent to Kill = True" (သေစေလိုသော ရည်ရွယ်ချက်ရှိသည်) ဆိုပြီး လူသတ်သမားအဖြစ် မှားယွင်းသတ်မှတ်ပါလိမ့်မယ်။
Regex ရလဒ်:
\bkill\bကို ရှာတဲ့အတွက် skilled ကို ကျော်သွားပြီး "False" လို့ အဖြေမှန် ထွက်ပါမယ်။
Scenario 3: "The Stable Condition" (လက်နက်သုံးမှု မှားယွင်းစစ်ဆေးခြင်း)
Input Text:
"The patient is in a stable condition."သာမန် ရှာဖွေမှု (Simple Search) အမှား:
ကွန်ပျူတာက "stab" (ဓားဖြင့်ထိုးသည်) ဆိုတဲ့ စာလုံးကို ရှာပါမယ်။
Stable (တည်ငြိမ်သော) ဆိုတဲ့ စကားလုံးထဲမှာ "stab" ပါနေပါတယ်။
ရလဒ်: "Intent to Injury = True" (ဓားဖြင့်ထိုးရန် ရည်ရွယ်ချက်ရှိသည်) လို့ အဖြေမှား ထွက်ပါမယ်။
Regex ရလဒ်:
\bstab\bကို ရှာတဲ့အတွက် stable ကို ကျော်သွားပြီး "False" လို့ အဖြေမှန် ထွက်ပါမယ်။
စမ်းသပ်ရန် Python Code
အထက်ပါ Scenario ၃ ခုကို မိတ်ဆွေရဲ့ AI ပရိုဂရမ်မှာ ထည့်သွင်းစမ်းသပ်နိုင်ဖို့ ကုဒ်တိုလေး ရေးပေးလိုက်ပါတယ်။ (AdvancedLegalAI Class ရှိပြီးသားဖြစ်ရပါမယ်။)
# AI Agent ကို ခေါ်လိုက်ပါ
ai_test = AdvancedLegalAI()
# လှည့်ကွက်ပါသော Scenario များ
tricky_scenarios = [
"The soldier is on a strict diet.", # "die" in soldier/diet
"John is a skilled carpenter.", # "kill" in skilled
"The patient is in a stable condition." # "stab" in stable
]
print("\n--- TRICKY SCENARIO TESTS (Regex vs Simple Check) ---")
for text in tricky_scenarios:
print(f"\nTesting: '{text}'")
facts = ai_test.extract_facts(text)
# ရလဒ်များကို စစ်ဆေးခြင်း
if facts['death_occurred']:
print(">> ERROR: Wrongly detected 'DEATH'.")
elif facts['intent_to_kill']:
print(">> ERROR: Wrongly detected 'INTENT TO KILL'.")
elif facts['intent_injury']:
print(">> ERROR: Wrongly detected 'STABBING/INJURY'.")
else:
print(">> SUCCESS: AI correctly ignored tricky words. (မှန်ကန်ပါတယ်)")
ဒီ Scenario လေးတွေက import re နဲ့ \b (Word Boundary) အသုံးပြုခြင်းရဲ့ အရေးပါပုံကို ကောင်းကောင်း မီးမောင်းထိုးပြနိုင်ပါလိမ့်မယ်။
*****************************
အထက်မှာ ရှင်းပြခဲ့တဲ့ tricky_scenarios (လှည့်ကွက်ပါတဲ့ ဖြစ်စဉ်များ) ကို process_case function သုံးပြီး လက်တွေ့ စမ်းသပ်မယ့် ကုဒ်ကို ရေးပေးလိုက်ပါတယ်။
ဒီကုဒ်ကို အရင်ရေးထားတဲ့ AdvancedLegalAI Class ရဲ့ အောက်ဆုံးမှာ ကူးထည့်ပြီး Run လိုက်ရုံပါပဲ။
Tricky Scenarios Test Code
# AI Agent ကို ခေါ်ယူခြင်း
ai_judge = AdvancedLegalAI()
print("\n==================================================")
print(" TRICKY SCENARIOS TEST (Regular Expression) ")
print("==================================================\n")
# လှည့်ကွက်ပါသော ဖြစ်စဉ်များ
tricky_scenarios = [
# 1. "die" အသံထွက်ပါသော်လည်း လူသေမှုမဟုတ်သည့် ကိစ္စ (Soldier / Diet)
"The soldier is on a strict diet.",
# 2. "kill" ပါသော်လည်း လူသတ်မှုမဟုတ်သည့် ကိစ္စ (Skilled)
"John is a skilled carpenter.",
# 3. "stab" ပါသော်လည်း ဓားထိုးမှုမဟုတ်သည့် ကိစ္စ (Stable)
"The patient is in a stable condition."
]
# ဖြစ်စဉ်တစ်ခုချင်းစီကို process_case ဖြင့် စစ်ဆေးခြင်း
for i, scenario in enumerate(tricky_scenarios, 1):
print(f"--- Test Case {i} ---")
ai_judge.process_case(scenario)
မျှော်မှန်းရမည့် ရလဒ် (Expected Output)
အကယ်၍ re (Regular Expression) ကုဒ် အလုပ်လုပ်တယ်ဆိုရင်၊ အောက်ပါအတိုင်း "CASE DISMISSED" ဆိုတဲ့ အဖြေမှန်တွေ ထွက်လာပါလိမ့်မယ်။ (ဆိုလိုတာက AI က Diet ကို Died လို့မမြင်တော့ပါ၊ Skilled ကို Killed လို့ မမြင်တော့ပါ)။
==================================================
TRICKY SCENARIOS TEST (Regular Expression)
==================================================
--- Test Case 1 ---
[Input Scenario]: "The soldier is on a strict diet."
[AI Reasoning] Death Detected? -> False
[AI Reasoning] Provocation Detected? -> False
[FINAL VERDICT]: CASE DISMISSED: No death occurred. (လူသေဆုံးခြင်း မရှိပါ)
------------------------------------------------------------
--- Test Case 2 ---
[Input Scenario]: "John is a skilled carpenter."
[AI Reasoning] Death Detected? -> False
[AI Reasoning] Provocation Detected? -> False
[FINAL VERDICT]: CASE DISMISSED: No death occurred. (လူသေဆုံးခြင်း မရှိပါ)
------------------------------------------------------------
--- Test Case 3 ---
[Input Scenario]: "The patient is in a stable condition."
[AI Reasoning] Death Detected? -> False
[AI Reasoning] Provocation Detected? -> False
[FINAL VERDICT]: CASE DISMISSED: No death occurred. (လူသေဆုံးခြင်း မရှိပါ)
------------------------------------------------------------
သုံးသပ်ချက် - တွေ့ရတဲ့အတိုင်း AI ဟာ...
Soldier/Diet ထဲမှာ 'die' ပါပေမယ့် လူမသေဘူး (Death Detected -> False) လို့ သိပါတယ်။
Skilled ထဲမှာ 'kill' ပါပေမယ့် လူသတ်ဖို့ရည်ရွယ်ချက် (Intent) မရှိဘူး၊ လူမသေဘူးလို့ သိပါတယ်။
Stable ထဲမှာ 'stab' ပါပေမယ့် ဓားနဲ့မထိုးဘူးလို့ သိပါတယ်။
ဒါဟာ Regular Expression ရဲ့ \b (Word Boundary) စွမ်းဆောင်ရည်ကြောင့် ဖြစ်ပါတယ်။
Knowledge Representation Problem 10.2 Part II
Part II
Make the necessary additions or changes to your knowledge base from the previous exercise so that the questions that follow can be answered. Include in your report a discussion of your changes, explaining why they were needed, whether they were minor or major, and what kinds of questions would necessitate further changes.
a. Are there other people in Safeway while John is there? [Yes—staff!]
b. Is John a vegetarian? [No]
c. Who owns the deodorant in Safeway? [Safeway Corporation]
d. Did John have an ounce of ground beef? [Yes]
e. Does the Shell station next door have any gas? [Yes]
f. Do the tomatoes fit in John’s car trunk? [Yes]
အထက်ပါမေးခွန်းတွေကို ဖြေဆိုနိုင်ဖို့အတွက် ယခင် Knowledge Base ကို ပြုပြင်ပြောင်းလဲမှုများ၊ ထပ်ဖြည့်စွက်ချက်များနှင့် ဆွေးနွေးချက်များကို အောက်ပါအတိုင်း လုပ်ဆောင်ကြည့်ပါမယ်။
၁။ ပြုပြင်ပြောင်းလဲမှုများနှင့် ဆွေးနွေးချက် (Discussion of Changes)
ယခင် Knowledge Base (KB) သည် "John စျေးဝယ်ထွက်ခြင်း" ဆိုသည့် အဓိက ဖြစ်စဉ်နှင့် ပတ်သက်သော ရိုးရှင်းသည့် ဆက်နွယ်မှုများ (Relations) ကိုသာ အာရုံစိုက်ခဲ့ပါသည်။ ယခု မေးခွန်းအသစ်များကို ဖြေဆိုရန်အတွက်မူ အောက်ပါ ကဏ္ဍသစ်များကို KB တွင် ထပ်ဖြည့်ရန် လိုအပ်ခဲ့ပါသည်။
လူမှုရေးရာနှင့် စီးပွားရေးရာ အခန်းကဏ္ဍများ (Social & Commercial Roles):
မေးခွန်း (a) အတွက် "ဆိုင်ဝန်ထမ်း" (Staff) ဆိုသည့် Concept ကို ထည့်သွင်းရသည်။ ဆိုင်ဖွင့်ထားလျှင် ဝန်ထမ်းရှိစမြဲဖြစ်ကြောင်း Rule ထည့်ရသည်။
မေးခွန်း (c) အတွက် "ကုန်ပစ္စည်း ပိုင်ဆိုင်မှု" (Inventory Ownership) သဘောတရား လိုအပ်လာသည်။ ဝယ်သူ မဝယ်ရသေးသော ပစ္စည်းသည် ဆိုင်ရှင် (Corporation) ပိုင်ကြောင်း သတ်မှတ်ရသည်။
အစားအသောက် စည်းမျဉ်းများ (Dietary Constraints):
မေးခွန်း (b) အတွက် "သက်သတ်လွတ်" (Vegetarian) ဟူသည် အသားမစားသူ ဖြစ်ကြောင်းနှင့်၊ John အမဲသားဝယ်လိုက်သည့်အတွက် သက်သတ်လွတ်သမား မဟုတ်နိုင်ကြောင်း ဆင်ခြင်နိုင်စွမ်း (Reasoning) ထည့်သွင်းရသည်။
အရေအတွက်နှင့် ယူနစ် တွက်ချက်ခြင်း (Quantities & Units):
မေးခွန်း (d) အတွက် "၁ ပေါင်တွင် ၁၆ အောင်စ ရှိသည်" ဆိုသော သင်္ချာအသိနှင့်၊ "အများကြီး ပိုင်ဆိုင်လျှင် အနည်းငယ်ကိုလည်း ပိုင်ဆိုင်သည်" (Part-Whole Reasoning) ဆိုသော Logic လိုအပ်လာသည်။
ရူပဗေဒဆိုင်ရာ ခန့်မှန်းချက်များ (Physical & Spatial Constraints):
မေးခွန်း (f) အတွက် ခရမ်းချဉ်သီးအိတ်၏ အရွယ်အစားနှင့် ကားနောက်ဖုံး (Trunk) ၏ အရွယ်အစားကို နှိုင်းယှဉ်နိုင်စွမ်း (Size Comparison) လိုအပ်သည်။ "သေးငယ်သော အရာသည် ကြီးမားသော နေရာလွတ်ထဲသို့ ဝင်ဆံ့သည်" ဟူသော Rule ထည့်ရသည်။
စီးပွားရေးဆိုင်ရာ ပုံသေယူဆချက်များ (Default Business Logic):
မေးခွန်း (e) အတွက် ဓာတ်ဆီဆိုင် (Shell) သည် ဓာတ်ဆီ ရောင်းသည်ဟု အလိုအလျောက် သိရှိနိုင်မည့် Knowledge လိုအပ်သည်။
ပြောင်းလဲမှု ပမာဏ (Minor vs. Major): ၎င်းပြောင်းလဲမှုများကို Major (အဓိကကျသော) ပြောင်းလဲမှုများဟု ဆိုနိုင်ပါသည်။ အဘယ်ကြောင့်ဆိုသော် ယခင်က Semantic Network (အမည်နာမ ဆက်နွယ်မှု) အဆင့်သာ ရှိရာမှ၊ ယခုအခါ Physical Reasoning (အရွယ်အစား နှိုင်းယှဉ်ခြင်း) နှင့် Mathematical Reasoning (ယူနစ်ပြောင်းလဲခြင်း) ကဲ့သို့သော နယ်ပယ်သစ် Logic များကိုပါ ထပ်ဖြည့်သွင်းလိုက်ရသောကြောင့် ဖြစ်ပါသည်။
၂။ Python Implementation (Updated AI Agent)
အောက်ပါ Python ကုဒ်သည် မေးခွန်းအသစ်များအတွက် လိုအပ်သော Logic များကို ဖြည့်စွက်ရေးသားထားသော ပရိုဂရမ် ဖြစ်ပါသည်။
class AdvancedKnowledgeBase:
def __init__(self):
# 1. Ontology (အမျိုးအစားများ - တိုးချဲ့ထားသည်)
self.taxonomy = {
"Ground Beef": ["Meat", "Food", "Perishable"],
"Tomatoes": ["Vegetable", "Food", "Produce", "SmallObject"],
"Deodorant": ["Toiletry", "Personal Care", "SmallObject"],
"Safeway": ["Supermarket", "Store", "Place", "Corporation"],
"Shell": ["Gas Station", "Store", "Place"],
"John": ["Person", "Agent", "Shopper", "CarOwner"],
"Staff": ["Person", "Employee"],
"Car Trunk": ["Container", "LargeSpace"],
"Gas": ["Fuel", "Liquid"]
}
# 2. Facts Database (အဖြစ်အပျက်များ)
self.facts = {
"John": {
"action": ["went", "bought"],
"bought": {"Tomatoes": 2.0, "Ground Beef": 1.0}, # Amounts in lbs
"location": "Safeway",
"has_property": ["CarOwner"] # John owns a car
},
"Safeway": {
"inventory": ["Deodorant", "Tomatoes", "Ground Beef"],
"staff_present": True # Stores have staff
},
"Shell": {
"primary_product": "Gas",
"location_context": "Next door"
}
}
# 3. Unit Conversions (ယူနစ် အချိုးအဆများ)
self.units = {
"lb_to_oz": 16.0 # 1 lb = 16 oz
}
# Helper: Check Is-A relationship
def is_a(self, entity, category):
if entity in self.taxonomy:
return category in self.taxonomy[entity]
return False
# --- New Reasoning Functions for Updated Questions ---
def query_a_others_in_store(self, store):
# Logic: If Store is open/operational -> Staff must be there
if self.facts[store].get("staff_present"):
return "Yes (Staff/Employees are required to run the store)"
return "Unknown"
def query_b_is_vegetarian(self, person):
# Logic: Check purchases. If Meat bought -> Not Vegetarian
purchases = self.facts[person].get("bought", {})
for item in purchases:
if self.is_a(item, "Meat"):
return f"No (He bought {item}, vegetarians do not eat meat)"
return "Yes (Unknown, but no meat purchased)"
def query_c_deodorant_owner(self, item, store):
# Logic: Unsold items in a store belong to the store/corporation
# Note: John did NOT buy deodorant in our facts
return f"{store} Corporation (Unsold inventory belongs to the business)"
def query_d_has_ounce_beef(self, person):
# Logic: Bought 1 lb. 1 lb = 16 oz. Since 16 > 1, he has 1 oz.
purchases = self.facts[person].get("bought", {})
if "Ground Beef" in purchases:
amount_lbs = purchases["Ground Beef"]
amount_oz = amount_lbs * self.units["lb_to_oz"]
if amount_oz >= 1.0:
return f"Yes (He has {amount_oz} oz, which includes 1 oz)"
return "No"
def query_e_shell_has_gas(self, store):
# Logic: Business type implies inventory of primary product
if self.facts[store].get("primary_product") == "Gas":
return "Yes (Gas stations by definition stock gas)"
return "Unknown"
def query_f_tomatoes_fit_trunk(self, item, container_owner):
# Logic: Size(SmallObject) < Size(LargeSpace/CarTrunk)
# 1. Check if item is small
is_small = self.is_a(item, "SmallObject") or self.is_a(item, "Produce")
# 2. Check if owner has a car/trunk
has_car = "CarOwner" in self.facts[container_owner]["has_property"]
if is_small and has_car:
return "Yes (2 lbs of tomatoes is small enough to fit in a standard car trunk)"
return "Unknown"
# --- AI Agent Interface ---
class UpdatedSmartAgent:
def __init__(self):
self.kb = AdvancedKnowledgeBase()
print("--- Updated AI Agent Initialized ---\n")
def answer_new_questions(self):
kb = self.kb
print("Q(a): Are there other people in Safeway while John is there?")
print(f"Ans: {kb.query_a_others_in_store('Safeway')}\n")
print("Q(b): Is John a vegetarian?")
print(f"Ans: {kb.query_b_is_vegetarian('John')}\n")
print("Q(c): Who owns the deodorant in Safeway?")
print(f"Ans: {kb.query_c_deodorant_owner('Deodorant', 'Safeway')}\n")
print("Q(d): Did John have an ounce of ground beef?")
print(f"Ans: {kb.query_d_has_ounce_beef('John')}\n")
print("Q(e): Does the Shell station next door have any gas?")
print(f"Ans: {kb.query_e_shell_has_gas('Shell')}\n")
print("Q(f): Do the tomatoes fit in John’s car trunk?")
print(f"Ans: {kb.query_f_tomatoes_fit_trunk('Tomatoes', 'John')}\n")
# --- Execution ---
if __name__ == "__main__":
agent = UpdatedSmartAgent()
agent.answer_new_questions()
ပရိုဂရမ်၏ အလုပ်လုပ်ပုံ သုံးသပ်ချက်
ဝန်ထမ်းရှိကြောင်း ဆင်ခြင်ခြင်း (Q-a):
Safeway၏ Facts ထဲတွင်staff_present: Trueဟု သတ်မှတ်ထားခြင်းအားဖြင့်၊ လူမရှိပါက ဆိုင်ဖွင့်၍ မရနိုင်ကြောင်း AI က နားလည်စေသည်။သက်သတ်လွတ် (Q-b): John ဝယ်သော
Ground Beefသည်Meatဖြစ်ကြောင်း Taxonomy တွင် ကြည့်ပြီး၊ အသားဝယ်သူသည် သက်သတ်လွတ်မဟုတ်ကြောင်း ကောက်ချက်ချသည်။ပိုင်ဆိုင်မှု (Q-c): John ၏
boughtစာရင်းတွင် Deodorant မပါပါ။ ထို့ကြောင့် စတိုးဆိုင်ထဲရှိ ပစ္စည်းသည်Safewayပိုင်ဆိုင်ကြောင်း AI က ဖြေဆိုသည်။ယူနစ်တွက်ချက်ခြင်း (Q-d):
boughtစာရင်းတွင် 1 lb ရှိသည်ကို16 ozဟု ပြောင်းလဲတွက်ချက်ပြီး၊ ၁ အောင်စထက်များသည့်အတွက် "Yes" ဟု ဖြေဆိုသည်။အရွယ်အစား နှိုင်းယှဉ်ခြင်း (Q-f): ခရမ်းချဉ်သီး ၂ ပေါင်သည်
SmallObjectဖြစ်ပြီး၊ ကားနောက်ဖုံး (Trunk) သည်LargeSpaceဖြစ်သည်ဟု Taxonomy တွင် သတ်မှတ်ထားသဖြင့် ဝင်ဆံ့ကြောင်း (Fit) ဆုံးဖြတ်နိုင်သည်။
Knowledge Representation Problem 10.2
နည်းသစ်ဉာဏ်ရည်တုပညာ အဆင့်မြင့်သီအိုရီများ၊ အခန်း (၁၀)၊ စာမျက်နှာ-၅၂ ရှိ Problem 10.2 ကို ကြည့်ပါ။
"John bought tomatoes" ပြဿနာသည် Knowledge Representation (အသိပညာကို ကိုယ်စားပြုခြင်း) နှင့် Common Sense Reasoning (သာမန် အသိဉာဏ်ဖြင့် ဆင်ခြင်ခြင်း) ကို စမ်းသပ်သော ဂန္တဝင် ပြဿနာတစ်ခု ဖြစ်ပါသည်။
ဤပြဿနာကို ဖြေရှင်းရန် လူသားတစ်ယောက်၏ ဦးနှောက်ထဲတွင်ရှိသော "နောက်ခံ ဗဟုသုတများ" (Background Knowledge) ကို ကွန်ပျူတာ နားလည်မည့် Logic ပုံစံသို့ ပြောင်းလဲရပါမည်။
အောက်ပါ အဆင့် (၃) ဆင့်ဖြင့် တည်ဆောက်ပြပါမည်။
အဆင့် (၁) - Knowledge Base (KB) တည်ဆောက်ခြင်း
ပထမဦးစွာ စာကြောင်းပါ အချက်အလက်များနှင့် လိုအပ်သော နောက်ခံဗဟုသုတများကို အမျိုးအစား ခွဲခြားသတ်မှတ်ကြပါစို့။
၁. Ontology (အမျိုးအစား ခွဲခြားခြင်း)
Entities (အရာဝတ္ထုများ):
John(Person, Agent)Safeway(Supermarket, Location)Tomatoes(Vegetable/Fruit, Food)Ground Beef(Meat, Food)Money / Credit Card(Payment Method)
Events (ဖြစ်စဉ်များ):
Buying(Transaction) - ပိုက်ဆံပေးပြီး ပစ္စည်းယူခြင်း။Going(Movement) - နေရာတစ်ခုသို့ သွားခြင်း။
Relations (ဆက်နွယ်မှုများ):
IsA(Object, Class)- အမျိုးအစား သတ်မှတ်ခြင်း။Sells(Store, Item)- ဆိုင်က ဘာရောင်းသလဲ။Owns(Person, Item)- ပိုင်ဆိုင်မှု။Location(Person, Place)- လူတစ်ယောက် ဘယ်ရောက်နေသလဲ။
၂. Logical Assertions (အချက်အလက်များကို Logic ပြောင်းခြင်း)
Scenario Facts (ဇာတ်လမ်းပါ အချက်အလက်များ):
Did(John, Go_to(Safeway))Did(John, Buy(Tomatoes, 2_lbs))Did(John, Buy(Ground_Beef, 1_lb))Time(Yesterday)
Background Knowledge (လိုအပ်သော နောက်ခံဗဟုသုတများ - Rules):
Buying Rule:
IFPerson buys ItemTHENPerson owns ItemANDPerson pays Money.Wealth Rule:
IFPerson buys ItemTHENPerson has less money.Meat Rule:
IFItem is Ground BeefTHENItem is Meat.Produce Rule:
IFItem is TomatoTHENItem is grown (not made in factory).Location Rule:
IFPerson A is at Place XANDPerson B is at Place Xat same timeTHENThey likely see each other.Store Rule:
IFStore is SupermarketTHENStore sells Food, Toiletries, etc.
အဆင့် (၂) - Reasoning Chain (ဆင်ခြင်စဉ်းစားပုံ အဆင့်ဆင့်)
မေးခွန်းများကို ဖြေဆိုရန် Logic စီးဆင်းပုံမှာ အောက်ပါအတိုင်း ဖြစ်သည်။
a. Is John a child or an adult?
Reasoning: Grocery shopping is typically a household responsibility.
Default Assumption-> John is an Adult.
c. Did John buy any meat?
Reasoning: John bought
Ground Beef.Ground BeefIS-AMeat. Therefore -> Yes.
e. Are the tomatoes made in the supermarket?
Reasoning:
TomatoesAREProduce.ProduceisGrown(Farming), notManufactured. Supermarkets sell goods, they don't grow them inside. Therefore -> No.
i. Does John have less money...?
Reasoning: Action is
Buying.Buyingrequires exchange of value (Money for Goods). Therefore -> Yes.
အဆင့် (၃) - Python Implementation (AI Agent)
ဤပရိုဂရမ်တွင် Knowledge Graph ပုံစံ အတိုကောက်တည်ဆောက်ထားပြီး၊ မေးခွန်းများကို ဖြေရှင်းနိုင်သော Inference Engine တစ်ခု ပါဝင်ပါသည်။
class KnowledgeBase:
def __init__(self):
# 1. Classes / Ontology (အမျိုးအစားများ)
self.taxonomy = {
"Ground Beef": ["Meat", "Food", "Perishable"],
"Tomatoes": ["Vegetable", "Food", "Produce"],
"Deodorant": ["Toiletry", "Personal Care"],
"Safeway": ["Supermarket", "Store", "Place"],
"John": ["Person", "Agent", "Shopper"],
"Mary": ["Person", "Agent"]
}
# 2. Facts Database (အဖြစ်အပျက်များ)
self.facts = {
"John": {
"action": ["went", "bought"],
"bought": {"Tomatoes": "2 lbs", "Ground Beef": "1 lb"},
"location": "Safeway",
"time": "Yesterday"
},
"Safeway": {
"sells_categories": ["Food", "Toiletry", "Household"],
"manufactures_produce": False
}
}
# 3. Rules (စည်းမျဉ်းများ - Dictionary ပုံစံဖြင့် သိမ်းဆည်းခြင်း)
self.rules = {
"buying_implies_ownership": True,
"buying_requires_money": True,
"buying_reduces_wealth": True,
"supermarket_sells_toiletries": True,
"co_location_implies_visibility": True
}
# Helper function to check inheritance (Is-A relationship)
def is_a(self, entity, category):
if entity in self.taxonomy:
return category in self.taxonomy[entity]
return False
# --- Reasoning Functions for specific questions ---
def query_a_age_group(self, person):
# Default Logic: Shoppers are typically adults
if "Shopper" in self.taxonomy[person]:
return "Adult (Based on default assumption for shoppers)"
return "Unknown"
def query_b_has_tomatoes(self, person):
# Logic: Bought implies ownership
purchases = self.facts[person].get("bought", {})
if "Tomatoes" in purchases:
return f"Yes, he bought {purchases['Tomatoes']}."
return "No"
def query_c_bought_meat(self, person):
# Logic: Check all purchases -> Check if any purchase Is-A Meat
purchases = self.facts[person].get("bought", {})
for item in purchases:
if self.is_a(item, "Meat"):
return f"Yes (Because he bought {item}, which is Meat)"
return "No"
def query_d_saw_mary(self, person1, person2):
# Logic: If Location(P1) == Location(P2) -> Saw each other
loc1 = self.facts[person1].get("location")
# Assuming Mary was there as per question hypothesis
loc2 = "Safeway"
if loc1 == loc2 and self.rules["co_location_implies_visibility"]:
return "Yes (Shared location implies visibility)"
return "No"
def query_e_tomatoes_made_in_store(self):
# Logic: Tomatoes are Produce -> Produce is grown, not made in store
if self.is_a("Tomatoes", "Produce") and not self.facts["Safeway"]["manufactures_produce"]:
return "No (Tomatoes are produce/grown, not manufactured in-store)"
return "Yes"
def query_f_purpose_of_tomatoes(self):
# Logic: Tomatoes Is-A Food -> Food is for Eating
if self.is_a("Tomatoes", "Food"):
return "Eat them (Default purpose of Food)"
return "Unknown"
def query_g_sells_deodorant(self, store):
# Logic: Store Is-A Supermarket -> Supermarket sells Toiletries -> Deodorant Is-A Toiletry
if self.is_a(store, "Supermarket"):
if "Toiletry" in self.facts[store]["sells_categories"] and self.is_a("Deodorant", "Toiletry"):
return "Yes (Supermarkets sell toiletries)"
return "No"
def query_h_brought_money(self, person):
# Logic: Did person Buy? -> Buying requires money
if "bought" in self.facts[person]["action"] and self.rules["buying_requires_money"]:
return "Yes (Buying transaction necessitates payment method)"
return "No"
def query_i_less_money(self, person):
# Logic: Did person Buy? -> Buying reduces wealth
if "bought" in self.facts[person]["action"] and self.rules["buying_reduces_wealth"]:
return "Yes (Exchange of assets for goods reduces liquid money)"
return "No"
# --- AI Agent Interface ---
class SmartAgent:
def __init__(self):
self.kb = KnowledgeBase()
print("--- AI Agent Initialized with Commonsense Knowledge Base ---\n")
def answer_questions(self):
kb = self.kb
print("Q(a): Is John a child or an adult?")
print(f"Ans: {kb.query_a_age_group('John')}\n")
print("Q(b): Does John now have at least two tomatoes?")
print(f"Ans: {kb.query_b_has_tomatoes('John')}\n")
print("Q(c): Did John buy any meat?")
print(f"Ans: {kb.query_c_bought_meat('John')}\n")
print("Q(d): If Mary was buying tomatoes at the same time, did he see her?")
print(f"Ans: {kb.query_d_saw_mary('John', 'Mary')}\n")
print("Q(e): Are the tomatoes made in the supermarket?")
print(f"Ans: {kb.query_e_tomatoes_made_in_store()}\n")
print("Q(f): What is John going to do with the tomatoes?")
print(f"Ans: {kb.query_f_purpose_of_tomatoes()}\n")
print("Q(g): Does Safeway sell deodorant?")
print(f"Ans: {kb.query_g_sells_deodorant('Safeway')}\n")
print("Q(h): Did John bring some money or a credit card?")
print(f"Ans: {kb.query_h_brought_money('John')}\n")
print("Q(i): Does John have less money after going to the supermarket?")
print(f"Ans: {kb.query_i_less_money('John')}\n")
# --- Execution ---
if __name__ == "__main__":
agent = SmartAgent()
agent.answer_questions()
ပရိုဂရမ်၏ အနှစ်သာရ သုံးသပ်ချက်
Taxonomy (မျိုးရိုးခွဲခြားခြင်း):
Ground Beefသည်Meatဖြစ်သည်၊Safewayသည်Supermarketဖြစ်သည် စသဖြင့် အဆင့်ဆင့် သတ်မှတ်ထားခြင်းကြောင့်၊ "John အမဲသား ဝယ်လား?" ဟု မေးသောအခါ ပရိုဂရမ်က John ဝယ်တာGround Beef၊Ground BeefကMeatဖြစ်တယ် ဆိုတာကို ဆက်စပ်တွေးခေါ် (Infer) နိုင်သွားသည်။Default Reasoning: မေးခွန်း (a) နှင့် (f) အတွက် တိကျသော အချက်အလက် မရှိသော်လည်း၊ "စျေးဝယ်သူသည် လူကြီးဖြစ်လေ့ရှိသည်"၊ "အစားအစာသည် စားရန်ဖြစ်သည်" ဆိုသော ပုံသေယူဆချက် (Defaults) များကို အသုံးပြုထားသည်။
Logical Rules: ဝယ်ယူခြင်း (Buying) သည် ပိုက်ဆံကုန်သည်၊ ပိုင်ဆိုင်မှု ပြောင်းလဲသည် ဆိုသော စည်းမျဉ်းများကို
rulesထဲတွင် ထည့်သွင်းထားသဖြင့် မေးခွန်း (h) နှင့် (i) ကို တိကျစွာ ဖြေဆိုနိုင်ခြင်း ဖြစ်သည်။


