-
Notifications
You must be signed in to change notification settings - Fork 5
/
NALSyntax.py
285 lines (233 loc) · 8.64 KB
/
NALSyntax.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
import enum
"""
Author: Christian Hahm
Created: October 9, 2020
Purpose: Defines the syntax to be used for Narsese
"""
class StatementSyntax(enum.Enum):
Start = "("
End = ")"
TruthValMarker = "%"
ExpectationMarker = "#"
ValueSeparator = ";"
TermDivider = ","
BudgetMarker = "$"
ArrayElementIndexStart = "["
ArrayElementIndexEnd = "]"
class Tense(enum.Enum):
Future = ":/:"
Past = ":\:"
Present = ":|:"
Eternal = None
@classmethod
def get_tense_from_string(cls, value):
for tense in cls:
if value == tense.value:
return tense
return None
class TermConnector(enum.Enum):
# NAL-2
ExtensionalSetStart = "{"
ExtensionalSetEnd = "}"
IntensionalSetStart = "["
IntensionalSetEnd = "]"
# NAL-3
ExtensionalIntersection = "&"
IntensionalIntersection = "|"
ExtensionalDifference = "-"
IntensionalDifference = "~"
# NAL-4
Product = "*"
ExtensionalImage = "/"
IntensionalImage = "\\"
ImagePlaceHolder = "_"
# NAL-5
Negation = "--"
Conjunction = "&&"
Disjunction = "||"
SequentialConjunction = "&/"
ParallelConjunction = "&|"
# Array
ArrayConjunction = "@&"
ArrayDisjunction = "@|"
@classmethod
def is_string_a_term_connector(cls, value):
return value in cls._value2member_map_
@classmethod
def get_term_connector_from_string(cls, value):
if not TermConnector.is_string_a_term_connector(value):
return None
for connector in cls:
if value == connector.value:
return connector
return None
@classmethod
def is_first_order(cls, connector):
"""
First order connectors are Term Connectors
Higher order connectors are Statement Connectors
"""
assert connector is not None,"ERROR: None is not a term connector"
return not (connector is cls.Negation or
connector is cls.Conjunction or
connector is cls.Disjunction or
connector is cls.SequentialConjunction or
connector is cls.ParallelConjunction or
connector is cls.ArrayConjunction)
@classmethod
def is_order_invariant(cls, connector):
return (connector is cls.ExtensionalIntersection or
connector is cls.IntensionalIntersection or
connector is cls.ExtensionalSetStart or
connector is cls.IntensionalSetStart or
connector is cls.Negation or
connector is cls.Conjunction or
connector is cls.Disjunction)
@classmethod
def is_conjunction(cls, connector):
#assert connector is not None, "ERROR: None is not a term connector"
return (connector is cls.Conjunction or
connector is cls.SequentialConjunction or
connector is cls.ParallelConjunction)
@classmethod
def contains_conjunction(cls,string):
return (cls.Conjunction.value in string or
cls.SequentialConjunction.value in string or
cls.ParallelConjunction.value in string)
@classmethod
def contains_higher_level_connector(cls,string):
for connector in cls:
if not cls.is_first_order(connector):
# higher order connector
if connector.value in string: return True
return False
@classmethod
def get_set_end_connector_from_set_start_connector(cls, start_connector):
if start_connector == TermConnector.ExtensionalSetStart: return TermConnector.ExtensionalSetEnd
if start_connector == TermConnector.IntensionalSetStart: return TermConnector.IntensionalSetEnd
assert False,"ERROR: Invalid start connector"
@classmethod
def is_set_bracket_start(cls, bracket):
"""
Returns true if character is a starting bracket for a set
:param bracket:
:return:
"""
assert bracket is not None, "ERROR: None is not a term connector"
return (bracket == TermConnector.IntensionalSetStart.value) or (
bracket == TermConnector.ExtensionalSetStart.value)
@classmethod
def is_set_bracket_end(cls, bracket):
"""
Returns true if character is an ending bracket for a set
:param bracket:
:return:
"""
assert bracket is not None, "ERROR: None is not a term connector"
return (bracket == TermConnector.IntensionalSetEnd.value) or (
bracket == TermConnector.ExtensionalSetEnd.value)
class Copula(enum.Enum):
# Primary copula
Inheritance = "-->"
Similarity = "<->"
Implication = "==>"
Equivalence = "<=>"
# Secondary copula
Instance = "{--"
Property = "--]"
InstanceProperty = "{-]"
PredictiveImplication = "=/>"
RetrospectiveImplication = r"=\>"
ConcurrentImplication = "=|>"
PredictiveEquivalence = "</>"
ConcurrentEquivalence = "<|>"
@classmethod
def is_implication(cls, copula):
return copula is cls.Implication \
or copula is cls.PredictiveImplication \
or copula is cls.RetrospectiveImplication \
@classmethod
def is_first_order(cls, copula):
return copula is cls.Inheritance \
or copula is cls.Similarity \
or copula is cls.Instance \
or copula is cls.Property \
or copula is cls.InstanceProperty
@classmethod
def is_temporal(cls, copula):
return copula == cls.PredictiveImplication \
or copula == cls.RetrospectiveImplication \
or copula == cls.ConcurrentImplication \
or copula == cls.PredictiveEquivalence \
or copula == cls.ConcurrentEquivalence
@classmethod
def is_symmetric(cls, copula):
return copula == cls.Similarity \
or copula == cls.Equivalence \
or copula == cls.PredictiveEquivalence \
or copula == cls.ConcurrentEquivalence
@classmethod
def is_string_a_copula(cls, value):
return value in cls._value2member_map_
@classmethod
def get_copula_from_string(cls, value):
if not Copula.is_string_a_copula(value):
return None
for copula in cls:
if value == copula.value:
return copula
return None
@classmethod
def contains_copula(cls, string):
for copula in cls:
if copula.value in string:
return True
return False
@classmethod
def contains_top_level_copula(cls,string):
copula, _ = cls.get_top_level_copula(string)
return copula is not None
@classmethod
def get_top_level_copula(cls,string):
"""
Searches for top-level copula in the string.
:returns copula and index if it exists,
:returns none and -1 otherwise
"""
copula = None
copula_idx = -1
depth = 0
for i, v in enumerate(string):
if v == StatementSyntax.Start.value:
depth += 1
elif v == StatementSyntax.End.value:
depth -= 1
elif depth == 1 and i + 3 <= len(string) and Copula.is_string_a_copula(string[i:i + 3]):
copula, copula_idx = Copula.get_copula_from_string(string[i:i + 3]), i
return copula, copula_idx
class Punctuation(enum.Enum):
Judgment = "."
Question = "?" # on truth-value
Goal = "!"
Quest = "`" # on desire-value #todo, decide value for Quest since @ is used for array now
@classmethod
def is_punctuation(cls, value):
return value in cls._value2member_map_
@classmethod
def get_punctuation_from_string(cls, value):
if not Punctuation.is_punctuation(value):
return None
for punctuation in cls:
if value == punctuation.value:
return punctuation
return None
"""
List of valid characters that can be used in a term.
"""
valid_term_chars = {
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
"0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "_", "^"
}