Skip to content

Commit

Permalink
Render results as justified tables for easier reading
Browse files Browse the repository at this point in the history
  • Loading branch information
thatbudakguy committed Jul 13, 2024
1 parent a59afb2 commit 707b3b4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 18 deletions.
9 changes: 4 additions & 5 deletions dphon/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
9 changes: 5 additions & 4 deletions dphon/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
22 changes: 13 additions & 9 deletions dphon/reuse.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down

0 comments on commit 707b3b4

Please sign in to comment.