Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Render results as justified tables for easier reading #368

Merged
merged 4 commits into from
Jul 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
12 changes: 7 additions & 5 deletions dphon/console.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# -*- coding: utf-8 -*-

from typing import List, Tuple

from rich.console import Console
Expand Down Expand Up @@ -98,7 +100,6 @@ def _mark_span(
for i in range(len(span)):
# gap in u: insertion in v (if not punctuation)
if alignment[i] == self.gap_char and other_alignment[i].isalnum():
marked_span.append(alignment[i])
other_ptr += 1
continue

Expand Down Expand Up @@ -136,8 +137,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
38 changes: 23 additions & 15 deletions dphon/match.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import Levenshtein as Lev
from rich.console import Console, ConsoleOptions, RenderResult
from rich.padding import Padding
from rich.table import Table
from spacy.tokens import Span


Expand All @@ -30,24 +31,31 @@ def __rich_console__(
self, console: Console, options: ConsoleOptions
) -> RenderResult:
"""Format the match for display in console."""
table = Table(show_header=False, box=None)
table.add_column("doc")
table.add_column("bounds")
table.add_column("text")
table.add_column("transcription")

# get colorized match text and transcription
su, sv = console.highlighter.format_match(self) # type: ignore
pu, pv = console.highlighter.transcription(self) # type: ignore
su, sv = console.highlighter.format_match(self)
pu, pv = console.highlighter.transcribe_match(self)

# add rows for each document
table.add_row(self.u, f"{self.utxt.start}-{self.utxt.end-1}", su, pu)
table.add_row(self.v, f"{self.vtxt.start}-{self.vtxt.end-1}", sv, pv)

# add left-padding to align with match numbers, and bottom-padding
# so that there's a space between matches in output
su, sv, pu = map(lambda t: Padding(t, (0, 0, 0, 4)), (su, sv, pu))
pv = Padding(pv, (0, 0, 1, 4))
return [table]

# return everything as an iterable of renderables
return (
f"1. [white]{self.u}[/white] ({self.utxt.start}–{self.utxt.end-1}):",
su,
pu,
f"2. [white]{self.v}[/white] ({self.vtxt.start}–{self.vtxt.end-1}):",
sv,
pv,
)
@property
def graphic_similarity(self) -> float:
"""Levenshtein ratio of the aligned sequences."""
return Lev.seqratio(self.au, self.av)

@property
def phonetic_similarity(self) -> float:
"""Similarity score of the phonetic content of the sequences."""
return self.weight

@property
def weighted_score(self) -> float:
Expand Down
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
Loading