forked from MOZI-AI/knowledge-import
-
Notifications
You must be signed in to change notification settings - Fork 0
/
atomwrappers.py
91 lines (64 loc) · 2.22 KB
/
atomwrappers.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
"""
Classes to generate atomese without working with atomspace
"""
__author__ = "Anatoly Belikov"
__email__ = "[email protected]"
class CAtom:
def __hash__(self):
return hash(str(self))
class CNode(CAtom):
atom_type = None
def __init__(self, name):
self.name = name
def __str__(self):
return '({0} "{1}")'.format(self.atom_type, self.name.replace('"', '\\"'))
def recursive_print(self, result='', indent=''):
return result + indent + str(self)
class CLink(CAtom):
def __init__(self, *atoms, stv=None):
self.outgoing = atoms
for atom in atoms:
assert isinstance(atom, CAtom)
self.stv = stv
def __str__(self):
outgoing = '\n'.join([str(x) for x in self.outgoing])
if self.stv is not None:
return '({0} {1} {2})'.format(self.atom_type, self.stv , outgoing)
return '({0} {1})'.format(self.atom_type, outgoing)
def recursive_print(self, result='', indent=''):
result += indent + '({0}'.format(self.atom_type)
indent = indent + ' '
for x in self.outgoing:
result = x.recursive_print(result + '\n', indent)
result += ')'
return result
class CEvaluationLink(CLink):
atom_type = 'EvaluationLink'
class CPredicateNode(CNode):
atom_type = 'PredicateNode'
class CConceptNode(CNode):
atom_type = 'ConceptNode'
class CMoleculeNode(CNode):
atom_type = 'MoleculeNode'
class CMemberLink(CLink):
atom_type = 'MemberLink'
class CListLink(CLink):
atom_type = 'ListLink'
class CGeneNode(CNode):
atom_type = 'GeneNode'
class CContextLink(CLink):
atom_type = 'ContextLink'
class CInheritanceLink(CLink):
atom_type = 'InheritanceLink'
class CSetLink(CLink):
atom_type = 'SetLink'
def __str__(self):
# str is used for hash computation, so need to sort outgoing set for all unordered links
outgoing = '\n'.join(sorted([str(x) for x in self.outgoing]))
return '({0} {1})'.format(self.atom_type, outgoing)
class CStv:
def __init__(self, tv, confidence):
self.tv = tv
self.confidence = confidence
def __str__(self):
return '(stv {0} {1})'.format(self.tv, self.confidence)