policies / policies.py
vosadcii's picture
regenerated readme
a1d99b9
raw
history blame
3.47 kB
import json
import os
import datasets
from datasets.tasks import QuestionAnsweringExtractive
logger = datasets.logging.get_logger(__name__)
_CITATION = """"""
_DESCRIPTION = """\
Manually generated dataset for policies qa
"""
_URLS = {
"train": "./data/train.json",
"test": "./data/test.json"
}
class PoliciesQAConfig(datasets.BuilderConfig):
"""BuilderConfig for Ineract Policies."""
def __init__(self, **kwargs):
"""BuilderConfig for Ineract Policies.
Args:
**kwargs: keyword arguments forwarded to super.
"""
super(PoliciesQAConfig, self).__init__(**kwargs)
class PoliciesQA(datasets.GeneratorBasedBuilder):
"""Ineract Policies: The Policy Question Answering Dataset. Version 0.1"""
BUILDER_CONFIGS = [
PoliciesQAConfig(
name="plain_text",
version=datasets.Version("1.0.0", ""),
description="Plain text",
),
]
DEFAULT_CONFIG_NAME = "plain_text"
def _info(self):
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=datasets.Features(
{
"context": datasets.Value("string"),
"question": datasets.Value("string"),
"answers": datasets.features.Sequence(
{
"text": datasets.Value("string"),
"answer_start": datasets.Value("int32"),
}
),
}
),
# No default supervised_keys (as we have to pass both question
# and context as input).
supervised_keys=None,
homepage="ineract.com",
task_templates=[
QuestionAnsweringExtractive(
question_column="question", context_column="context", answers_column="answers"
)
],
citation=_CITATION,
)
def _split_generators(self, dl_manager):
downloaded_files = dl_manager.download_and_extract(_URLS)
return [
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={
"filepath": downloaded_files["train"], "split": "train"}),
datasets.SplitGenerator(name=datasets.Split.TEST, gen_kwargs={
"filepath": downloaded_files["test"], "split": "test"})
]
def _generate_examples(self, filepath, split):
print(os.fspath(filepath))
"""This function returns the examples in the raw (text) form."""
logger.info("generating examples from = %s", filepath)
key = 0
with open(filepath, encoding="utf-8") as f:
policies = json.load(f)
for policy in policies["data"]:
context = policy["context"]
question = policy["question"]
answer_starts = [answer_start
for answer_start in policy["answers"]["answer_start"]]
answers = [
answer_text for answer_text in policy["answers"]["text"]]
yield key, {
"context": context,
"question": question,
"answers": {
"answer_start": answer_starts,
"text": answers,
},
}
key += 1