Spaces:
Running
Running
Commit ·
ca7d138
1
Parent(s): 3f5f11a
Align Markdown preprocessing with master
Browse files- tts_processor.py +172 -2
tts_processor.py
CHANGED
|
@@ -413,10 +413,180 @@ def tech_humanize(text):
|
|
| 413 |
|
| 414 |
return text
|
| 415 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 416 |
# Main preprocessing pipeline
|
| 417 |
def preprocess_all(string):
|
| 418 |
-
|
| 419 |
-
string = re.sub(r"(?m)^\s{0,3}[-*+]\s+", "", string)
|
| 420 |
string = normalize_dates(string)
|
| 421 |
string = replace_invalid_chars(string)
|
| 422 |
string = replace_numbers(string)
|
|
|
|
| 413 |
|
| 414 |
return text
|
| 415 |
|
| 416 |
+
# Markdown block syntax
|
| 417 |
+
_MD_FENCE = re.compile(r'^\s{0,3}(```|~~~)(.*)$')
|
| 418 |
+
_MD_RULE = re.compile(r'^\s{0,3}([-*_])[ \t]*(?:\1[ \t]*){2,}$')
|
| 419 |
+
_MD_HEADING = re.compile(r'^\s{0,3}#{1,6}[ \t]+')
|
| 420 |
+
_MD_QUOTE = re.compile(r'^\s{0,3}(?:>[ \t]?)+')
|
| 421 |
+
_MD_BULLET = re.compile(r'^\s*[-*+][ \t]+')
|
| 422 |
+
_MD_ORDERED = re.compile(r'^\s*\d{1,9}[.)][ \t]+')
|
| 423 |
+
_MD_TASK = re.compile(r'^\s*\[[ xX]\][ \t]+')
|
| 424 |
+
_MD_SEPARATOR_CELL = re.compile(r'^:?-+:?$')
|
| 425 |
+
|
| 426 |
+
|
| 427 |
+
def _split_table_cells(line):
|
| 428 |
+
"""Split a markdown table row into its cell values."""
|
| 429 |
+
stripped = line.strip()
|
| 430 |
+
if stripped.startswith('|'):
|
| 431 |
+
stripped = stripped[1:]
|
| 432 |
+
if stripped.endswith('|'):
|
| 433 |
+
stripped = stripped[:-1]
|
| 434 |
+
return [cell.strip() for cell in stripped.split('|')]
|
| 435 |
+
|
| 436 |
+
|
| 437 |
+
def _is_table_separator(line):
|
| 438 |
+
"""True if the line is a markdown table separator row such as |---|---|."""
|
| 439 |
+
if '|' not in line:
|
| 440 |
+
return False
|
| 441 |
+
cells = _split_table_cells(line)
|
| 442 |
+
if not cells:
|
| 443 |
+
return False
|
| 444 |
+
return all(_MD_SEPARATOR_CELL.match(cell) for cell in cells)
|
| 445 |
+
|
| 446 |
+
|
| 447 |
+
def _render_table(header, rows):
|
| 448 |
+
"""Render a markdown table as labelled sentences, e.g. 'Host: srv1, Status: up.'"""
|
| 449 |
+
parts = []
|
| 450 |
+
for row in rows:
|
| 451 |
+
cells = []
|
| 452 |
+
for index, value in enumerate(row):
|
| 453 |
+
if not value:
|
| 454 |
+
continue
|
| 455 |
+
label = header[index] if index < len(header) else ''
|
| 456 |
+
if label and label.lower() not in value.lower():
|
| 457 |
+
cells.append(f"{label}: {value}")
|
| 458 |
+
else:
|
| 459 |
+
cells.append(value)
|
| 460 |
+
if cells:
|
| 461 |
+
parts.append(", ".join(cells))
|
| 462 |
+
if not parts:
|
| 463 |
+
return ""
|
| 464 |
+
return ". ".join(parts) + "."
|
| 465 |
+
|
| 466 |
+
|
| 467 |
+
def _replace_markdown_link(match):
|
| 468 |
+
"""Keep the link label and append the URL when it carries extra information."""
|
| 469 |
+
label = match.group(1).strip()
|
| 470 |
+
url = match.group(2).strip()
|
| 471 |
+
if not url:
|
| 472 |
+
return label
|
| 473 |
+
if not label:
|
| 474 |
+
return url
|
| 475 |
+
if url.lower() in label.lower():
|
| 476 |
+
return label
|
| 477 |
+
return f"{label}, {url}"
|
| 478 |
+
|
| 479 |
+
|
| 480 |
+
def _strip_inline_markdown(text, escaped):
|
| 481 |
+
"""Remove inline markdown syntax from a single run of text."""
|
| 482 |
+
# Escaped punctuation first, stashed so emphasis rules cannot touch it
|
| 483 |
+
text = re.sub(r'\\([\\`*_{}\[\]()#+\-.!>~|])', lambda m: escaped.append(m.group(1)) or f"\x00{len(escaped) - 1}\x00", text)
|
| 484 |
+
# Inline code spans
|
| 485 |
+
text = re.sub(r'`+([^`]*)`+', r'\1', text)
|
| 486 |
+
# Images carry no spoken content
|
| 487 |
+
text = re.sub(r'!\[[^\]]*\]\([^)]*\)', '', text)
|
| 488 |
+
# Links: [label](url)
|
| 489 |
+
text = re.sub(r'\[([^\]]*)\]\(([^)\s]*)(?:\s+"[^"]*")?\)', _replace_markdown_link, text)
|
| 490 |
+
# Reference links: [label][ref]
|
| 491 |
+
text = re.sub(r'\[([^\]]*)\]\[[^\]]*\]', r'\1', text)
|
| 492 |
+
# Autolinks: <https://example.com>
|
| 493 |
+
text = re.sub(r'<((?:https?|mailto):[^>\s]+)>', r'\1', text)
|
| 494 |
+
# HTML tags
|
| 495 |
+
text = re.sub(r'</?[a-zA-Z][^>]*>', '', text)
|
| 496 |
+
# Heading markers that do not start a line, e.g. "**Bold** ## Heading"
|
| 497 |
+
text = re.sub(r'(?<!\S)#{1,6}[ \t]+', '', text)
|
| 498 |
+
# Strikethrough
|
| 499 |
+
text = re.sub(r'~~(.+?)~~', r'\1', text)
|
| 500 |
+
# Bold
|
| 501 |
+
text = re.sub(r'\*\*(.+?)\*\*', r'\1', text)
|
| 502 |
+
text = re.sub(r'(?<!\w)__(.+?)__(?!\w)', r'\1', text)
|
| 503 |
+
# Italic
|
| 504 |
+
text = re.sub(r'(?<!\w)\*([^*\n]+)\*(?!\w)', r'\1', text)
|
| 505 |
+
text = re.sub(r'(?<!\w)_([^_\n]+)_(?!\w)', r'\1', text)
|
| 506 |
+
return text
|
| 507 |
+
|
| 508 |
+
|
| 509 |
+
def strip_markdown(string):
|
| 510 |
+
"""
|
| 511 |
+
Remove markdown syntax so the text reads naturally when spoken.
|
| 512 |
+
|
| 513 |
+
Handles headings, lists, blockquotes, tables, code fences, links, images,
|
| 514 |
+
emphasis and horizontal rules. Tables are rendered as labelled sentences
|
| 515 |
+
rather than dropped, because the row and column names are the content.
|
| 516 |
+
"""
|
| 517 |
+
if not string:
|
| 518 |
+
return string
|
| 519 |
+
|
| 520 |
+
escaped = []
|
| 521 |
+
lines = string.replace('\r\n', '\n').replace('\r', '\n').split('\n')
|
| 522 |
+
output = []
|
| 523 |
+
in_code_block = False
|
| 524 |
+
index = 0
|
| 525 |
+
|
| 526 |
+
while index < len(lines):
|
| 527 |
+
line = lines[index]
|
| 528 |
+
|
| 529 |
+
if in_code_block:
|
| 530 |
+
if _MD_FENCE.match(line):
|
| 531 |
+
in_code_block = False
|
| 532 |
+
else:
|
| 533 |
+
output.append(line)
|
| 534 |
+
index += 1
|
| 535 |
+
continue
|
| 536 |
+
|
| 537 |
+
fence = _MD_FENCE.match(line)
|
| 538 |
+
if fence:
|
| 539 |
+
in_code_block = True
|
| 540 |
+
index += 1
|
| 541 |
+
continue
|
| 542 |
+
|
| 543 |
+
# Table: a header row followed by a separator row
|
| 544 |
+
if '|' in line and index + 1 < len(lines) and _is_table_separator(lines[index + 1]):
|
| 545 |
+
header = _split_table_cells(line)
|
| 546 |
+
index += 2
|
| 547 |
+
rows = []
|
| 548 |
+
while index < len(lines) and '|' in lines[index] and lines[index].strip():
|
| 549 |
+
rows.append(_split_table_cells(lines[index]))
|
| 550 |
+
index += 1
|
| 551 |
+
rendered = _render_table(header, rows)
|
| 552 |
+
if rendered:
|
| 553 |
+
output.append(rendered)
|
| 554 |
+
continue
|
| 555 |
+
|
| 556 |
+
# Orphaned separator row, e.g. a table header that was never rendered
|
| 557 |
+
if _is_table_separator(line):
|
| 558 |
+
index += 1
|
| 559 |
+
continue
|
| 560 |
+
|
| 561 |
+
# Horizontal rule
|
| 562 |
+
if _MD_RULE.match(line):
|
| 563 |
+
index += 1
|
| 564 |
+
continue
|
| 565 |
+
|
| 566 |
+
# Heading
|
| 567 |
+
line = _MD_HEADING.sub('', line)
|
| 568 |
+
|
| 569 |
+
# Blockquote
|
| 570 |
+
line = _MD_QUOTE.sub('', line)
|
| 571 |
+
|
| 572 |
+
# List markers, including task list checkboxes
|
| 573 |
+
line = _MD_BULLET.sub('', line)
|
| 574 |
+
line = _MD_ORDERED.sub('', line)
|
| 575 |
+
line = _MD_TASK.sub('', line)
|
| 576 |
+
|
| 577 |
+
output.append(line)
|
| 578 |
+
index += 1
|
| 579 |
+
|
| 580 |
+
text = _strip_inline_markdown("\n".join(output), escaped)
|
| 581 |
+
|
| 582 |
+
# Restore escaped punctuation
|
| 583 |
+
text = re.sub(r'\x00(\d+)\x00', lambda m: escaped[int(m.group(1))], text)
|
| 584 |
+
|
| 585 |
+
return text
|
| 586 |
+
|
| 587 |
# Main preprocessing pipeline
|
| 588 |
def preprocess_all(string):
|
| 589 |
+
string = strip_markdown(string)
|
|
|
|
| 590 |
string = normalize_dates(string)
|
| 591 |
string = replace_invalid_chars(string)
|
| 592 |
string = replace_numbers(string)
|