Update codeforces_question_solution_label.py
Browse files123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657import datasetsimport osimport jsonclass CodeforcesQuestionSolutionLabel(datasets.GeneratorBasedBuilder):"""Codeforces Question Solution Label Dataset."""# Define supported configurations (language subsets)BUILDER_CONFIGS = [datasets.BuilderConfig(name="cpp", description="C++ solutions for Codeforces problems"),datasets.BuilderConfig(name="py", description="Python solutions for Codeforces problems"),datasets.BuilderConfig(name="java", description="Java solutions for Codeforces problems"),]def _info(self):# Define dataset features (fields and types)return datasets.DatasetInfo(description="A dataset of Codeforces problem solutions labeled by language",features=datasets.Features({"contest_id": datasets.Value("string"),"problem_id": datasets.Value("string"),"statement": datasets.Value("string"),"tags": datasets.Sequence(datasets.Value("string")),"code": datasets.Value("string"),"language": datasets.Value("string"),}),homepage="https://codeforces.com/",citation="Data collected from Codeforces API",)def _split_generators(self, dl_manager):# Get the current configuration (language)config = self.config.namedata_dir = os.path.join("data", config) # Data files are in data/<language>/# Define generators for each splitreturn [datasets.SplitGenerator(name=datasets.Split.TRAIN,gen_kwargs={"filepath": os.path.join(data_dir, "train.jsonl")},),datasets.SplitGenerator(name=datasets.Split.VALIDATION,gen_kwargs={"filepath": os.path.join(data_dir, "validation.jsonl")},),datasets.SplitGenerator(name=datasets.Split.TEST,gen_kwargs={"filepath": os.path.join(data_dir, "test.jsonl")},),]def _generate_examples(self, filepath):# Generate examples from the filewith open(filepath, encoding="utf-8") as f:for key, row in enumerate(f):data = json.loads(row)yield key, data123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171# coding=utf-8# Copyright 2023 The HuggingFace Datasets Authors.## Licensed under the Apache License, Version 2.0 (the "License");# you may not use this file except in compliance with the License.# You may obtain a copy of the License at## http://www.apache.org/licenses/LICENSE-2.0## Unless required by applicable law or agreed to in writing, software# distributed under the License is distributed on an "AS IS" BASIS,# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.# See the License for the specific language governing permissions and# limitations under the License.# Lint as: python3"""Codeforces Question Solution Label Dataset."""import jsonimport osimport textwrapimport datasetsclass CodeforcesConfig(datasets.BuilderConfig):"""BuilderConfig for Codeforces Question Solution Label."""def __init__(self, homepage: str = None, citation: str = None, base_path: str = None, paths: dict = None, **kwargs):"""BuilderConfig for Codeforces Question Solution Label.Args:homepage (`str`): Homepage.citation (`str`): Citation reference.base_path (`str`): Base path for data files.paths (`dict`): Data file paths relative to base_path.**kwargs: keyword arguments forwarded to super."""super().__init__(**kwargs)self.homepage = homepageself.citation = citationself.base_path = base_pathself.paths = pathsclass CodeforcesQuestionSolutionLabel(datasets.GeneratorBasedBuilder):"""Codeforces Question Solution Label Dataset."""BUILDER_CONFIGS = [CodeforcesConfig(name="cpp",version=datasets.Version("1.0.0"),description=textwrap.dedent("Codeforces Question Solution Label Dataset (C++):\n""A dataset containing solutions to Codeforces problems written in C++. Each entry includes the contest ID, ""problem ID, problem statement, tags, solution code, and programming language."),homepage="https://codeforces.com/",citation=textwrap.dedent("""\@misc{codeforces_solutions,author = {AlbertHatsuki},title = {Codeforces Question Solution Label Dataset},year = {2023},publisher = {Hugging Face},howpublished = {\\url{https://huggingface.co/datasets/AlbertHatsuki/codeforces-question-solution-label}}}"""),base_path="data/cpp",paths={datasets.Split.TRAIN: "train.jsonl",datasets.Split.VALIDATION: "validation.jsonl",datasets.Split.TEST: "test.jsonl",},),CodeforcesConfig(name="py",version=datasets.Version("1.0.0"),description=textwrap.dedent("Codeforces Question Solution Label Dataset (Python):\n""A dataset containing solutions to Codeforces problems written in Python. Each entry includes the contest ID, ""problem ID, problem statement, tags, solution code, and programming language."),homepage="https://codeforces.com/",citation=textwrap.dedent("""\@misc{codeforces_solutions,author = {AlbertHatsuki},title = {Codeforces Question Solution Label Dataset},year = {2023},publisher = {Hugging Face},howpublished = {\\url{https://huggingface.co/datasets/AlbertHatsuki/codeforces-question-solution-label}}}"""),base_path="data/py",paths={datasets.Split.TRAIN: "train.jsonl",datasets.Split.VALIDATION: "validation.jsonl",datasets.Split.TEST: "test.jsonl",},),CodeforcesConfig(name="java",version=datasets.Version("1.0.0"),description=textwrap.dedent("Codeforces Question Solution Label Dataset (Java):\n""A dataset containing solutions to Codeforces problems written in Java. Each entry includes the contest ID, ""problem ID, problem statement, tags, solution code, and programming language."),homepage="https://codeforces.com/",citation=textwrap.dedent("""\@misc{codeforces_solutions,author = {AlbertHatsuki},title = {Codeforces Question Solution Label Dataset},year = {2023},publisher = {Hugging Face},howpublished = {\\url{https://huggingface.co/datasets/AlbertHatsuki/codeforces-question-solution-label}}}"""),base_path="data/java",paths={datasets.Split.TRAIN: "train.jsonl",datasets.Split.VALIDATION: "validation.jsonl",datasets.Split.TEST: "test.jsonl",},),]def _info(self):features = {"contest_id": datasets.Value("string"),"problem_id": datasets.Value("string"),"statement": datasets.Value("string"),"tags": datasets.Sequence(datasets.Value("string")),"code": datasets.Value("string"),"language": datasets.Value("string"),}return datasets.DatasetInfo(description=self.config.description,features=datasets.Features(features),homepage=self.config.homepage,citation=self.config.citation,)def _split_generators(self, dl_manager):"""Returns SplitGenerators."""dl_paths = {split: os.path.join(self.config.base_path, path)for split, path in self.config.paths.items()}return [datasets.SplitGenerator(name=split,gen_kwargs={"filepath": dl_paths[split]},)for split in dl_paths.keys()]def _generate_examples(self, filepath):"""Yields examples."""with open(filepath, encoding="utf-8") as f:for row_id, row in enumerate(f):data = json.loads(row)yield row_id, {"contest_id": data["contest_id"],"problem_id": data["problem_id"],"statement": data["statement"],"tags": data["tags"],"code": data["code"],"language": data["language"],}
codeforces_question_solution_label.py+151-37