diff --git a/dphon/cli.py b/dphon/cli.py index 97ab09f..bfa8898 100644 --- a/dphon/cli.py +++ b/dphon/cli.py @@ -148,17 +148,16 @@ def run() -> None: elif args["--output-format"] == "html": console.record = True with console.capture(): - for match in results: - console.print(match) + for doc in graph.docs: + for group in doc._.groups: + console.print(group) sys.stdout.write(console.export_html()) else: # use system pager by default; colorize if LESS=R with console.pager(styles=os.getenv("LESS", "") == "R"): for doc in graph.docs: for group in doc._.groups: - console.print(group, "\n") - # for match in results: - # console.print(match) + console.print(group) def setup(args: Dict) -> Language: diff --git a/dphon/console.py b/dphon/console.py index 1605819..8e69dd4 100644 --- a/dphon/console.py +++ b/dphon/console.py @@ -136,8 +136,9 @@ def _add_span_context(self, span: Span) -> Tuple[str, str]: Context coloration can be changed by the default theme; a dim appearance is used in terminals. """ - context_left = ( - f"[context]{span.doc[span.start-self.context:span.start]}[/context]" + context_left = span.doc[span.start - self.context : span.start] + context_right = span.doc[span.end : span.end + self.context] + return ( + f"[context]{context_left.text.rjust(self.context, " ")}[/context]", + f"[context]{context_right.text.ljust(self.context, " ")}[/context]", ) - context_right = f"[context]{span.doc[span.end:span.end+self.context]}[/context]" - return context_left, context_right diff --git a/dphon/reuse.py b/dphon/reuse.py index 747ca1c..4d66165 100644 --- a/dphon/reuse.py +++ b/dphon/reuse.py @@ -9,6 +9,7 @@ from networkx import MultiGraph, create_empty_copy from rich.console import Console, ConsoleOptions, RenderResult from rich.progress import BarColumn, Progress, SpinnerColumn +from rich.table import Table from spacy.tokens import Doc, Span from .align import Aligner @@ -35,29 +36,32 @@ def __rich_console__( self, console: Console, options: ConsoleOptions ) -> RenderResult: """Format the group for display in console.""" - render_results = [] + table = Table(title=self.anchor_span.text, title_justify="left", show_header=False) + table.add_column("doc", no_wrap=True) + table.add_column("text") + table.add_column("transcription") # render the "anchor" span first (i.e., the span that all matches share) - render_results += [ - f"[bold]{self.doc._.id}[/bold] ({self.start}–{self.end-1}):", + table.add_row( + f"{self.doc._.id} ({self.start}–{self.end-1})", console.highlighter.format_span(self.anchor_span), console.highlighter.transcribe_span(self.anchor_span), - ] + ) # render the non-anchor spans from each match in the group - for i, match in enumerate(self.matches): + for match in self.matches: span = self.non_anchor_span(match) alignment = self.non_anchor_alignment(match) anchor_alignment = self.anchor_alignment(match) - render_results += [ - f"{i + 1}. {span.doc._.id} ({span.start}–{span.end-1}):", + table.add_row( + f"{span.doc._.id} ({span.start}–{span.end-1})", console.highlighter.format_span( span, self.anchor_span, alignment, anchor_alignment ), console.highlighter.transcribe_span(span), - ] + ) - return render_results + return [table] @cached_property def anchor_span(self) -> Span: