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

Python
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)

ဘာတွေ ပြင်လိုက်သလဲ?

  1. Keyword ထပ်ဖြည့်ခြင်း: death_keywords စာရင်းထဲမှာ "death" ကို ထည့်လိုက်ပါတယ်။ ဒါကြောင့် "stabbed to death" ဆိုတဲ့ စာသားကို AI က ဖတ်မိပြီး death_occurred = True လို့ မှန်ကန်စွာ သတ်မှတ်ပေးနိုင်ပါပြီ။

  2. re (Regular Expression) အသုံးပြုခြင်း: ယခင် in command အစား 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 ရှိပြီးသားဖြစ်ရပါမယ်။)

Python
# 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

Python
# 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 လို့ မမြင်တော့ပါ)။

Plaintext
==================================================
       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 ဟာ...

  1. Soldier/Diet ထဲမှာ 'die' ပါပေမယ့် လူမသေဘူး (Death Detected -> False) လို့ သိပါတယ်။

  2. Skilled ထဲမှာ 'kill' ပါပေမယ့် လူသတ်ဖို့ရည်ရွယ်ချက် (Intent) မရှိဘူး၊ လူမသေဘူးလို့ သိပါတယ်။

  3. Stable ထဲမှာ 'stab' ပါပေမယ့် ဓားနဲ့မထိုးဘူးလို့ သိပါတယ်။

ဒါဟာ Regular Expression ရဲ့ \b (Word Boundary) စွမ်းဆောင်ရည်ကြောင့် ဖြစ်ပါတယ်။

No comments:

Post a Comment

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