-
Notifications
You must be signed in to change notification settings - Fork 0
/
emda_parser.py
184 lines (125 loc) · 4.09 KB
/
emda_parser.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
#!/usr/bin/env python
# coding: utf-8
# In[108]:
from IPython.display import JSON
import json
from pyvis.network import Network
import pathlib
import datetime
# In[103]:
class Block:
def __init__(self, text):
self.text = text
self.words = self.text.split()
self.children = []
def add(self, x):
if type(x) in [list, tuple]:
self.children.extend(x)
else:
self.children.append(x)
return x
def traverse(self, callback):
return [callback(c) for c in self.children]
# def collect(self):
# collected = Block('')
# collected.add(self.traverse(lambda a: a))
# collected = []
# for c in self.children:
# print(c.collect())
# collected.extend([[d.text, c] for a, d in c.collect()])
# return collected
def markdown(self, level=0):
prefixes = ['# ', '## ', '', '- ', '', '', '']
return self.text + '\n' + '\n'.join(prefixes[level]+c.markdown(level+1) for c in self.children)
# def indent(self, n):
# return ['\t'*n+c for c in self.children]
def tostring(self, l=0):
return self.text + ''.join(['\n'+('\t'*l)+c.tostring(l+1) for c in self.children])
def find(self, name):
return list(filter(lambda x: x.text == name, self.children))
def relations(self):
rlist = []
for c in self.children:
# print(c.relations())
rlist.append([self, c])
# rlist.extend([[c, t] for t, r in c.relations()])
rlist.extend(c.relations())
return rlist
def visualize(self, path='./graph-visualization.html'):
self.vis = Network(notebook=True, width=800, height=800)
rel_text = [[' '.join(a.text.split()[:3]) for a in b] for b in self.relations()]
# print(rel_text)
for x, y in rel_text:
for w in [x, y]:
self.vis.add_node(w)
self.vis.add_edge(x, y)
return self.vis.show(path)
def __str__(self):
return self.tostring()
def __getattr__(self, name):
nodes = self.find(name)
if nodes:
return nodes[0]
else:
return self.add(Block(name))
def getlinks(node):
links = []
for w in node.words:
if '$' in w:
links.append(w.split('$'))
for c in node.children:
links.extend(getlinks(c))
return links
# In[140]:
# path = 'entities.edma'
# path = '../todo_july.txt'
path = './readme_source.txt'
class Doc:
def __init__(self, source):
self.source = source
with open(self.source, 'r') as source_file:
self.content = source_file.read()
self.lines = self.content.splitlines()
self.root = Block('')
self.levels = {0: self.root}
for line in self.lines:
content = line.lstrip()
if content:
tabs = len(line) - len(content)
lblock = Block(content)
self.levels[tabs+1] = lblock
self.levels[tabs].add(lblock)
def save(self, file_path):
with open(file_path, 'w') as savefile:
savefile.write(str(self.root))
def backup(self):
backup_dir = 'eparse_backups'
pathlib.Path(f'./{backup_dir}').mkdir(parents=True, exist_ok=True)
timestamp = datetime.datetime.now().strftime('%d-%m-%Y_%H-%M-%S')
backup_path = f'./{backup_dir}/backup_{timestamp}.emda'
self.save(backup_path)
return backup_path
maindoc = Doc(path)
# print(levels)
# JSON(json.dumps(root, default=vars))
urls = []
for l in getlinks(maindoc.root):
if l[1] == 'wiki':
b = f'https://en.wikipedia.org/wiki/{l[0].replace(" ", "_")}'
else:
b = l[1]
urls.append(Block(b))
maindoc.root.Metadata.Links.add(urls)
# maindoc.backup()
# print(root.Metadata.Links)
# print(root)
# In[137]:
# In[105]:
maindoc.root.visualize()
# In[62]:
getlinks(root)
# In[142]:
print(maindoc.root.markdown())
# In[48]:
_
# In[ ]: