Commit ·
c550325
0
Parent(s):
ChapterEnd: human and generated textbook questions, corpus and scoring scripts
Browse files- .gitattributes +60 -0
- README.md +174 -0
- bm25_baseline.py +29 -0
- corpus.jsonl +0 -0
- generated.jsonl +0 -0
- human.jsonl +0 -0
- score.py +21 -0
.gitattributes
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
*.7z filter=lfs diff=lfs merge=lfs -text
|
| 2 |
+
*.arrow filter=lfs diff=lfs merge=lfs -text
|
| 3 |
+
*.avro filter=lfs diff=lfs merge=lfs -text
|
| 4 |
+
*.bin filter=lfs diff=lfs merge=lfs -text
|
| 5 |
+
*.bz2 filter=lfs diff=lfs merge=lfs -text
|
| 6 |
+
*.ckpt filter=lfs diff=lfs merge=lfs -text
|
| 7 |
+
*.ftz filter=lfs diff=lfs merge=lfs -text
|
| 8 |
+
*.gz filter=lfs diff=lfs merge=lfs -text
|
| 9 |
+
*.h5 filter=lfs diff=lfs merge=lfs -text
|
| 10 |
+
*.joblib filter=lfs diff=lfs merge=lfs -text
|
| 11 |
+
*.lfs.* filter=lfs diff=lfs merge=lfs -text
|
| 12 |
+
*.lz4 filter=lfs diff=lfs merge=lfs -text
|
| 13 |
+
*.mds filter=lfs diff=lfs merge=lfs -text
|
| 14 |
+
*.mlmodel filter=lfs diff=lfs merge=lfs -text
|
| 15 |
+
*.model filter=lfs diff=lfs merge=lfs -text
|
| 16 |
+
*.msgpack filter=lfs diff=lfs merge=lfs -text
|
| 17 |
+
*.npy filter=lfs diff=lfs merge=lfs -text
|
| 18 |
+
*.npz filter=lfs diff=lfs merge=lfs -text
|
| 19 |
+
*.onnx filter=lfs diff=lfs merge=lfs -text
|
| 20 |
+
*.ot filter=lfs diff=lfs merge=lfs -text
|
| 21 |
+
*.parquet filter=lfs diff=lfs merge=lfs -text
|
| 22 |
+
*.pb filter=lfs diff=lfs merge=lfs -text
|
| 23 |
+
*.pickle filter=lfs diff=lfs merge=lfs -text
|
| 24 |
+
*.pkl filter=lfs diff=lfs merge=lfs -text
|
| 25 |
+
*.pt filter=lfs diff=lfs merge=lfs -text
|
| 26 |
+
*.pth filter=lfs diff=lfs merge=lfs -text
|
| 27 |
+
*.rar filter=lfs diff=lfs merge=lfs -text
|
| 28 |
+
*.safetensors filter=lfs diff=lfs merge=lfs -text
|
| 29 |
+
saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
| 30 |
+
*.tar.* filter=lfs diff=lfs merge=lfs -text
|
| 31 |
+
*.tar filter=lfs diff=lfs merge=lfs -text
|
| 32 |
+
*.tflite filter=lfs diff=lfs merge=lfs -text
|
| 33 |
+
*.tgz filter=lfs diff=lfs merge=lfs -text
|
| 34 |
+
*.wasm filter=lfs diff=lfs merge=lfs -text
|
| 35 |
+
*.xz filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 38 |
+
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 39 |
+
# Audio files - uncompressed
|
| 40 |
+
*.pcm filter=lfs diff=lfs merge=lfs -text
|
| 41 |
+
*.sam filter=lfs diff=lfs merge=lfs -text
|
| 42 |
+
*.raw filter=lfs diff=lfs merge=lfs -text
|
| 43 |
+
# Audio files - compressed
|
| 44 |
+
*.aac filter=lfs diff=lfs merge=lfs -text
|
| 45 |
+
*.flac filter=lfs diff=lfs merge=lfs -text
|
| 46 |
+
*.mp3 filter=lfs diff=lfs merge=lfs -text
|
| 47 |
+
*.ogg filter=lfs diff=lfs merge=lfs -text
|
| 48 |
+
*.wav filter=lfs diff=lfs merge=lfs -text
|
| 49 |
+
# Image files - uncompressed
|
| 50 |
+
*.bmp filter=lfs diff=lfs merge=lfs -text
|
| 51 |
+
*.gif filter=lfs diff=lfs merge=lfs -text
|
| 52 |
+
*.png filter=lfs diff=lfs merge=lfs -text
|
| 53 |
+
*.tiff filter=lfs diff=lfs merge=lfs -text
|
| 54 |
+
# Image files - compressed
|
| 55 |
+
*.jpg filter=lfs diff=lfs merge=lfs -text
|
| 56 |
+
*.jpeg filter=lfs diff=lfs merge=lfs -text
|
| 57 |
+
*.webp filter=lfs diff=lfs merge=lfs -text
|
| 58 |
+
# Video files - compressed
|
| 59 |
+
*.mp4 filter=lfs diff=lfs merge=lfs -text
|
| 60 |
+
*.webm filter=lfs diff=lfs merge=lfs -text
|
README.md
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: cc-by-nc-sa-4.0
|
| 3 |
+
language:
|
| 4 |
+
- en
|
| 5 |
+
task_categories:
|
| 6 |
+
- text-retrieval
|
| 7 |
+
- question-answering
|
| 8 |
+
pretty_name: ChapterEnd
|
| 9 |
+
size_categories:
|
| 10 |
+
- 1K<n<10K
|
| 11 |
+
tags:
|
| 12 |
+
- retrieval
|
| 13 |
+
- rag
|
| 14 |
+
- benchmark
|
| 15 |
+
- evaluation
|
| 16 |
+
- textbooks
|
| 17 |
+
- openstax
|
| 18 |
+
- synthetic-vs-human
|
| 19 |
+
configs:
|
| 20 |
+
- config_name: human
|
| 21 |
+
default: true
|
| 22 |
+
data_files:
|
| 23 |
+
- split: test
|
| 24 |
+
path: human.jsonl
|
| 25 |
+
- config_name: generated
|
| 26 |
+
data_files:
|
| 27 |
+
- split: test
|
| 28 |
+
path: generated.jsonl
|
| 29 |
+
- config_name: corpus
|
| 30 |
+
data_files:
|
| 31 |
+
- split: corpus
|
| 32 |
+
path: corpus.jsonl
|
| 33 |
+
---
|
| 34 |
+
|
| 35 |
+
# ChapterEnd
|
| 36 |
+
|
| 37 |
+
Real end-of-chapter questions from six open textbooks, side by side with LLM-written questions about the same sections.
|
| 38 |
+
|
| 39 |
+
I built it to answer one question: do retrieval scores measured on generated questions hold up on questions that people wrote? In my tests they don't. The same BM25 gets 91.7% Recall@1 on the generated questions here and 54.6% on the human ones.
|
| 40 |
+
|
| 41 |
+
## Why this exists
|
| 42 |
+
|
| 43 |
+
I was reproducing [STAIR](https://arxiv.org/abs/2609.03874), a paper from IBM Research that finds the right section of a book by reading its table of contents. Its benchmark, [SearchTome](https://huggingface.co/datasets/meghanadh/SearchTome), is made of questions written by an LLM, which is how most document benchmarks are built now. I wanted to see what happens with questions written by humans and couldn't find a test set for it.
|
| 44 |
+
|
| 45 |
+
OpenStax textbooks turned out to be a good source. Every chapter ends with review questions written by the book's authors, grouped by the section they belong to, so the correct answer comes with the question.
|
| 46 |
+
|
| 47 |
+
## What the two kinds look like
|
| 48 |
+
|
| 49 |
+
Three sections, with one question of each kind:
|
| 50 |
+
|
| 51 |
+
| Section | Written by the textbook authors | Written by an LLM |
|
| 52 |
+
|---|---|---|
|
| 53 |
+
| Sleep and Why We Sleep | How could researchers determine if given areas of the brain are involved in the regulation of sleep? | What does comparative research indicate about the relationship between predatory risk and the amount of deep sleep in different species? |
|
| 54 |
+
| Overview of Photosynthesis | What two products result from photosynthesis? | What process provides the majority of energy for living organisms on Earth? |
|
| 55 |
+
| How To Organize Economies | What are the three ways that societies can organize themselves economically? | How does the scarcity of resources affect the media's decision to send reporters to cover an event? |
|
| 56 |
+
|
| 57 |
+
The LLM was shown one paragraph at a time, so its questions follow that paragraph closely. The authors ask about the section as a whole, often in words the text doesn't use.
|
| 58 |
+
|
| 59 |
+
## The numbers
|
| 60 |
+
|
| 61 |
+
| | Human | Generated |
|
| 62 |
+
|---|---|---|
|
| 63 |
+
| Questions | 3,568 | 2,604 |
|
| 64 |
+
| Sections they point to | 492 (shared) | 492 (shared) |
|
| 65 |
+
| BM25 Recall@1 | 54.6% | 91.7% |
|
| 66 |
+
| BM25 Recall@5 | 82.7% | 98.6% |
|
| 67 |
+
|
| 68 |
+
## Load it
|
| 69 |
+
|
| 70 |
+
```python
|
| 71 |
+
from datasets import load_dataset
|
| 72 |
+
|
| 73 |
+
human = load_dataset("Samurai007AK/ChapterEnd", "human", split="test")
|
| 74 |
+
generated = load_dataset("Samurai007AK/ChapterEnd", "generated", split="test")
|
| 75 |
+
corpus = load_dataset("Samurai007AK/ChapterEnd", "corpus", split="corpus")
|
| 76 |
+
```
|
| 77 |
+
|
| 78 |
+
## The task
|
| 79 |
+
|
| 80 |
+
You get a question and the book it comes from. Rank that book's sections so the one that answers the question is first. A section is an OpenStax lesson module, for example "2.1 Describe the Income Statement, Statement of Owner's Equity, Balance Sheet, and Statement of Cash Flows". Books have between 32 and 142 sections, so guessing at random gets 1 to 3% Recall@1.
|
| 81 |
+
|
| 82 |
+
The books are different from SearchTome's, so scores here can't be compared with scores there.
|
| 83 |
+
|
| 84 |
+
## What's inside
|
| 85 |
+
|
| 86 |
+
| File | Rows | |
|
| 87 |
+
|---|---|---|
|
| 88 |
+
| `corpus.jsonl` | 492 | one section per row |
|
| 89 |
+
| `human.jsonl` | 3,568 | questions written by the textbook authors |
|
| 90 |
+
| `generated.jsonl` | 2,604 | questions written by Mistral-7B-Instruct-v0.2 |
|
| 91 |
+
| `score.py` | | Recall@1 and Recall@5 for a run file |
|
| 92 |
+
| `bm25_baseline.py` | | the BM25 baseline, also a template for a run file |
|
| 93 |
+
|
| 94 |
+
**corpus.jsonl**: `module_id` (the OpenStax page id), `book`, `path` (chapter and section title) and `paragraphs` (the section text as a list).
|
| 95 |
+
|
| 96 |
+
**human.jsonl**: `qid`, `book`, `question` (as printed), `question_no_lo` and `lo_tagged` (explained below), `page_type` (which end-of-chapter page it came from, such as "Review Questions") and `gold` (the `module_id` of the correct section).
|
| 97 |
+
|
| 98 |
+
**generated.jsonl**: `qid`, `book`, `question`, `gold`, and `gold_paragraph` (which paragraph of that section the question was written from).
|
| 99 |
+
|
| 100 |
+
| Book | Sections | Human | Generated |
|
| 101 |
+
|---|---|---|---|
|
| 102 |
+
| Principles of Economics 3e | 142 | 1,060 | 356 |
|
| 103 |
+
| Psychology 2e | 88 | 571 | 404 |
|
| 104 |
+
| Principles of Accounting, Volume 1 | 84 | 777 | 417 |
|
| 105 |
+
| Concepts of Biology | 82 | 351 | 517 |
|
| 106 |
+
| Principles of Accounting, Volume 2 | 64 | 665 | 440 |
|
| 107 |
+
| Business Law I Essentials 2e | 32 | 144 | 470 |
|
| 108 |
+
|
| 109 |
+
## Scoring your own method
|
| 110 |
+
|
| 111 |
+
Write a JSONL file with one line per question, best section first. Only rank sections from the question's own book.
|
| 112 |
+
|
| 113 |
+
```
|
| 114 |
+
{"qid": "h0000", "ranking": ["046014ef-296a-4ef2-ae9d-8e58aec02bf3", "..."]}
|
| 115 |
+
```
|
| 116 |
+
|
| 117 |
+
```
|
| 118 |
+
python score.py human.jsonl my_run.jsonl
|
| 119 |
+
python score.py generated.jsonl my_run.jsonl
|
| 120 |
+
```
|
| 121 |
+
|
| 122 |
+
To reproduce the BM25 numbers, run `pip install rank_bm25 numpy` and then `python bm25_baseline.py`.
|
| 123 |
+
|
| 124 |
+
## Two things that fooled me
|
| 125 |
+
|
| 126 |
+
My first results on the human questions were wrong twice, for reasons worth knowing if you build something similar.
|
| 127 |
+
|
| 128 |
+
The first time, every method scored far lower than made sense. The end-of-chapter pages themselves ("Review Questions", "Key Terms", "Summary") were in my corpus, and they contain the questions word for word, so every retriever found the page that quotes the question. BM25 did this for 442 of 450 questions I sampled. The corpus here contains lesson sections only.
|
| 129 |
+
|
| 130 |
+
The second time, one method looked suspiciously good on accounting. 1,441 accounting questions start with a learning-objective tag like "LO 2.1", and that number is the number of the correct section 99.3% of the time. A model that reads the table of contents can match the digits and skip the question. `question_no_lo` has the tag removed and `lo_tagged` marks those questions, and `score.py` reports tagged and untagged questions separately.
|
| 131 |
+
|
| 132 |
+
## Baselines
|
| 133 |
+
|
| 134 |
+
Recall@1 on the human questions. BM25 and the dense retriever score paragraphs, and a section gets the score of its best paragraph. The re-rankers re-order the retrieved candidates.
|
| 135 |
+
|
| 136 |
+
| Method | All | LO-tagged | Untagged |
|
| 137 |
+
|---|---|---|---|
|
| 138 |
+
| Dense retrieval, bge-base-en-v1.5 | 62.2 | 52.2 | 69.0 |
|
| 139 |
+
| bge-reranker-v2-m3, zero-shot | 58.2 | 44.9 | 67.2 |
|
| 140 |
+
| BM25 | 54.6 | 45.6 | 60.7 |
|
| 141 |
+
| bge-reranker-base, zero-shot | 53.0 | 40.6 | 61.4 |
|
| 142 |
+
| bge-reranker-base, fine-tuned on generated questions | 44.3 | 25.8 | 56.8 |
|
| 143 |
+
|
| 144 |
+
The last row surprised me most. Fine-tuning the re-ranker on generated questions improved it on generated questions and made it worse on human ones, from 53.0 to 44.3.
|
| 145 |
+
|
| 146 |
+
## How it was built
|
| 147 |
+
|
| 148 |
+
Text and human questions come from the OpenStax archive API. On each end-of-chapter page the questions are grouped by lesson section, in order, so a question's correct section is the matching section of its chapter. Where the number of groups didn't match the number of sections I skipped the page (149 of 450). I dropped questions shorter than 25 characters and exact duplicates within a book.
|
| 149 |
+
|
| 150 |
+
For the generated set, Mistral-7B-Instruct-v0.2 wrote three questions per paragraph for a random sample of paragraphs from these books. The correct section is the one containing the paragraph. This file is the held-out test part of that set.
|
| 151 |
+
|
| 152 |
+
## What it can't tell you
|
| 153 |
+
|
| 154 |
+
- It covers six books in English. I tried twelve more OpenStax books and left them out because their end-of-chapter pages didn't line up with their sections.
|
| 155 |
+
- The correct section is the one the book files the question under. Sometimes another section answers it too, so Recall@1 is a little harsh on every method.
|
| 156 |
+
- Some human questions aren't search queries. A few point at a figure or table and some ask for an opinion. I kept them as printed.
|
| 157 |
+
- The generated questions come from one model and one prompt. Another question writer may give a smaller or larger gap.
|
| 158 |
+
|
| 159 |
+
## License and credit
|
| 160 |
+
|
| 161 |
+
All text and questions come from [OpenStax](https://openstax.org) textbooks (Rice University), licensed [CC BY-NC-SA 4.0](https://creativecommons.org/licenses/by-nc-sa/4.0/). This dataset carries the same license: credit OpenStax, no commercial use, share changes under the same terms. The two scripts are MIT. OpenStax did not create or endorse this dataset.
|
| 162 |
+
|
| 163 |
+
Books: [Principles of Accounting, Volume 1](https://openstax.org/details/books/principles-financial-accounting) · [Volume 2](https://openstax.org/details/books/principles-managerial-accounting) · [Business Law I Essentials 2e](https://openstax.org/details/books/business-law-i-essentials-2e) · [Concepts of Biology](https://openstax.org/details/books/concepts-biology) · [Principles of Economics 3e](https://openstax.org/details/books/principles-economics-3e) · [Psychology 2e](https://openstax.org/details/books/psychology-2e)
|
| 164 |
+
|
| 165 |
+
## Citation
|
| 166 |
+
|
| 167 |
+
```bibtex
|
| 168 |
+
@misc{konar2026chapterend,
|
| 169 |
+
author = {Arijit Konar},
|
| 170 |
+
title = {ChapterEnd: human-written textbook questions for section retrieval},
|
| 171 |
+
year = {2026},
|
| 172 |
+
url = {https://huggingface.co/datasets/Samurai007AK/ChapterEnd}
|
| 173 |
+
}
|
| 174 |
+
```
|
bm25_baseline.py
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# BM25 over paragraphs, a module scores its best paragraph. Writes run files for every question set.
|
| 2 |
+
# pip install rank_bm25 && python bm25_baseline.py
|
| 3 |
+
import json, re
|
| 4 |
+
from collections import defaultdict
|
| 5 |
+
import numpy as np
|
| 6 |
+
from rank_bm25 import BM25Okapi
|
| 7 |
+
from score import load, score
|
| 8 |
+
tok = lambda s: re.findall(r"[a-z0-9]+", s.lower())
|
| 9 |
+
by_book = defaultdict(lambda: {"ids": [], "owner": [], "text": []})
|
| 10 |
+
for m in load("corpus.jsonl"):
|
| 11 |
+
B = by_book[m["book"]]; B["ids"].append(m["module_id"])
|
| 12 |
+
for p in m["paragraphs"]:
|
| 13 |
+
B["owner"].append(len(B["ids"]) - 1); B["text"].append(p)
|
| 14 |
+
for B in by_book.values():
|
| 15 |
+
B["bm25"], B["owner"] = BM25Okapi([tok(t) for t in B["text"]]), np.array(B["owner"])
|
| 16 |
+
def run(qs, field):
|
| 17 |
+
out = []
|
| 18 |
+
for q in qs:
|
| 19 |
+
B = by_book[q["book"]]; s = B["bm25"].get_scores(tok(q[field]))
|
| 20 |
+
best = np.full(len(B["ids"]), -1e9); np.maximum.at(best, B["owner"], s)
|
| 21 |
+
out.append({"qid": q["qid"], "ranking": [B["ids"][i] for i in np.argsort(-best)[:10]]})
|
| 22 |
+
return out
|
| 23 |
+
human, gen = load("human.jsonl"), load("generated.jsonl")
|
| 24 |
+
for name, qs, field in [("human", human, "question"), ("human, LO tags stripped", human, "question_no_lo"),
|
| 25 |
+
("generated", gen, "question")]:
|
| 26 |
+
r = run(qs, field)
|
| 27 |
+
with open(f"run_bm25_{name.split(',')[0]}{'_nolo' if 'LO' in name else ''}.jsonl", "w") as f:
|
| 28 |
+
f.writelines(json.dumps(x) + "\n" for x in r)
|
| 29 |
+
print(name, json.dumps(score(qs, r)))
|
corpus.jsonl
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
generated.jsonl
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
human.jsonl
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
score.py
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Score a run file against human.jsonl or generated.jsonl.
|
| 2 |
+
# A run file is JSONL, one line per question: {"qid": "h0000", "ranking": ["<module_id>", ...]} (best first).
|
| 3 |
+
# Rank only modules from the question's own book (corpus.jsonl). Questions missing from the run count as wrong.
|
| 4 |
+
# python score.py human.jsonl run.jsonl
|
| 5 |
+
import json, sys
|
| 6 |
+
def load(p):
|
| 7 |
+
return [json.loads(l) for l in open(p, encoding="utf-8")]
|
| 8 |
+
def score(qs, runs):
|
| 9 |
+
rank = {r["qid"]: r["ranking"] for r in runs}
|
| 10 |
+
hit = lambda q, k: q["gold"] in rank.get(q["qid"], [])[:k]
|
| 11 |
+
out = {}
|
| 12 |
+
groups = {"all": qs}
|
| 13 |
+
if "lo_tagged" in qs[0]:
|
| 14 |
+
groups |= {"LO-tagged": [q for q in qs if q["lo_tagged"]], "untagged": [q for q in qs if not q["lo_tagged"]]}
|
| 15 |
+
for name, g in groups.items():
|
| 16 |
+
out[name] = {"n": len(g), "R@1": round(100 * sum(hit(q, 1) for q in g) / len(g), 1),
|
| 17 |
+
"R@5": round(100 * sum(hit(q, 5) for q in g) / len(g), 1)}
|
| 18 |
+
return out
|
| 19 |
+
if __name__ == "__main__":
|
| 20 |
+
for k, v in score(load(sys.argv[1]), load(sys.argv[2])).items():
|
| 21 |
+
print(f"{k:10} n={v['n']:5} R@1 {v['R@1']:5.1f} R@5 {v['R@5']:5.1f}")
|