-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathintermediate_derivation.py
More file actions
executable file
·65 lines (49 loc) · 1.97 KB
/
intermediate_derivation.py
File metadata and controls
executable file
·65 lines (49 loc) · 1.97 KB
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
import json
from tags import Markup
class IntermediateDeriv(object):
"""
This class is used in the probabilistic Monte Carlo Expansions to associate markup with
productions
markup contains all markup which led to a particular string being formed
expansion contains that string
these are then joined at the end
"""
def __init__(self, markup, expansion):
self.markup = markup
self.expansion = expansion
self.probability = 1
def __eq__(self, other):
if isinstance(other, IntermediateDeriv):
return self.__str__() == other.__str__()
else:
return False
def __str__(self):
return self.expansion.__str__() + " MARKUP" + self.markup.__str__()
def __add__(self, other):
# if adding two derivations together, add them
if isinstance(other, IntermediateDeriv):
return IntermediateDeriv(self.markup | other.markup, self.expansion + other.expansion)
# if adding markup to a derivation, only add markup, preserve expansion
else:
return IntermediateDeriv(self.markup | other, self.expansion)
def __lt__(self, other):
return self.expansion < other.expansion
def __radd__(self, other):
if other == 0:
return self
else:
return self.__add__(other)
def __repr__(self):
return self.__str__()
def __hash__(self):
return hash((self.expansion))
def to_json(self):
def set_default(obj):
if isinstance(obj, Markup):
return obj.__str__()
return json.dumps({"derivation": self.expansion.__str__(), "markup": list(self.markup)}, default=set_default)
def mcm_derive(self, samplesscalar, markup):
return self.expansion.mcm_derive(samplesscalar, markup)
def exhaustively_and_nonprobabilistically_derive(self, markup):
""""""
return self.expansion.exhaustively_and_nonprobabilistically_derive(markup=markup)