HarvestForTwo / content_items.py
princessamygdala's picture
Claude Opus 5
The correct quiz answer is no longer always the top choice
730c8df
Raw History Blame Contribute Delete
5.83 kB
"""Load the study text and item banks from content/.
The wording lives in files rather than in code so it can be edited without
touching Python, and so an IRB-approved consent form is stored verbatim instead
of being retyped into a string literal.
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.
"""
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.
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(dyad=False, first_task=None):
"""The consent form for this session, or "" if none has been supplied.
Two versions, because they are not the same document. The dyadic study
consents a parent about themselves and their child; the SONA sample has no
child, and telling an undergraduate that "whether or not you and your child
take part is up to you" is simply wrong. consent.md is the neutral form,
consent_dyad.md the family one. If the dyad form is missing the neutral one
is NOT substituted -- a parent must not be consented on a form that never
mentions their child.
`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.
"""
name = "consent_dyad.md" if dyad else "consent.md"
text = _read(name)
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 lands unevenly on Boldness, Meanness and Disinhibition.
Pass the pid. None keeps file order, which is what the scoring key and the
export columns are written in.
"""
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 consent_text(dyad=True):
out.append("consent_dyad.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)")
m = missing()
if m:
print("\nstill to fill in: " + ", ".join(m))