# Copyright (c) 2026 LlamaIndex, Inc. All rights reserved. # Licensed under the OCRia-1 Model and Code License (LICENSE at the root of this repository). # This is not open-source software: see the license for permitted uses. """Content words of a transcript (shared by the GPU capture and the scoring): character spans that hold page text, with markup removed, so ocria's RCL / HTML / markdown and Gemini's markdown / HTML are cut the same way.""" import re # HTML tags (a letter after "<", one line: a bare "<" is text, e.g. RCL's merge-left cell or "< 8.0 V"); # markdown link / image targets TAG = re.compile(r"\n]*>|\]\([^)\s]*\)|!\[") # a word starts with a letter or digit (or a currency sign / opening bracket before one) and runs over letters, digits and # in-word punctuation; markdown/RCL markup (#, *, |, !, `, _, quotes) is never part of a word WORD = re.compile(r"[$€£¥₹(]?[^\W_][^\s<>|*`\"\t]*") _NON_WORD = re.compile(r"[\W_]") TRAIL = ".,;:)]}!?'’" def norm(w: str) -> str: lowered = w.lower() return lowered if lowered.isalnum() else _NON_WORD.sub("", lowered) def word_spans(text: str) -> list[tuple[int, int, str]]: masked = TAG.sub(lambda m: " " * len(m.group()), text) masked = masked.replace(" ", " ").replace("&", "& ") out = [] for m in WORD.finditer(masked): s, e = m.start(), m.end() while ( e > s + 1 and masked[e - 1] in TRAIL and not (masked[e - 1] == ")" and "(" in masked[s : e - 1]) ): e -= 1 w = text[s:e] if norm(w): out.append((s, e, w)) return out