| from __future__ import annotations |
|
|
| import hashlib |
| import re |
| import unicodedata |
| from typing import Iterable |
|
|
|
|
| WHITESPACE_RE = re.compile(r"\s+") |
| PUNCT_RE = re.compile(r"[^\w\s]", flags=re.UNICODE) |
| SENTENCE_BOUNDARY_RE = re.compile(r"(?<=[.!?。!?])\s+") |
|
|
|
|
| def normalize_whitespace(text: object) -> str: |
| if text is None: |
| return "" |
| return WHITESPACE_RE.sub(" ", str(text).replace("\x00", " ")).strip() |
|
|
|
|
| def normalize_for_hash(text: object) -> str: |
| normalized = unicodedata.normalize("NFKC", normalize_whitespace(text)).casefold() |
| normalized = normalized.replace("’", "'").replace("‘", "'") |
| normalized = normalized.replace("“", '"').replace("”", '"') |
| normalized = normalized.replace("–", "-").replace("—", "-") |
| normalized = PUNCT_RE.sub(" ", normalized) |
| return normalize_whitespace(normalized) |
|
|
|
|
| def stable_hash(text: object, length: int = 16) -> str: |
| return hashlib.sha1(normalize_for_hash(text).encode("utf-8")).hexdigest()[:length] |
|
|
|
|
| def stable_hash_raw(*parts: object, length: int = 16) -> str: |
| raw = "\u241f".join(normalize_whitespace(part) for part in parts) |
| return hashlib.sha1(raw.encode("utf-8")).hexdigest()[:length] |
|
|
|
|
| def word_count(text: object) -> int: |
| text = normalize_whitespace(text) |
| if not text: |
| return 0 |
| return len(text.split()) |
|
|
|
|
| def split_sentences(text: object) -> list[str]: |
| text = normalize_whitespace(text) |
| if not text: |
| return [] |
| parts = [normalize_whitespace(part) for part in SENTENCE_BOUNDARY_RE.split(text)] |
| return [part for part in parts if part] |
|
|
|
|
| def split_long_text_by_words(text: str, max_words: int, overlap_words: int = 30) -> list[str]: |
| words = text.split() |
| if len(words) <= max_words: |
| return [text] if text else [] |
| chunks: list[str] = [] |
| step = max(1, max_words - overlap_words) |
| for start in range(0, len(words), step): |
| chunk_words = words[start : start + max_words] |
| if chunk_words: |
| chunks.append(" ".join(chunk_words)) |
| if start + max_words >= len(words): |
| break |
| return chunks |
|
|
|
|
| def make_sentence_chunks( |
| sentences: Iterable[str], |
| max_words: int = 180, |
| overlap_sentences: int = 1, |
| ) -> list[dict[str, object]]: |
| sentence_list = [normalize_whitespace(sentence) for sentence in sentences if normalize_whitespace(sentence)] |
| chunks: list[dict[str, object]] = [] |
| i = 0 |
| while i < len(sentence_list): |
| current: list[str] = [] |
| start_i = i |
| total_words = 0 |
| while i < len(sentence_list): |
| sentence_words = word_count(sentence_list[i]) |
| if current and total_words + sentence_words > max_words: |
| break |
| if not current and sentence_words > max_words: |
| for sub_idx, sub_chunk in enumerate(split_long_text_by_words(sentence_list[i], max_words=max_words)): |
| chunks.append( |
| { |
| "text": sub_chunk, |
| "start_sent_id": i, |
| "end_sent_id": i, |
| "subchunk": sub_idx, |
| } |
| ) |
| i += 1 |
| break |
| current.append(sentence_list[i]) |
| total_words += sentence_words |
| i += 1 |
| if current: |
| chunks.append( |
| { |
| "text": normalize_whitespace(" ".join(current)), |
| "start_sent_id": start_i, |
| "end_sent_id": i - 1, |
| "subchunk": None, |
| } |
| ) |
| if overlap_sentences > 0 and i < len(sentence_list): |
| i = max(start_i + 1, i - overlap_sentences) |
| return chunks |
|
|
|
|
| def canonical_label(dataset: str, raw_label: object) -> str | None: |
| if raw_label is None: |
| return None |
| label = normalize_whitespace(raw_label) |
| if dataset == "vifactcheck": |
| return {"0": "SUPPORTS", "1": "REFUTES", "2": "NEI"}.get(label) |
| normalized = normalize_for_hash(label) |
| if dataset == "averitec": |
| mapping = { |
| "supported": "SUPPORTS", |
| "refuted": "REFUTES", |
| "not enough evidence": "NEI", |
| "conflicting evidence cherrypicking": "CONFLICTING", |
| "conflicting evidence cherry picking": "CONFLICTING", |
| "conflicting evidence cherry-picking": "CONFLICTING", |
| } |
| return mapping.get(normalized) |
| if dataset == "healthver": |
| mapping = { |
| "supports": "SUPPORTS", |
| "support": "SUPPORTS", |
| "refutes": "REFUTES", |
| "refute": "REFUTES", |
| "neutral": "NEI", |
| "nei": "NEI", |
| } |
| return mapping.get(normalized) |
| return label.upper() if label else None |
|
|
|
|
| def json_safe(value: object) -> object: |
| if value is None: |
| return None |
| if isinstance(value, (str, int, float, bool)): |
| return value |
| return str(value) |
|
|