Include reproducible source conversion
Browse files
repackage_dataset/dialogue.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Small original-source conversions that avoid audio downloads or legacy loaders."""
|
| 2 |
+
from collections import Counter
|
| 3 |
+
import hashlib
|
| 4 |
+
import io
|
| 5 |
+
import json
|
| 6 |
+
from pathlib import Path
|
| 7 |
+
import urllib.request
|
| 8 |
+
import xml.etree.ElementTree as ET
|
| 9 |
+
import zipfile
|
| 10 |
+
|
| 11 |
+
from datasets import Dataset, DatasetDict
|
| 12 |
+
|
| 13 |
+
from .relevance import text_key, write_mirror
|
| 14 |
+
|
| 15 |
+
SLURP_REVISION = '8eb16545762be97ace75334109d73824217311f1'
|
| 16 |
+
MCSCRIPT_URL = 'https://fedora.clarin-d.uni-saarland.de/sfb1102/MCScript-2.0.zip'
|
| 17 |
+
MCSCRIPT_SHA256 = '50037b08123a4660e2e00ad07bcfa2a7ddc1d666b05b198d359cd2cda8f5e88c'
|
| 18 |
+
|
| 19 |
+
|
| 20 |
+
def slurp():
|
| 21 |
+
"""SLURP original human transcripts and intent labels, without bundled audio.
|
| 22 |
+
|
| 23 |
+
Native real train/devel/test partitions; synthetic speech is not included.
|
| 24 |
+
Identical transcripts with inconsistent intents are excluded, and duplicate
|
| 25 |
+
transcripts stay in their highest-priority native holdout (test, dev, train).
|
| 26 |
+
One- and two-word transcripts are excluded as insufficient intent context.
|
| 27 |
+
Original terms: CC BY 4.0. All removals and source hashes are recorded.
|
| 28 |
+
"""
|
| 29 |
+
splits={}; hashes={}; labels={};counts=Counter()
|
| 30 |
+
for split,native in [('train','train'),('validation','devel'),('test','test')]:
|
| 31 |
+
url=f'https://raw.githubusercontent.com/pswietojanski/slurp/{SLURP_REVISION}/dataset/slurp/{native}.jsonl'
|
| 32 |
+
raw=urllib.request.urlopen(url).read();hashes[native]=hashlib.sha256(raw).hexdigest()
|
| 33 |
+
splits[split]=[json.loads(line) for line in raw.splitlines() if line.strip()]
|
| 34 |
+
for row in splits[split]:labels.setdefault(text_key(row['sentence']),set()).add(row['intent'])
|
| 35 |
+
output={};seen=set()
|
| 36 |
+
for split in ('test','validation','train'):
|
| 37 |
+
output[split]=[]
|
| 38 |
+
for row in splits[split]:
|
| 39 |
+
key=text_key(row['sentence'])
|
| 40 |
+
reason='ambiguous_intent' if len(labels[key])!=1 else 'short_transcript' if len(row['sentence'].split())<3 else 'duplicate_transcript' if key in seen else None
|
| 41 |
+
if reason:counts[split+'/'+reason]+=1;continue
|
| 42 |
+
seen.add(key)
|
| 43 |
+
meta=dict(id=str(row['slurp_id']),split_group_id=key,scenario=row['scenario'],entities=row['entities'],source_revision=SLURP_REVISION)
|
| 44 |
+
output[split].append(dict(sentence1=row['sentence'],sentence2='',labels=row['intent'],metadata=json.dumps(meta,sort_keys=True)))
|
| 45 |
+
return write_mirror('slurp',output,'string',dict(source='https://github.com/pswietojanski/slurp',revision=SLURP_REVISION,
|
| 46 |
+
source_hashes=hashes,excluded=dict(counts),split_policy='native splits; exact transcript conflicts and overlap removed',
|
| 47 |
+
conversion_sha256=hashlib.sha256(Path(__file__).read_bytes()).hexdigest(),license='cc-by-4.0'))
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
def mcscript2():
|
| 51 |
+
"""MCScript2.0 native two-answer reading comprehension, original XML partitions.
|
| 52 |
+
|
| 53 |
+
Preserve stories, questions and native choices; exactly one gold answer and
|
| 54 |
+
two distinct non-empty options are required. Story hashes prevent train/dev/
|
| 55 |
+
test copies. Source URL/archive hash and per-split counts are recorded.
|
| 56 |
+
Dataset terms: unspecified until original distribution terms are verified.
|
| 57 |
+
"""
|
| 58 |
+
raw=urllib.request.urlopen(MCSCRIPT_URL).read()
|
| 59 |
+
if hashlib.sha256(raw).hexdigest()!=MCSCRIPT_SHA256:raise ValueError('MCScript archive changed')
|
| 60 |
+
output={};groups={};counts=Counter()
|
| 61 |
+
with zipfile.ZipFile(io.BytesIO(raw)) as archive:
|
| 62 |
+
for split,native in [('train','train'),('validation','dev'),('test','test')]:
|
| 63 |
+
output[split]=[];groups[split]=set()
|
| 64 |
+
for story in ET.fromstring(archive.read(native+'-data.xml')):
|
| 65 |
+
text=story.findtext('text');group=text_key(text);groups[split].add(group)
|
| 66 |
+
for question in story.findall('./questions/question'):
|
| 67 |
+
choices=[a.attrib['text'] for a in question.findall('answer')]
|
| 68 |
+
gold=[i for i,a in enumerate(question.findall('answer')) if a.attrib['correct'].lower()=='true']
|
| 69 |
+
if len(choices)!=2 or len(set(choices))!=2 or not all(c.strip() for c in choices) or len(gold)!=1:
|
| 70 |
+
counts[split+'/invalid_choices']+=1;continue
|
| 71 |
+
meta=dict(id=story.attrib['id']+':'+question.attrib['id'],split_group_id=group,
|
| 72 |
+
scenario=story.attrib['scenario'],question_type=question.attrib['type'])
|
| 73 |
+
output[split].append(dict(inputs=f"Story: {text}\nQuestion: {question.attrib['text']}",
|
| 74 |
+
choices_list=choices,labels=gold[0],metadata=json.dumps(meta,sort_keys=True)))
|
| 75 |
+
assert all(not groups[a]&groups[b] for a in groups for b in groups if a!=b),'MCScript story overlap'
|
| 76 |
+
data=DatasetDict({s:Dataset.from_list(r) for s,r in output.items()})
|
| 77 |
+
directory=Path('build/mcscript2-release');directory.mkdir(parents=True,exist_ok=True)
|
| 78 |
+
for split,rows in data.items():rows.to_parquet(str(directory/(split+'.parquet')))
|
| 79 |
+
provenance=dict(source_url=MCSCRIPT_URL,archive_sha256=MCSCRIPT_SHA256,excluded=dict(counts),
|
| 80 |
+
splits={s:len(r) for s,r in data.items()},split_policy='native story partitions',
|
| 81 |
+
conversion_sha256=hashlib.sha256(Path(__file__).read_bytes()).hexdigest(),license='unspecified')
|
| 82 |
+
(directory/'provenance.json').write_text(json.dumps(provenance,indent=2)+'\n')
|
| 83 |
+
(directory/'excluded-questions.jsonl').write_text('')
|
| 84 |
+
return data
|