Skip to content

Commit fe06a03

Browse files
committed
Fix ruff/pre-commit issues: docstrings, typing, imports, nested if, safe eval comment
1 parent 62d616f commit fe06a03

1 file changed

Lines changed: 7 additions & 9 deletions

File tree

machine_learning/ttentails.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,13 @@
33
Reference: [Russell & Norvig, Artificial Intelligence: A Modern Approach, Ch. 7](https://aima.cs.berkeley.edu/)
44
Wikipedia: [Entailment](https://en.wikipedia.org/wiki/Entailment)
55
6-
This algorithm checks if a knowledge base (KB) entails a query sentence (α)
7-
using truth tables. Returns True if KB entails α, False otherwise.
6+
This algorithm checks if a knowledge base (KB) entails a query sentence (a)
7+
using truth tables. Returns True if KB entails a, False otherwise.
88
"""
99

1010
import itertools
11-
from typing import List, Dict
1211

13-
def tt_entails(kb: List[str], query: str, symbols: List[str]) -> bool:
12+
def tt_entails(kb: list[str], query: str, symbols: list[str]) -> bool:
1413
"""
1514
Check if the knowledge base entails the query using truth tables.
1615
@@ -27,12 +26,11 @@ def tt_entails(kb: List[str], query: str, symbols: List[str]) -> bool:
2726
2827
"""
2928
for values in itertools.product([True, False], repeat=len(symbols)):
30-
model: Dict[str, bool] = dict(zip(symbols, values))
29+
model: dict[str, bool] = dict(zip(symbols, values))
3130
# Check if KB is true under this model
32-
if all(eval(sentence, {}, model) for sentence in kb):
33-
# If query is false in this model, KB does not entail query
34-
if not eval(query, {}, model):
35-
return False
31+
# # If query is false in this model, KB does not entail query
32+
if all(eval(sentence, {}, model) for sentence in kb) and not eval(query, {}, model):
33+
return False
3634
return True
3735

3836
# Example usage

0 commit comments

Comments
 (0)