|
20 | 20 | from itertools import chain |
21 | 21 | from pprint import pformat |
22 | 22 |
|
| 23 | +from nltk.internals import find_binary |
23 | 24 | from nltk.tree import Tree |
24 | 25 |
|
25 | 26 | ################################################################# |
@@ -185,7 +186,8 @@ def to_dot(self): |
185 | 186 |
|
186 | 187 | def _repr_svg_(self): |
187 | 188 | """Show SVG representation of the transducer (IPython magic). |
188 | | -
|
| 189 | + >>> from nltk.test.setup_fixt import check_binary |
| 190 | + >>> check_binary('dot') |
189 | 191 | >>> dg = DependencyGraph( |
190 | 192 | ... 'John N 2\\n' |
191 | 193 | ... 'loves V 0\\n' |
@@ -459,7 +461,7 @@ def contains_cycle(self): |
459 | 461 | >>> cyclic_dg.root = top |
460 | 462 |
|
461 | 463 | >>> cyclic_dg.contains_cycle() |
462 | | - [3, 1, 2, 4] |
| 464 | + [1, 2, 4, 3] |
463 | 465 |
|
464 | 466 | """ |
465 | 467 | distances = {} |
@@ -550,30 +552,36 @@ def dot2img(dot_string, t="svg"): |
550 | 552 | Create image representation fom dot_string, using the 'dot' program |
551 | 553 | from the Graphviz package. |
552 | 554 |
|
553 | | - Use the 't' argument to specify the image file format, for ex. |
554 | | - 'png' or 'jpeg' (Running 'dot -T:' lists all available formats). |
| 555 | + Use the 't' argument to specify the image file format, for ex. 'jpeg', 'eps', |
| 556 | + 'json', 'png' or 'webp' (Running 'dot -T:' lists all available formats). |
555 | 557 |
|
556 | | - sys.stdout is used instead of subprocess.PIPE, to avoid decoding errors |
| 558 | + Note that the "capture_output" option of subprocess.run() is only available |
| 559 | + with text formats (like svg), but not with binary image formats (like png). |
557 | 560 | """ |
558 | | - from sys import stderr, stdout |
559 | 561 |
|
560 | 562 | try: |
561 | | - proc = subprocess.run( |
562 | | - ["dot", "-T%s" % t], |
563 | | - input=dot_string, |
564 | | - stdout=stdout, |
565 | | - stderr=stderr, |
566 | | - text=True, |
567 | | - ) |
| 563 | + find_binary("dot") |
| 564 | + try: |
| 565 | + if t in ["dot", "dot_json", "json", "svg"]: |
| 566 | + proc = subprocess.run( |
| 567 | + ["dot", "-T%s" % t], |
| 568 | + capture_output=True, |
| 569 | + input=dot_string, |
| 570 | + text=True, |
| 571 | + ) |
| 572 | + else: |
| 573 | + proc = subprocess.run( |
| 574 | + ["dot", "-T%s" % t], |
| 575 | + input=bytes(dot_string, encoding="utf8"), |
| 576 | + ) |
| 577 | + return proc.stdout |
| 578 | + except: |
| 579 | + raise Exception( |
| 580 | + "Cannot create image representation by running dot from string: {}" |
| 581 | + "".format(dot_string) |
| 582 | + ) |
568 | 583 | except OSError as e: |
569 | 584 | raise Exception("Cannot find the dot binary from Graphviz package") from e |
570 | | - out, err = proc.stdout, proc.stderr |
571 | | - if err: |
572 | | - raise Exception( |
573 | | - "Cannot create image representation by running dot from string: {}" |
574 | | - "".format(dot_string) |
575 | | - ) |
576 | | - return out |
577 | 585 |
|
578 | 586 |
|
579 | 587 | class DependencyGraphError(Exception): |
|
0 commit comments