-
Notifications
You must be signed in to change notification settings - Fork 0
/
sentence.py
148 lines (121 loc) · 3.12 KB
/
sentence.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
"""
sentence.py
Defines sentence
"""
import numpy as np
import copy
import sys
from .literal import *
from .exp_tree import *
class AtomicSentence:
"""An atomic sentence consists of
- its symbol
- whether it is a positive literal
- its true table
For example: p is a positive literal -> positive == true
¬p is a negative literal -> positive == false
"""
def __init__(self, positive = None, symbol = None):
self.l = Literal(positive, symbol)
if self.l.equalSymbol("True") or self.l.equalSymbol("False"):
self.t = np.array(True)
else:
self.t = np.array([True, False])
def table(self):
return self.t
def set(self, val):
cp = copy.deepcopy(self)
cp.t = np.array(val)
return cp
def setSymbol(self, symbol):
self.l.setSymbol(symbol)
# logical connectives
def cAnd (self, other):
cpSelf = Sentence(self)
cpOther = Sentence(copy.deepcopy(other))
cpSelf.cAnd(cpOther)
return cpSelf
def cOr (self, other):
cpSelf = Sentence(self)
cpOther = Sentence(copy.deepcopy(other))
cpSelf.cOr(cpOther)
return cpSelf
def cImp(self, other):
cpSelf = Sentence(self)
cpOther = Sentence(copy.deepcopy(other))
cpSelf.cImp(cpOther)
return cpSelf
def cIff(self, other):
cpSelf = Sentence(self)
cpOther = Sentence(copy.deepcopy(other))
cpSelf.cIff(cpOther)
return cpSelf
def cnf(self):
cpSelf = Sentence(self)
return cpSelf.cnf()
# fake method to test if an object is of AtomicSentence
def atom(self):
return
# magic
def __str__(self):
# neg = bytes.decode(172)
return str(self.l)
def __invert__(self):
cp = Sentence(self)
cp = ~cp
return cp
from pyPL.connective import *
class Sentence:
def __init__(self, obj):
try:
self.l = obj.l
if isinstance(obj, Sentence):
self.pns = obj.pns # a queue that represents postfix notation string of of the complex sentence
else:
self.pns = []
self.pns.append(self.l)
except AttributeError as e:
print("'obj' is not an instance of Sentence")
# logical connectives
def cAnd (self, other):
cpOther = other
if not isinstance(other, Sentence):
cpOther = Sentence(copy.deepcopy(other))
self.pns.extend(cpOther.pns)
self.pns.append(Conn.AND)
return self
def cOr (self, other):
cpOther = other
if not isinstance(other, Sentence):
cpOther = Sentence(copy.deepcopy(other))
self.pns.extend(cpOther.pns)
self.pns.append(Conn.OR)
return self
def cImp(self, other):
cpOther = other
if not isinstance(other, Sentence):
cpOther = Sentence(copy.deepcopy(other))
self.pns.extend(cpOther.pns)
self.pns.append(Conn.IMP)
return self
def cIff(self, other):
cpOther = other
if not isinstance(other, Sentence):
cpOther = Sentence(copy.deepcopy(other))
self.pns.extend(cpOther.pns)
self.pns.append(Conn.IFF)
return self
# magic - some logical connectives
def __str__(self):
# include the literal ?
# but needs to manually enter the literal
# that doesnt conflict with the current literals
s = [str(e) for e in self.pns]
return str(s)
# operator NOT
def __invert__(self):
self.pns.append(Conn.NOT)
return self
def cnf(self):
et = ExpTree(self.pns)
return et.cnf()