"""Reproduce factual LibriVox credit-sequence tables, without network or packages. Credits are catalogue identifiers, not independently verified voices or people. Run: python reproduce.py --input-dir INPUT --out OUTPUT """ import argparse import csv import json from collections import Counter, defaultdict from fractions import Fraction from pathlib import Path def write_csv(path, fields, rows): with path.open('w', encoding='utf-8', newline='') as handle: writer = csv.DictWriter(handle, fieldnames=fields, lineterminator='\n') writer.writeheader() writer.writerows(rows) def pct(numerator, denominator): if not denominator: return None value = Fraction(100 * numerator, denominator) # Rational half-up rounding to one decimal, independent of binary floats. rounded = (value.numerator * 20 + value.denominator) // (2 * value.denominator) return f'{rounded // 10}.{rounded % 10}' def ids(value): if not value: return () parts = value.split('|') if any(not item.isdigit() or int(item) <= 0 for item in parts): raise ValueError('Unresolved identifiers must be empty, never guessed.') if len(set(parts)) != len(parts): raise ValueError('Duplicate identifier inside selected credit set.') return tuple(sorted(parts, key=int)) def derive(input_dir, out): projects = json.loads((input_dir / 'project-facts.json').read_text(encoding='utf-8')) with (input_dir / 'section-credits.csv').open(encoding='utf-8', newline='') as handle: rows = list(csv.DictReader(handle)) grouped = defaultdict(list) for row in rows: grouped[row['project_id']].append(row) if len({project['project_id'] for project in projects}) != len(projects): raise ValueError('Project IDs must be unique.') if set(grouped) != {project['project_id'] for project in projects}: raise ValueError('Project and section IDs disagree.') complete = [] for project in projects: ss = grouped[project['project_id']] numbers = [int(row['section_number']) for row in ss] if len(numbers) != len(set(numbers)) or len(ss) != project['section_count']: raise ValueError('Section order/count is unresolved.') for row in ss: ids(row['reader_ids']) if all(row['reader_ids'] for row in ss): complete.append(project) if len(complete) < 8 or len({p['work'] for p in complete}) < 3: raise ValueError('Predeclared minimum source-resolution gate not met.') metrics, adjacent, runs, reader_counts, point_rows = [], [], [], [], [] for project in projects: ident = project['project_id'] ss = sorted(grouped[ident], key=lambda row: int(row['section_number'])) if [int(row['position']) for row in ss] != list(range(1, len(ss) + 1)): raise ValueError('Ordinal positions must follow numerical section order.') sequence = [ids(row['reader_ids']) for row in ss] count = Counter(reader for credit_set in sequence for reader in credit_set) known_pairs = singleton_pairs = handoffs = singleton_handoffs = 0 for index, (left, right) in enumerate(zip(sequence, sequence[1:])): eligible = bool(left) and bool(right) singleton = len(left) == len(right) == 1 changed = left != right if eligible else None known_pairs += eligible singleton_pairs += singleton handoffs += bool(changed) singleton_handoffs += singleton and left != right adjacent.append({'project_id': ident, 'left_position': index + 1, 'right_position': index + 2, 'left_section_number': ss[index]['section_number'], 'right_section_number': ss[index + 1]['section_number'], 'left_reader_ids': '|'.join(left), 'right_reader_ids': '|'.join(right), 'eligible_known_sets': int(eligible), 'credit_set_changed': '' if changed is None else int(changed), 'eligible_singleton_pair': int(singleton)}) singleton_runs = [] current = None for index, credit_set in enumerate(sequence): if len(credit_set) != 1: if current: singleton_runs.append(current) current = None continue reader = credit_set[0] if current and current['reader_id'] == reader: current.update(end_position=index + 1, end_section_number=ss[index]['section_number'], length=current['length'] + 1) else: if current: singleton_runs.append(current) current = {'project_id': ident, 'reader_id': reader, 'start_position': index + 1, 'end_position': index + 1, 'start_section_number': ss[index]['section_number'], 'end_section_number': ss[index]['section_number'], 'length': 1} if current: singleton_runs.append(current) runs.extend(singleton_runs) run_counts = Counter(row['reader_id'] for row in singleton_runs) singleton_complete = all(len(credit_set) == 1 for credit_set in sequence) minimum = len(count) - 1 if singleton_complete else None listed = project['header_individual_reader_ids'] metrics.append({'project_id': ident, 'work': project['work'], 'catalogue_title': project['catalogue_title'], 'header_read_by_text': project['header_read_by_text'], 'header_collective_label': int(project['header_is_collective_label']), 'header_individual_id_count': '' if project['header_individual_id_count'] is None else project['header_individual_id_count'], 'sections': len(ss), 'unique_section_reader_ids': len(count), 'unresolved_sections': sum(not s for s in sequence), 'multi_ID_sections': sum(len(s) > 1 for s in sequence), 'all_adjacencies': max(0, len(ss) - 1), 'eligible_known_set_adjacencies': known_pairs, 'credited_ID_set_handoffs': handoffs, 'handoff_percent': pct(handoffs, known_pairs), 'eligible_singleton_adjacencies': singleton_pairs, 'singleton_ID_handoffs': singleton_handoffs, 'singleton_runs': len(singleton_runs), 'longest_singleton_run_sections': max((r['length'] for r in singleton_runs), default=0), 'IDs_with_nonadjacent_returns': sum(n > 1 for n in run_counts.values()), 'hypothetical_contiguous_grouping_minimum_handoffs': '' if minimum is None else minimum, 'observed_minus_hypothetical_minimum': '' if minimum is None else handoffs - minimum, 'first_credit_ids': '|'.join(sequence[0]), 'last_credit_ids': '|'.join(sequence[-1]), 'listed_individual_IDs_absent_from_sections': '|'.join(sorted(set(listed) - set(count), key=int))}) for reader, occurrences in sorted(count.items(), key=lambda item: int(item[0])): reader_counts.append({'project_id': ident, 'reader_id': reader, 'credited_sections': occurrences, 'singleton_runs': run_counts[reader], 'nonadjacent_return': int(run_counts[reader] > 1)}) for index, credit_set in enumerate(sequence): point_rows.append({'project_id': ident, 'position': index + 1, 'section_number': ss[index]['section_number'], 'reader_ids': '|'.join(credit_set)}) out.mkdir(parents=True, exist_ok=True) for name, table in [('project-metrics.csv', metrics), ('adjacencies.csv', adjacent), ('singleton-runs.csv', runs), ('reader-credit-counts.csv', reader_counts), ('sequence-chart-points.csv', point_rows)]: write_csv(out / name, list(table[0]), table) summary = {'projects': len(projects), 'source_matched_works': len({p['work'] for p in projects}), 'sections': len(rows), 'fully_resolved_projects': len(complete), 'all_adjacencies_checksum': len(adjacent), 'credited_ID_changes_checksum': sum(p['credited_ID_set_handoffs'] for p in metrics), 'singleton_projects': sum(p['unique_section_reader_ids'] == 1 for p in metrics), 'collective_header_projects': sum(p['header_collective_label'] for p in metrics), 'limitations': 'Selected catalogue credit identifiers and file boundaries only; no audio, voice/person equivalence, quality, attention, behaviour, text identity or population prevalence measured. Grouping minimum is hypothetical, not an observed recording or edit recommendation.'} (out / 'summary.json').write_text(json.dumps(summary, ensure_ascii=False, indent=2) + '\n', encoding='utf-8', newline='\n') return summary if __name__ == '__main__': parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('--input-dir', type=Path, required=True) parser.add_argument('--out', type=Path, required=True) args = parser.parse_args() derive(args.input_dir, args.out)