-
Notifications
You must be signed in to change notification settings - Fork 0
/
srcml2dot.py
executable file
·95 lines (73 loc) · 2.42 KB
/
srcml2dot.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
#!/usr/bin/python3
from bs4 import BeautifulSoup
from html.parser import HTMLParser
import multiprocessing
import pickle
import networkx as nx
import re
import statistics
import numpy as np
import sys
from networkx.drawing.nx_agraph import write_dot
if len(sys.argv) < 3:
print('USAGE: srcml2dot.py INPUT.srcml OUTPUT.dot')
quit()
infile = sys.argv[1]
outfile = sys.argv[2]
def re_0002(i):
# split camel case and remove special characters
tmp = i.group(0)
if len(tmp) > 1:
if tmp.startswith(' '):
return tmp
else:
return '{} {}'.format(tmp[0], tmp[1])
else:
return ' '.format(tmp)
re_0001_ = re.compile(r'([^a-zA-Z0-9 ])|([a-z0-9_][A-Z])')
class MyHTMLParser(HTMLParser):
def __init__(self):
super(MyHTMLParser, self).__init__()
self.parentstack = list()
self.curtag = -1
self.tagidx = -1
self.graph = nx.Graph()
self.seq = list()
def handle_starttag(self, tag, attrs):
self.parentstack.append(self.curtag)
self.tagidx += 1
self.seq.append(tag)
self.graph.add_node(self.tagidx, label=tag)
if self.parentstack[-1] >= 0:
self.graph.add_edge(self.parentstack[-1], self.tagidx)
self.curtag = self.tagidx
def handle_endtag(self, tag):
self.curtag = self.parentstack.pop()
def handle_data(self, data):
# first, do dats text preprocessing
data = re_0001_.sub(re_0002, data).lower().rstrip()
# second, create a node if there is text
if(data != ''):
for d in data.split(' '): # each word gets its own node
if d != '':
self.parentstack.append(self.curtag)
self.tagidx += 1
self.seq.append(d)
self.graph.add_node(self.tagidx, label=d, fontname='times-bold')
self.graph.add_edge(self.parentstack[-1], self.tagidx)
self.curtag = self.tagidx
self.curtag = self.parentstack.pop()
def get_graph(self):
return(self.graph)
def get_seq(self):
return(self.seq)
c = 0
def xmldecode(unit):
parser = MyHTMLParser()
parser.feed(unit)
return(parser.get_graph(), parser.get_seq())
with open(infile, 'r') as f:
unit = f.read()
(graph, seq) = xmldecode(unit)
write_dot(graph, outfile)
print(seq)