kasaysayan-check / reasoning_engine.py
FuzzyD1no's picture
Upload 15 files
85a2c74 verified
Raw History Blame Contribute Delete
12.2 kB
import collections
import collections.abc
# Python 3.10 patch for experta
collections.Mapping = collections.abc.Mapping
from experta import *
# ==========================================
# 1. DEFINE FACT SCHEMAS
# ==========================================
class MLClaim(Fact):
"""Data coming from the multi-layered AI pipeline."""
pass
class KBRecord(Fact):
"""Verified data pulled from your Knowledge Base/Database."""
pass
# ==========================================
# 2. THE REASONING ENGINE
# ==========================================
class KasaysayanEngine(KnowledgeEngine):
# ----------------------------------------------------
# RULE 1: The Nostalgia Filter (Semantic Override)
# ----------------------------------------------------
@Rule(
# CONDITION 1: Claim matches the nostalgia myth meaning (Ignores ML tone score!)
MLClaim(
text=MATCH.text,
matched_concepts=MATCH.concepts & P(lambda x: "nostalgia_myth" in x)
),
# CONDITION 2: The KB contains records of debt or violations for that era
KBRecord(
topic="historical_era",
era="martial_law",
has_massive_debt=True,
has_human_rights_violations=True
)
)
def apply_nostalgia_filter(self, text):
print("\n[TRIGGERED: NOSTALGIA FILTER (SEMANTIC MATCH)] ")
print(f"Analyzing Claim: '{text}'")
print("VERDICT: FALSE / MISLEADING")
print("Reasoning: This claim perfectly aligns with the 'Golden Age' semantic narrative. The Knowledge Base verifies severe economic debt and documented violations during this period.")
# ----------------------------------------------------
# RULE 2: Election Integrity Defense (Entity/Semantic Override)
# ----------------------------------------------------
@Rule(
# CONDITION 1: Claim specifically targets the COMELEC or matches election fraud semantics
MLClaim(
text=MATCH.text,
target_orgs=MATCH.orgs & P(lambda x: "COMELEC" in x)
),
# CONDITION 2: KB confirms there is no proven systemic fraud
KBRecord(
topic="election_integrity",
org="COMELEC",
has_proven_fraud=False
)
)
def apply_election_defense(self, text):
print("\n [TRIGGERED: INSTITUTIONAL ATTACK DETECTED] ")
print(f"Analyzing Claim: '{text}'")
print("VERDICT: UNVERIFIED / POTENTIAL DISINFORMATION")
print("Reasoning: Claim makes allegations against the COMELEC. The Knowledge Base currently shows no verified systemic fraud that supports this claim.")
# ----------------------------------------------------
# RULE 3: Human Rights Denial Defense (Semantic Override)
# ----------------------------------------------------
@Rule(
# CONDITION 1: Claim matches the denial of human rights/torture
MLClaim(
text=MATCH.text,
matched_concepts=MATCH.concepts & P(lambda x: "human_rights_denial" in x)
),
# CONDITION 2: KB confirms human rights violations occurred
KBRecord(
topic="historical_era",
era="martial_law",
has_human_rights_violations=True # Cross-referencing our mock database!
)
)
def apply_human_rights_defense(self, text):
print("\n [TRIGGERED: ATROCITY DENIAL DETECTED] ")
print(f"Analyzing Claim: '{text}'")
print("VERDICT: FALSE / HISTORICAL REVISIONISM")
print("Reasoning: This claim denies documented atrocities. The Knowledge Base independently verifies that severe human rights violations occurred during this period.")
# ----------------------------------------------------
# RULE 4: Economy Myth Defense
# ----------------------------------------------------
@Rule(
# CONDITION 1: Claim matches economy myth meaning
MLClaim(
text=MATCH.text,
matched_concepts=MATCH.concepts & P(lambda x: "economy_myth" in x)
),
# CONDITION 2: KB confirms debt existed
KBRecord(
topic="historical_era",
era="martial_law",
has_massive_debt=True
)
)
def apply_economy_defense(self, text):
print("\n [TRIGGERED: ECONOMIC REVISIONISM DETECTED] ")
print(f"Analyzing Claim: '{text}'")
print("VERDICT: FALSE / MISLEADING")
print("Reasoning: The Knowledge Base confirms massive debt existed during this period.")
# ----------------------------------------------------
# RULE 5: Crime Myth Defense
# ----------------------------------------------------
@Rule(
MLClaim(
text=MATCH.text,
matched_concepts=MATCH.concepts & P(lambda x: "crime_myth" in x)
),
KBRecord(
topic="historical_era",
era="martial_law",
has_arbitrary_arrests=True
)
)
def apply_crime_myth_defense(self, text):
print("\n [TRIGGERED: PEACE & ORDER MYTH DETECTED] ")
print(f"Analyzing Claim: '{text}'")
print("VERDICT: FALSE / MISLEADING")
print("Reasoning: The Knowledge Base confirms thousands were arbitrarily arrested without trial during this period.")
# ----------------------------------------------------
# RULE 6: Marcos Best President Defense
# ----------------------------------------------------
@Rule(
MLClaim(
text=MATCH.text,
matched_concepts=MATCH.concepts & P(lambda x: "marcos_best_president" in x)
),
KBRecord(
topic="historical_era",
era="martial_law",
has_ill_gotten_wealth=True
)
)
def apply_marcos_best_defense(self, text):
print("\n [TRIGGERED: LEADERSHIP GLORIFICATION DETECTED] ")
print(f"Analyzing Claim: '{text}'")
print("VERDICT: DISPUTED / MISLEADING")
print("Reasoning: The Knowledge Base confirms billions in ill-gotten wealth were stolen from the Filipino people.")
# ----------------------------------------------------
# RULE 7: Press Freedom Myth Defense
# ----------------------------------------------------
@Rule(
MLClaim(
text=MATCH.text,
matched_concepts=MATCH.concepts & P(lambda x: "press_freedom_myth" in x)
),
KBRecord(
topic="historical_era",
era="martial_law",
has_media_censorship=True
)
)
def apply_press_myth_defense(self, text):
print("\n [TRIGGERED: PRESS FREEDOM MYTH DETECTED] ")
print(f"Analyzing Claim: '{text}'")
print("VERDICT: FALSE")
print("Reasoning: The Knowledge Base confirms all major media outlets were shut down or controlled by the regime.")
# ----------------------------------------------------
# RULE 8: Infrastructure Myth Defense
# ----------------------------------------------------
@Rule(
MLClaim(
text=MATCH.text,
matched_concepts=MATCH.concepts & P(lambda x: "infrastructure_myth" in x)
),
KBRecord(
topic="historical_era",
era="martial_law",
has_overpriced_infrastructure=True
)
)
def apply_infrastructure_myth_defense(self, text):
print("\n [TRIGGERED: INFRASTRUCTURE MYTH DETECTED] ")
print(f"Analyzing Claim: '{text}'")
print("VERDICT: FALSE / MISLEADING")
print("Reasoning: The Knowledge Base confirms many projects were overpriced and funded through foreign debt that burdened future generations.")
# ----------------------------------------------------
# RULE 9: Anti-Vaccine Myth Defense
# ----------------------------------------------------
@Rule(
MLClaim(
text=MATCH.text,
matched_concepts=MATCH.concepts & P(lambda x: "antivax_myth" in x)
),
KBRecord(
topic="vaccine_safety",
has_scientific_consensus=True,
causes_autism=False
)
)
def apply_antivax_defense(self, text):
print("\n [TRIGGERED: ANTI-VACCINE MISINFORMATION DETECTED] ")
print(f"Analyzing Claim: '{text}'")
print("VERDICT: FALSE / DANGEROUS MISINFORMATION")
print("Reasoning: The Knowledge Base confirms vaccines are WHO-approved and the scientific consensus finds no link between vaccines and autism.")
# ----------------------------------------------------
# RULE 10: COVID Misinformation Defense
# ----------------------------------------------------
@Rule(
MLClaim(
text=MATCH.text,
matched_concepts=MATCH.concepts & P(lambda x: "covid_myth" in x)
),
KBRecord(
topic="covid_facts",
was_global_pandemic=True,
government_exaggerated=False
)
)
def apply_covid_defense(self, text):
print("\n [TRIGGERED: COVID MISINFORMATION DETECTED] ")
print(f"Analyzing Claim: '{text}'")
print("VERDICT: FALSE / DANGEROUS MISINFORMATION")
print("Reasoning: The Knowledge Base confirms COVID-19 was a WHO-declared global pandemic and vaccines contain no microchips.")
# ----------------------------------------------------
# RULE 11: West Philippine Sea Denial Defense
# ----------------------------------------------------
@Rule(
MLClaim(
text=MATCH.text,
matched_concepts=MATCH.concepts & P(lambda x: "wps_denial" in x)
),
KBRecord(
topic="west_philippine_sea",
philippines_won_arbitration=True,
china_claim_legally_valid=False
)
)
def apply_wps_defense(self, text):
print("\n [TRIGGERED: WEST PHILIPPINE SEA DENIAL DETECTED] ")
print(f"Analyzing Claim: '{text}'")
print("VERDICT: FALSE / LEGALLY BASELESS")
print("Reasoning: The Knowledge Base confirms the Philippines won the 2016 UNCLOS arbitration ruling. China's claim has no basis under international law.")
# ----------------------------------------------------
# RULE 12: Climate Change Denial Defense
# ----------------------------------------------------
@Rule(
MLClaim(
text=MATCH.text,
matched_concepts=MATCH.concepts & P(lambda x: "climate_denial" in x)
),
KBRecord(
topic="climate_science",
has_scientific_consensus=True,
is_human_caused=True,
is_fake=False
)
)
def apply_climate_defense(self, text):
print("\n [TRIGGERED: CLIMATE CHANGE DENIAL DETECTED] ")
print(f"Analyzing Claim: '{text}'")
print("VERDICT: FALSE / ANTI-SCIENCE")
print("Reasoning: The Knowledge Base confirms that 97% of climate scientists agree climate change is real and primarily caused by human activity.")
# ----------------------------------------------------
# RULE 13: Social Media Propaganda Defense
# ----------------------------------------------------
@Rule(
MLClaim(
text=MATCH.text,
matched_concepts=MATCH.concepts & P(lambda x: "social_media_propaganda" in x)
),
KBRecord(
topic="media_integrity",
social_media_unverified=True,
has_fact_checkers=True
)
)
def apply_social_media_defense(self, text):
print("\n [TRIGGERED: ANTI-MEDIA PROPAGANDA DETECTED] ")
print(f"Analyzing Claim: '{text}'")
print("VERDICT: MISLEADING / PROPAGANDA")
print("Reasoning: The Knowledge Base confirms mainstream media is regulated and fact-checked, while social media remains largely unverified and prone to disinformation.")