Download content_items.py from princessamygdala/TreasureHuntForTwo: direct link, hf CLI and curl.
- Browser
- Download file 5.58 kB
-
https://huggingface.co/spaces/princessamygdala/TreasureHuntForTwo/resolve/main/content_items.py
- Command line
-
hf download hf://spaces/princessamygdala/TreasureHuntForTwo/content_items.py
-
curl -L -o content_items.py https://huggingface.co/spaces/princessamygdala/TreasureHuntForTwo/resolve/main/content_items.py
5.58 kB
| """Load the SONA-study text and item banks from content/ (ported from task_pe-cu-nicewebrl). | |
| Nothing here ships with any instrument. A file that is missing, empty, or still carrying its | |
| PLACEHOLDER line reads as "not supplied yet", and the matching stage is skipped. That is | |
| deliberate: a half-filled folder should never block a pilot run, and should never show a | |
| participant a placeholder. | |
| Only reached via the combined-SONA-study flow (?study=sona) -- see order_link.py and | |
| web_app.py's finish_experiment(). The child CU-traits study never calls this. | |
| """ | |
| import os | |
| import random | |
| import zlib | |
| HERE = os.path.dirname(os.path.abspath(__file__)) | |
| CONTENT = os.path.join(HERE, "content") | |
| PLACEHOLDER = "PLACEHOLDER" | |
| def _read(name): | |
| try: | |
| with open(os.path.join(CONTENT, name), encoding="utf-8") as f: | |
| return f.read() | |
| except FileNotFoundError: | |
| return "" | |
| def _lines(name): | |
| """Non-blank, non-comment lines, or [] if the file is a placeholder.""" | |
| text = _read(name) | |
| if not text.strip() or PLACEHOLDER in text: | |
| return [] | |
| out = [] | |
| for raw in text.splitlines(): | |
| line = raw.strip() | |
| if line and not line.startswith("#"): | |
| out.append(line) | |
| return out | |
| #: The one sentence in consent.md that depends on which task this participant does first. The | |
| #: combined study randomises the order, and the form used to state that the treasure hunt came | |
| #: first, so half of the sample read an order they were not going to get. Three fixed strings, | |
| #: picked by the order this session already resolved, never generated. Mirrors | |
| #: task_pe-cu-nicewebrl's content_items.py -- byte-identical sentences, so a participant never | |
| #: sees the two tasks described differently depending on which one they're reading this on. | |
| TASK_ORDER_SENTENCES = { | |
| "pe": "You will do the corn harvest first and the treasure hunt second.", | |
| "sl": "You will do the treasure hunt first and the corn harvest second.", | |
| } | |
| TASK_ORDER_UNKNOWN = "Which of the two you do first is decided at random." | |
| TASK_ORDER_SLOT = "{TASK_ORDER}" | |
| def consent_text(first_task=None): | |
| """The combined-study consent form, or "" if none has been supplied. | |
| One version only: the SONA sample is solo adults (no dyad/parent-child variant here -- | |
| that concept belongs to this task's own separate ?dyad_mode, an unrelated child-study flag). | |
| `first_task` is "pe", "sl", or None for a session that has not resolved an order, which is | |
| every single-task run. The slot is filled either way, so a form can never reach a | |
| participant with a brace in it. | |
| """ | |
| text = _read("consent.md") | |
| if not text.strip() or PLACEHOLDER in text: | |
| return "" | |
| return text.replace(TASK_ORDER_SLOT, | |
| TASK_ORDER_SENTENCES.get(first_task, TASK_ORDER_UNKNOWN)) | |
| def tripm_items(order_seed=None): | |
| """[(key, item_text)], in file order or in a per-participant order. | |
| Keys are positional (tripm_1, tripm_2, ...), so reordering the FILE renumbers the columns. | |
| Fix the item list before collecting data. | |
| `order_seed` shuffles the presentation only, and the key travels with its own item, so | |
| tripm_key.csv keeps scoring what it was written against. 58 items on one page in one fixed | |
| order means the last items are the tired ones for everyone, and the three scales are | |
| interleaved in the published order, so that attention cost lands unevenly across Boldness, | |
| Meanness and Disinhibition. Pass the pid. None keeps file order, which is what the scoring | |
| key and the export columns are written in. Mirrors task_pe-cu-nicewebrl's content_items.py. | |
| """ | |
| items = [(f"tripm_{i}", t) for i, t in enumerate(_lines("tripm.txt"), start=1)] | |
| if order_seed and items: | |
| rng = random.Random(zlib.crc32(f"tripm|{order_seed}".encode())) | |
| rng.shuffle(items) | |
| return items | |
| def demographics_items(): | |
| """[{key, question, type, options}] from the pipe-separated file. | |
| Malformed lines are dropped rather than raising: a typo in the item file should cost you | |
| that one question, not the whole session. | |
| """ | |
| items = [] | |
| for line in _lines("demographics.txt"): | |
| parts = [p.strip() for p in line.split("|")] | |
| if len(parts) < 3: | |
| continue | |
| key, question, qtype = parts[0], parts[1], parts[2].lower() | |
| if not key or not question: | |
| continue | |
| if key.lower() == "key" and question.lower() == "question": | |
| continue # the header row from the README | |
| opts = [o.strip() for o in parts[3].split(";")] if len(parts) > 3 and parts[3] else [] | |
| if qtype not in ("number", "text", "choice", "scale", "dropdown", "multi"): | |
| continue | |
| if qtype in ("choice", "dropdown", "multi") and not opts: | |
| continue # a choice with no options is unanswerable | |
| items.append({"key": key, "question": question, "type": qtype, "options": opts}) | |
| return items | |
| def missing(): | |
| """Which content files are still placeholders -- for a startup log line.""" | |
| out = [] | |
| if not consent_text(): | |
| out.append("consent.md") | |
| if not demographics_items(): | |
| out.append("demographics.txt") | |
| if not tripm_items(): | |
| out.append("tripm.txt") | |
| return out | |
| if __name__ == "__main__": | |
| print(f"consent: {'supplied' if consent_text() else 'PLACEHOLDER (stage skipped)'}") | |
| print(f"demographics: {len(demographics_items())} item(s)") | |
| print(f"tripm: {len(tripm_items())} item(s)") | |