def html(text, delete_x_ids=True): amr = AMR(text) elems = [e for e in amr.text_elements] nodes = [id for id in amr.node_ids()] edges = [id for id in amr.edge_ids()] node_indices = [i for i,e in enumerate(amr.text_elements) if amr.NODE_RE.match(e)] edge_indices = [i for i,e in enumerate(amr.text_elements) if amr.EDGE_RE.match(e)] Named_Entity_RE = re.compile('x[0-9]+/".*?"') for i,e in enumerate(elems): if i in node_indices: id = nodes.pop(0) frame = e.split('/')[-1] if '/' in e else '_' node = e if delete_x_ids: node = re.sub('^x[0-9]+/', '', e, 1) if frame in propbank_frames_dictionary: description = propbank_frames_dictionary[frame].replace('\t','\n') elems[i] = f'<span class="amr-frame" tok-id="{id}" title="{description}">{node}</span>' elif Named_Entity_RE.match(e): elems[i] = f'<span class="amr-entity" tok-id="{id}">{node}</span>' else: elems[i] = f'<span class="amr-node" tok-id="{id}">{node}</span>' elif i in edge_indices: id = edges.pop(0) elems[i] = f'<span class="amr-edge" tok-id="{id}">{e}</span>' text = ''.join(elems) return '\n<div class="amr-container">\n<pre>\n'+text+'\n</pre>\n</div>\n'
def latex(text): amr = AMR(text) text = str(amr) for x in re.findall('x[0-9]+ ?/ ?[^()\s]+', text): text = text.replace(x, '(' + x + ')') edges = [(e, id) for e, id in zip(amr.edges(), amr.edge_ids())] elems = [] max_depth = paren_utils.max_depth(text) prev_depth = 0 depth = 0 i = 0 node_depth = {} for t in paren_utils.paren_iter(text): node = amr.NODE_RE.match(t).group() id = node.split('/')[0].strip() # clean node if re.match('x[0-9]+/', node): node = node.split('/')[1] node = node.replace('"', '``', 1).replace('"', "''", 1) prev_depth = depth depth = paren_utils.depth_at(text, text.index(t)) if depth > prev_depth: i = 0 node_depth[id] = depth num_nodes = paren_utils.mark_depth(text).count(f'<{depth}>') x = AMR_Latex.get_x(i, num_nodes) y = AMR_Latex.get_y(depth, max_depth) color = AMR_Latex.get_color(i) elems.append(f'\t\\node[{color}]({id}) at ({x},{y}) {{{node}}};') i += 1 for edge, id in edges: source = id.split('_')[0] target = id.split('_')[2] dir1 = 'south' dir2 = 'north' if node_depth[source] > node_depth[target]: dir1 = 'north' dir2 = 'south' if node_depth[source] == node_depth[target]: dir1 = 'north' dir2 = 'north' elems.append( f'\t\draw[->, thick] ({source}.{dir1}) -- ({target}.{dir2}) node[midway, above, sloped] {{{edge}}};' ) latex = '\n\\begin{tikzpicture}[\n' latex += 'red/.style={rectangle, draw=red!60, fill=red!5, very thick, minimum size=7mm},\n' latex += 'blue/.style={rectangle, draw=blue!60, fill=blue!5, very thick, minimum size=7mm},\n' latex += 'green/.style={rectangle, draw=green!60, fill=green!5, very thick, minimum size=7mm},\n' latex += 'purple/.style={rectangle, draw=purple!60, fill=purple!5, very thick, minimum size=7mm},\n' latex += 'orange/.style={rectangle, draw=orange!60, fill=orange!5, very thick, minimum size=7mm},\n' latex += ']\n' latex += '\n'.join(elems) latex += '\n\end{tikzpicture}\n' return latex