Skip to content

Commit 518f003

Browse files
committed
Rename file to lowercase without dash to follow repo conventions
1 parent 969b313 commit 518f003

1 file changed

Lines changed: 52 additions & 0 deletions

File tree

machine_learning/ttentails.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
"""
2+
TT-ENTAILS Algorithm (Propositional Logic)
3+
Reference: Russell & Norvig, Artificial Intelligence: A Modern Approach
4+
5+
This algorithm checks if a knowledge base (KB) entails a query sentence (α)
6+
using truth tables. Returns True if KB entails α, False otherwise.
7+
"""
8+
9+
import itertools
10+
from typing import List, Dict
11+
12+
def tt_entails(kb: List[str], query: str, symbols: List[str]) -> bool:
13+
"""
14+
Check if the knowledge base entails the query using truth tables.
15+
16+
Args:
17+
kb (List[str]): List of propositional sentences in KB as strings
18+
query (str): Query sentence to test entailment
19+
symbols (List[str]): List of all propositional symbols used
20+
21+
Returns:
22+
bool: True if KB entails query, False otherwise
23+
24+
Example:
25+
tt_entails(["P or Q"], "Q", ["P","Q"])
26+
27+
"""
28+
for values in itertools.product([True, False], repeat=len(symbols)):
29+
model: Dict[str, bool] = dict(zip(symbols, values))
30+
# Check if KB is true under this model
31+
if all(eval(sentence, {}, model) for sentence in kb):
32+
# If query is false in this model, KB does not entail query
33+
if not eval(query, {}, model):
34+
return False
35+
return True
36+
37+
# Example usage
38+
if __name__ == "__main__":
39+
# Example 1: KB entails query → should return True
40+
symbols = ["P", "Q"]
41+
kb = ["P or Q", "not P or Q"] # KB says P or Q is True, and not P or Q is True
42+
query = "Q" # Query: Is Q True?
43+
print("Does KB entail query? : ", tt_entails(kb, query, symbols))
44+
45+
# Example 2: KB does NOT entail query → should return False
46+
symbols2 = ["P", "Q"]
47+
kb2 = ["P"] # KB says only P is True
48+
query2 = "Q" # Query asks if Q is True
49+
print("Does KB2 entail query2? : ", tt_entails(kb2, query2, symbols2))
50+
51+
52+

0 commit comments

Comments
 (0)