henry-yeh's picture htr234234's picture
Chiron data release
1b86964
Raw History Blame Contribute Delete
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))