Skip to content

Commit

Permalink
Add table formatting for Match objects
Browse files Browse the repository at this point in the history
  • Loading branch information
thatbudakguy committed Jul 28, 2024
1 parent 85f97f5 commit 25be44a
Showing 1 changed file with 23 additions and 15 deletions.
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

0 comments on commit 25be44a

Please sign in to comment.