Monday, January 19, 2026

Building A Justice Agent Part-03

 AIMA စာအုပ်ရဲ့ အနှစ်သာရဖြစ်တဲ့ "Logic" နဲ့ "Programming" ကို ပေါင်းစပ်ပြီး လက်တွေ့အလုပ်လုပ်နိုင်သော AI စနစ်တစ်ခု တည်ဆောက်ကြည့်ပါမယ်။

ဒီစနစ်ကို အပိုင်း (၃) ပိုင်း ခွဲခြားတည်ဆောက်မယ်။

  1. Natural Language Understanding (NLU): လူသားဘာသာစကား (English Text) ကို ကွန်ပျူတာနားလည်သော အချက်အလက် (Facts) အဖြစ် ပြောင်းလဲခြင်း။

  2. Knowledge Base (KB): ရာဇသတ်ကြီး ပုဒ်မ ၃၀၀ ပါ ဥပဒေစည်းမျဉ်းများကို သိမ်းဆည်းထားခြင်း။

  3. Inference Engine: ရရှိလာသော အချက်အလက်များနှင့် ဥပဒေစည်းမျဉ်းများကို တိုက်ဆိုင်စစ်ဆေးပြီး အဆုံးအဖြတ်ပေးခြင်း။

အောက်ပါ Python ပရိုဂရမ်ဟာ၊ အင်္ဂလိပ်ဘာသာနဲ့ ပေးလိုက်တဲ့ အမှုဖြစ်စဉ် (Scenario) ကို ဖတ်ရှုပြီး၊ လူသတ်မှု မြောက်/မမြောက် စစ်ဆေးပေးမဲ့ အသေးစား အေအိုင်အေးဂျင့် တစ်ခုဖြစ်တယ်။


အဆင့် (၁) - Knowledge Representation (ဒေတာတည်ဆောက်ပုံ)

First-Order Logic (FOL) မှာ သုံးခဲ့တဲ့ Does(x, a), Intends(x) စသည်တို့ကို Python Class နဲ့ Dictionary များအဖြစ် ပြောင်းလဲသတ်မှတ်မယ်။


class LegalCase:
    def __init__(self, description):
        self.description = description
        # Logic Variables (Facts extracted from text)
        self.facts = {
            "act_caused_death": False,      # Actus Reus (လူသေမှု)
            "intent_to_kill": False,        # Mens Rea 1 (သေစေလိုသော စိတ်)
            "intent_bodily_injury": False,  # Mens Rea 2 (ဒဏ်ရာရစေလိုသော စိတ်)
            "injury_sufficient_to_kill": False, # ဒဏ်ရာသည် သေစေနိုင်လောက်ခြင်း
            "is_provoked": False,           # ရန်စခံရခြင်း (Exception)
            "is_sudden_and_grave": False,   # ရန်စမှု ကြီးလေး/ရုတ်တရက်ဖြစ်ခြင်း
            "lost_self_control": False      # စိတ်မထိန်းနိုင်ဖြစ်ခြင်း
        }



အဆင့် (၂) - Input Processing (စာသားမှ Logic သို့ ပြောင်းခြင်း)

လက်တွေ့ကမ္ဘာမှာ ဒီအဆင့်အတွက် LLM (ဥပမာ Gemini API) သို့မဟုတ် NLP models တွေကို သုံးရမှာဖြစ်ပေမဲ့၊ ယခု ပေးတဲ့ ဥပမာမှာတော့၊ Logic အလုပ်လုပ်ပုံကို မြင်သာစေဖို့  Rule-based Parser (keyword ရှာဖွေသည့်စနစ်) ကို ရေးပြမှာဖြစ်တယ်။ ၎င်းဟာ User ပေးလိုက်တဲ့ English Text ထဲက (အမှုနဲ့ဆိုင်တဲ့) အဓိကစကားလုံးတွေကို ရှာပြီး Logic တန်ဖိုးများ ဖြည့်သွင်းပေးမှာဖြစ်တယ်။

def extract_facts_from_text(self):
        # စာသားကို အသေးလုံးပြောင်းပြီး စစ်ဆေးမည်
        text = self.description.lower()
        # 1. Check Actus Reus (သေဆုံးမှု ရှိမရှိ)
        if "dead" in text or "died" in text or "killed" in text:
            self.facts["act_caused_death"] = True
        # 2. Check Mens Rea (ရည်ရွယ်ချက်)
        if "intentionally" in text or "planned" in text or "purpose" in text:
            self.facts["intent_to_kill"] = True
        
        # ဒဏ်ရာရစေလိုမှုနှင့် ထိုဒဏ်ရာ၏ ပြင်းထန်မှု
        if "stabbed" in text or "shot" in text or "hit" in text:
            self.facts["intent_bodily_injury"] = True
            # ရိုးရှင်းစေရန် 'stabbed'/'shot' ပါလျှင် သေစေလောက်သည်ဟု ယူဆမည်
            self.facts["injury_sufficient_to_kill"] = True 
        # 3. Check Exceptions (ခြွင်းချက်များ - Provocation)
        if "provoked" in text or "insulted" in text:
            self.facts["is_provoked"] = True
        
        if "suddenly" in text and "grave" in text:
            self.facts["is_sudden_and_grave"] = True
            
        if "lost control" in text or "anger" in text:
            self.facts["lost_self_control"] = True
            
        print("\n--- Extracted Facts (AI နားလည်ထားသော အချက်အလက်များ) ---")
        for key, value in self.facts.items():
            print(f"{key}: {value}")

အဆင့် (၃) - Inference Engine (ပုဒ်မ ၃၀၀၊ လူသတ်မှု မြောက်/မမြောက် ကို အဆုံးအဖြတ်ပေးခြင်း)

def judge_case(self):
        facts = self.facts
        
        # Step A: Actus Reus Check
        if not facts["act_caused_death"]:
            return "NOT MURDER (လူသေဆုံးခြင်း မရှိပါ)"
        # Step B: Mens Rea Check (Section 300 Definition)
        # Condition 1: Intention to cause death
        has_intent_to_kill = facts["intent_to_kill"]
        
        # Condition 2: Intention to cause injury + Injury is sufficient to kill
        has_fatal_injury_intent = facts["intent_bodily_injury"] and facts["injury_sufficient_to_kill"]
        
        # Mens Rea ပြည့်စုံမှု ရှိမရှိ
        is_prima_facie_murder = has_intent_to_kill or has_fatal_injury_intent
        if not is_prima_facie_murder:
             return "CULPABLE HOMICIDE NOT AMOUNTING TO MURDER (ရည်ရွယ်ချက်မခိုင်လုံသဖြင့် လူသတ်မှု မမြောက်ပါ)"
        # Step C: Exception Check (Grave and Sudden Provocation)
        # ခြွင်းချက် အကျုံးဝင်ရန် အချက် ၃ ချက်လုံး ပြည့်စုံရမည်
        exception_applies = (
            facts["is_provoked"] and 
            facts["is_sudden_and_grave"] and 
            facts["lost_self_control"]
        )
        if exception_applies:
            return "CULPABLE HOMICIDE NOT AMOUNTING TO MURDER (Exception 1: Grave & Sudden Provocation ကြောင့် လျော့ပေါ့)"
        
        # အဆင့်အားလုံးကျော်ဖြတ်ပြီးပါက - လူသတ်မှု မြောက်သည်
        return "MURDER (Section 300 အရ လူသတ်မှု မြောက်သည်)"


အဆင့်-၄ (Code အပြည့်အစုံ ဖြင့် စမ်းသပ်ခြင်း)

# Penal Code Section 300 Logic AI
# Based on AIMA Knowledge Representation Concepts
class LegalAI_Agent:
    def __init__(self):
        print("--- AI Legal Agent Initialized ---")
    def analyze_scenario(self, user_input_text):
        print(f"\nAnalyzing Scenario: '{user_input_text}'")
        
        # 1. NLU Simulation (Text -> Facts)
        case_facts = self.extract_facts(user_input_text)
        
        # 2. Inference Engine (Facts -> Verdict)
        verdict = self.apply_section_300_logic(case_facts)
        
        print(f"\n>>> VERDICT (အဆုံးအဖြတ်): {verdict}")
        print("--------------------------------------------------")
    def extract_facts(self, text):
        text = text.lower()
        facts = {
            "death": False,
            "intent_kill": False,
            "intent_injury": False,
            "sufficient_injury": False,
            "provoked": False,
            "sudden_grave": False,
            "loss_control": False
        }
        # Simple Keyword Extraction Rule
        if any(w in text for w in ["killed", "died", "dead", "fatal"]):
            facts["death"] = True
        
        if any(w in text for w in ["intentionally", "purpose", "plan"]):
            facts["intent_kill"] = True
            
        if any(w in text for w in ["stabbed", "shot", "hit", "cut"]):
            facts["intent_injury"] = True
            # Assuming strictly for this demo that weapons imply sufficient injury
            facts["sufficient_injury"] = True 
        if "provoked" in text:
            facts["provoked"] = True
        
        if "grave" in text and "sudden" in text:
            facts["sudden_grave"] = True
            
        if "lost control" in text or "deprived of self-control" in text:
            facts["loss_control"] = True
        return facts
    def apply_section_300_logic(self, f):
        # Logic Rule implementation
        
        # Rule 1: Must involve death
        if not f["death"]:
            return "Case Dismissed: No death occurred."
        # Rule 2: Establishing Prima Facie Murder (Mens Rea)
        # (Intent to kill) OR (Intent to injure + Sufficient injury)
        mens_rea = f["intent_kill"] or (f["intent_injury"] and f["sufficient_injury"])
        
        if not mens_rea:
            return "Not Murder: Lacks necessary intention."
        # Rule 3: Checking Exceptions (Provocation)
        # Must have Provocation AND (Sudden & Grave) AND (Loss of Control)
        exception_provocation = f["provoked"] and f["sudden_grave"] and f["loss_control"]
        if exception_provocation:
            return "Reduced Charge: Culpable Homicide (Due to Provocation Exception)."
        # Default if fits definition and no exceptions
        return "GUILTY: Murder under Section 300."
# --- လက်တွေ့ စမ်းသပ်ခန်း ---
agent = LegalAI_Agent()
# Scenario 1: ရိုးရှင်းသော လူသတ်မှု
scenario_1 = "John intentionally stabbed Bob and Bob died."
agent.analyze_scenario(scenario_1)
# Scenario 2: ခြွင်းချက်နှင့် ငြိစွန်းသော အမှု (Provocation)
scenario_2 = "Bob provoked John with grave and sudden insults. John lost control and stabbed Bob, and Bob died."
agent.analyze_scenario(scenario_2)
# Scenario 3: သေဆုံးမှု မရှိသော အမှု
scenario_3 = "John punched Bob. Bob was hurt but survived."
agent.analyze_scenario(scenario_3)


ကုဒ်၏ လုပ်ဆောင်ပုံ အနှစ်ချုပ်

  1. Object-Oriented Design: Agent ကို Class တစ်ခုအနေနဲ့ တည်ဆောက်ထားတယ်။

  2. Symbolic Logic: apply_section_300_logic function ထဲတွင် if... or... and... စတဲ့ Boolean Logic တွေကို အသုံးပြုထားခြင်းဟာ AIMA စာအုပ်ပါ Symbolic AI နည်းလမ်းဖြစ်တယ်။

  3. Explainability: ဒီကုဒ်က Black Box မဟုတ်ဘူး။ Reduced Charge လို့ အဖြေထွက်လာလျှင် ဘာကြောင့် လျော့ပေါ့ပေးသလဲ (Exception Provocation ကြောင့်) ဆိုတာကို Logic အရ ရှင်းပြနိုင်စွမ်းရှိတယ်။

နောက်တစ်ဆင့်အနေနဲ့ ဒီ AI ကို "Self-Defense" (ကာကွယ်ခုခံပိုင်ခွင့်) ဆိုတဲ့ Logic ထပ်ဖြည့်ရေးပြီး တင်ပြပါအုံးမယ်။

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.