Returns a dot-formatted string that can be visualized as a graph in GraphViz.
(sentence, font="Arial", colors=BLUE)
| 1525 | return s |
| 1526 | |
| 1527 | def graphviz_dot(sentence, font="Arial", colors=BLUE): |
| 1528 | """ Returns a dot-formatted string that can be visualized as a graph in GraphViz. |
| 1529 | """ |
| 1530 | s = 'digraph sentence {\n' |
| 1531 | s += '\tranksep=0.75;\n' |
| 1532 | s += '\tnodesep=0.15;\n' |
| 1533 | s += '\tnode [penwidth=1, fontname="%s", shape=record, margin=0.1, height=0.35];\n' % font |
| 1534 | s += '\tedge [penwidth=1];\n' |
| 1535 | s += '\t{ rank=same;\n' |
| 1536 | # Create node groups for words, chunks and PNP chunks. |
| 1537 | for w in sentence.words: |
| 1538 | s += '\t\tword%s [label="<f0>%s|<f1>%s"%s];\n' % (w.index, w.string, w.type, _colorize(w, colors)) |
| 1539 | for w in sentence.words[:-1]: |
| 1540 | # Invisible edges forces the words into the right order: |
| 1541 | s += '\t\tword%s -> word%s [color=none];\n' % (w.index, w.index+1) |
| 1542 | s += '\t}\n' |
| 1543 | s += '\t{ rank=same;\n' |
| 1544 | for i, ch in enumerate(sentence.chunks): |
| 1545 | s += '\t\tchunk%s [label="<f0>%s"%s];\n' % (i+1, "-".join([x for x in ( |
| 1546 | ch.type, ch.role, str(ch.relation or '')) if x]) or '-', _colorize(ch, colors)) |
| 1547 | for i, ch in enumerate(sentence.chunks[:-1]): |
| 1548 | # Invisible edges forces the chunks into the right order: |
| 1549 | s += '\t\tchunk%s -> chunk%s [color=none];\n' % (i+1, i+2) |
| 1550 | s += '}\n' |
| 1551 | s += '\t{ rank=same;\n' |
| 1552 | for i, ch in enumerate(sentence.pnp): |
| 1553 | s += '\t\tpnp%s [label="<f0>PNP"%s];\n' % (i+1, _colorize(ch, colors)) |
| 1554 | s += '\t}\n' |
| 1555 | s += '\t{ rank=same;\n S [shape=circle, margin=0.25, penwidth=2]; }\n' |
| 1556 | # Connect words to chunks. |
| 1557 | # Connect chunks to PNP or S. |
| 1558 | for i, ch in enumerate(sentence.chunks): |
| 1559 | for w in ch: |
| 1560 | s += '\tword%s -> chunk%s;\n' % (w.index, i+1) |
| 1561 | if ch.pnp: |
| 1562 | s += '\tchunk%s -> pnp%s;\n' % (i+1, sentence.pnp.index(ch.pnp)+1) |
| 1563 | else: |
| 1564 | s += '\tchunk%s -> S;\n' % (i+1) |
| 1565 | if ch.type == 'VP': |
| 1566 | # Indicate related chunks with a dotted |
| 1567 | for r in ch.related: |
| 1568 | s += '\tchunk%s -> chunk%s [style=dotted, arrowhead=none];\n' % ( |
| 1569 | i+1, sentence.chunks.index(r)+1) |
| 1570 | # Connect PNP to anchor chunk or S. |
| 1571 | for i, ch in enumerate(sentence.pnp): |
| 1572 | if ch.anchor: |
| 1573 | s += '\tpnp%s -> chunk%s;\n' % (i+1, sentence.chunks.index(ch.anchor)+1) |
| 1574 | s += '\tpnp%s -> S [color=none];\n' % (i+1) |
| 1575 | else: |
| 1576 | s += '\tpnp%s -> S;\n' % (i+1) |
| 1577 | s += "}" |
| 1578 | return s |
| 1579 | |
| 1580 | ### STDOUT TABLE ################################################################################### |
| 1581 |