Datasets:
Download code/domain_adapter/wvs.py from chiron-group/chiron-data: direct link, hf CLI and curl.
- Browser
- Download file 3.81 kB
-
https://huggingface.co/datasets/chiron-group/chiron-data/resolve/main/code/domain_adapter/wvs.py
- Command line
-
hf download hf://datasets/chiron-group/chiron-data/code/domain_adapter/wvs.py
-
curl -L -o wvs.py https://huggingface.co/datasets/chiron-group/chiron-data/resolve/main/code/domain_adapter/wvs.py
3.81 kB
| """WVS (World Values Survey) runtime adapter. | |
| Data construction lives in the dataset repo (``chiron-group/wvs`` / local mirror | |
| ``data/raw/wvs/scripts/``); this module is runtime-only. | |
| """ | |
| from __future__ import annotations | |
| import random | |
| import re | |
| from pathlib import Path | |
| from domain_adapter.base import DomainAdapter, Sample | |
| MIN_OBSERVATIONS = 50 | |
| MAX_OBSERVATIONS = 100 | |
| _ADMIN_RE = re.compile( | |
| r"\((?=[^)]*(?:read out|read and code|code|interviewer|not applicable))[^)]*\)", | |
| re.IGNORECASE, | |
| ) | |
| _ADMIN_TAIL_RE = re.compile( | |
| r"\b(?:interviewer|do not read|code\s+[-\"'\w ]*not applicable)\b.*$", | |
| re.IGNORECASE, | |
| ) | |
| _DANGLING_PROMPT_TAIL_RE = re.compile( | |
| r"([?.!])\s+(?:Are you|Would you say it is)(?=\s*(?:\||$))", | |
| re.IGNORECASE, | |
| ) | |
| def _clean_wvs_text(text: str) -> str: | |
| """Remove WVS interviewer/routing artifacts without rewriting item meaning.""" | |
| text = " ".join(str(text).strip().split()) | |
| text = _ADMIN_RE.sub("", text) | |
| text = _ADMIN_TAIL_RE.sub("", text) | |
| text = _DANGLING_PROMPT_TAIL_RE.sub(r"\1", text) | |
| text = re.sub(r"\s+", " ", text) | |
| return text.strip() | |
| def _clean_wvs_observation(observation: str) -> str: | |
| if " | Response:" not in observation: | |
| return _clean_wvs_text(observation) | |
| item, response = observation.split(" | Response:", 1) | |
| item = _clean_wvs_text(item) | |
| response = _clean_wvs_text(response) | |
| if item and response: | |
| return f"{item} | Response: {response}" | |
| return item or response | |
| def _clean_wvs_question(question: str) -> str: | |
| lines: list[str] = [] | |
| for raw_line in question.strip().splitlines(): | |
| line = raw_line.strip() | |
| if not line: | |
| continue | |
| if line.startswith("Survey question:"): | |
| body = _clean_wvs_text(line.removeprefix("Survey question:")) | |
| if body: | |
| lines.append(f"Survey question: {body}") | |
| elif line.startswith("Question supplement:"): | |
| body = _clean_wvs_text(line.removeprefix("Question supplement:")) | |
| if body: | |
| lines.append(f"Question supplement: {body}") | |
| else: | |
| lines.append(_clean_wvs_text(line)) | |
| return "\n".join(line for line in lines if line) | |
| class WVSAdapter(DomainAdapter): | |
| name = "wvs" | |
| def data_source(self) -> str: | |
| return "indievalue_wvs" | |
| def get_sample_id(self, sample: Sample) -> str: | |
| return str(sample.extra_info["interview_id"]) | |
| def default_input_path(self) -> Path: | |
| return Path("data/raw/wvs/qa_jsonl/train.jsonl") | |
| def record_context(self) -> str: | |
| return "Below are survey response records describing this respondent's values, beliefs, and background." | |
| def format_observations_block(self, observations: list[str]) -> str: | |
| rendered: list[str] = [] | |
| for observation in observations: | |
| text = _clean_wvs_observation(observation) | |
| if not text or re.fullmatch(r"-\d+(?:\.0)?", text): | |
| continue | |
| rendered.append(f"- {text}") | |
| return "\n".join(rendered) | |
| def format_question_block(self, questions: list[str]) -> str: | |
| return "\n\n".join( | |
| f"Q{i}.\n{_clean_wvs_question(question)}" | |
| for i, question in enumerate(questions, start=1) | |
| ) | |
| def select_observations( | |
| self, | |
| sample: Sample, | |
| *, | |
| seed: int, | |
| sample_index: int, | |
| ) -> list[str]: | |
| observations = list(sample.observations) | |
| if len(observations) <= MIN_OBSERVATIONS: | |
| return observations | |
| rng = random.Random(seed ^ (sample_index * 9173) ^ 0x575653) | |
| rng.shuffle(observations) | |
| n_observations = rng.randint(MIN_OBSERVATIONS, min(MAX_OBSERVATIONS, len(observations))) | |
| return observations[:n_observations] | |