Datasets:
Download code/domain_adapter/ess09.py from chiron-group/chiron-data: direct link, hf CLI and curl.
- Browser
- Download file 2.29 kB
-
https://huggingface.co/datasets/chiron-group/chiron-data/resolve/main/code/domain_adapter/ess09.py
- Command line
-
hf download hf://datasets/chiron-group/chiron-data/code/domain_adapter/ess09.py
-
curl -L -o ess09.py https://huggingface.co/datasets/chiron-group/chiron-data/resolve/main/code/domain_adapter/ess09.py
2.29 kB
| """ESS09 psych-filtered runtime adapter.""" | |
| from __future__ import annotations | |
| import re | |
| from pathlib import Path | |
| from domain_adapter.base import DomainAdapter, Sample | |
| _ADMIN_RE = re.compile( | |
| r"\((?=[^)]*(?:read out|read and code|code|interviewer|not applicable))[^)]*\)", | |
| re.IGNORECASE, | |
| ) | |
| _ADMIN_TAIL_RE = re.compile( | |
| r"\b(?:interviewer|interveiwer|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_survey_text(text: str) -> str: | |
| 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_survey_observation(observation: str) -> str: | |
| if " | Response:" not in observation: | |
| return _clean_survey_text(observation) | |
| item, response = observation.split(" | Response:", 1) | |
| item = _clean_survey_text(item) | |
| response = _clean_survey_text(response) | |
| if item and response: | |
| return f"{item} | Response: {response}" | |
| return item or response | |
| class ESS09PsychAdapter(DomainAdapter): | |
| name = "ess09_psych" | |
| def data_source(self) -> str: | |
| return "ess09_psych" | |
| def get_sample_id(self, sample: Sample) -> str: | |
| return str(sample.extra_info["person_id"]) | |
| def default_input_path(self) -> Path: | |
| return Path("data/raw/ess09-psych/qa_jsonl/train.jsonl") | |
| def record_context(self) -> str: | |
| return "Below are survey response records describing this respondent's attitudes, values, and life context." | |
| def format_observations_block(self, observations: list[str]) -> str: | |
| return "\n".join(f"- {_clean_survey_observation(observation)}" for observation in observations) | |
| def format_question_block(self, questions: list[str]) -> str: | |
| cleaned = [] | |
| for question in questions: | |
| lines = [_clean_survey_text(line) for line in question.strip().splitlines()] | |
| cleaned.append("\n".join(line for line in lines if line)) | |
| return "\n\n".join(f"Q{i}.\n{question}" for i, question in enumerate(cleaned, start=1)) | |