Datasets:
Download code/check_export_hub.py from rvashurin/coqar-clarifications: direct link, hf CLI and curl.
- Browser
- Download file 3.97 kB
-
https://huggingface.co/datasets/rvashurin/coqar-clarifications/resolve/main/code/check_export_hub.py
- Command line
-
hf download hf://datasets/rvashurin/coqar-clarifications/code/check_export_hub.py
-
curl -L -o check_export_hub.py https://huggingface.co/datasets/rvashurin/coqar-clarifications/resolve/main/code/check_export_hub.py
3.97 kB
| """Offline placement and integrity tests; no inference or Hub operations.""" | |
| import json | |
| from pathlib import Path | |
| import tempfile | |
| import unittest | |
| import pyarrow as pa | |
| import pyarrow.parquet as pq | |
| from export_hub import arrow_schema, place_addition, validate_example | |
| class PlacementTests(unittest.TestCase): | |
| def check_roundtrip(self, text, raw, placement, anchor): | |
| result = place_addition(text, raw, placement, anchor) | |
| output = result["clarified_story"] | |
| self.assertEqual(output[:result["inserted_block_start"]] + output[result["inserted_block_end"]:], text) | |
| self.assertEqual(output[result["addition_start"]:result["addition_end"]], raw.strip()) | |
| return result | |
| def test_empty_addition_preserves_every_character(self): | |
| for addition in ("", " \n\t"): | |
| for placement in ("appended", "inserted"): | |
| for offset in (0, 3, 10): | |
| r = self.check_roundtrip("A.\n B.\t ", addition, placement, offset) | |
| self.assertTrue(r["unchanged"]) | |
| self.assertEqual(r["prefix_separator"], "") | |
| self.assertEqual(r["suffix_separator"], "") | |
| def test_appended_matches_existing_rule(self): | |
| r = self.check_roundtrip("A. \n", " Answer. ", "appended", 0) | |
| self.assertEqual(r["clarified_story"], "A. \n\n\nAnswer.") | |
| def test_insertion_preserves_whitespace_holes(self): | |
| r = self.check_roundtrip("A. \n B.", "X.", "inserted", 3) | |
| self.assertEqual(r["clarified_story"], "A. \n\nX.\n\n \n B.") | |
| def test_boundaries_unicode_and_raw_whitespace(self): | |
| for text in ("", "É. 🐈.\n", "\"A.\"B."): | |
| for offset in range(len(text) + 1): | |
| r = self.check_roundtrip(text, " 名. \n", "inserted", offset) | |
| self.assertEqual(r["prefix_separator"], "\n\n" if offset else "") | |
| self.assertEqual(r["suffix_separator"], "\n\n" if offset < len(text) else "") | |
| def test_invalid_anchor_rejected(self): | |
| for offset in (-1, 4, 1.5, True): | |
| with self.assertRaises(ValueError): | |
| place_addition("abc", "x", "inserted", offset) | |
| def test_multiple_overlapping_sentences_preserve_gap(self): | |
| original = "A. B. C. D." | |
| sentences = [dict(sentence_index=i, start=a, end=b, text=original[a:b]) | |
| for i, (a, b) in enumerate(((0, 2), (3, 5), (7, 9), (10, 12)))] | |
| answer = dict(turn_id=1, span_start=4, span_end=8, span_text=original[4:8]) | |
| example = dict(original_story=original, modified_story="A. D.", turn_id=1, | |
| question="Question?", gold_answer_annotation=answer, | |
| sentences=sentences, removed_sentences=sentences[1:3]) | |
| source = dict(story=original, answers=[answer], questions=[dict(turn_id=1, paraphrase=["", "Question?"])]) | |
| self.assertEqual(validate_example(example, source), (3, 9)) | |
| example["modified_story"] = "A. D." | |
| with self.assertRaises(ValueError): | |
| validate_example(example, source) | |
| def test_parquet_metadata_and_nested_list(self): | |
| metadata = {"publication_id": "test", "commit_hash": "test"} | |
| schema = arrow_schema(metadata) | |
| row = {field.name: None for field in schema} | |
| row["clarifications"] = [{field.name: None for field in schema.field("clarifications").type.value_type} for _ in range(5)] | |
| for i, sample in enumerate(row["clarifications"]): | |
| sample.update(sample_index=i, addition="", clarified_story="É. 🐈.") | |
| with tempfile.TemporaryDirectory() as d: | |
| p = Path(d) / "data.parquet" | |
| pq.write_table(pa.Table.from_pylist([row], schema=schema), p) | |
| loaded = pq.read_table(p) | |
| self.assertEqual(loaded.to_pylist(), [row]) | |
| self.assertEqual(json.loads(loaded.schema.metadata[b"coqar_provenance"]), metadata) | |
| if __name__ == "__main__": | |
| unittest.main() | |