Update codeforces_question_solution_label.py
Browse files12123456789101112131415161718192021222324252627282930313233343536373839404142434445464784810111249505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812916130131132133134135136137138181920212223242526272813914014114214314414532333435361461471481491501511523940414243444546474849153154155156157158159531601615516216357164165166167168169170171import datasetsimport os# 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 textwrap
import datasets
class 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 = homepage self.citation = citation self.base_path = base_path self.paths = paths
class 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"), 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): # Define dataset features (fields and types) 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="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", description=self.config.description, features=datasets.Features(features), homepage=self.config.homepage, citation=self.config.citation, )
def _split_generators(self, dl_manager): # Get the current configuration (language) config = self.config.name data_dir = os.path.join("data", config) # Data files are in data/<language>/
# Define generators for each split """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=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")}, ), name=split, gen_kwargs={"filepath": dl_paths[split]}, ) for split in dl_paths.keys() ]
def _generate_examples(self, filepath): # Generate examples from the file """Yields examples.""" with open(filepath, encoding="utf-8") as f: for key, row in enumerate(f): for row_id, row in enumerate(f): data = json.loads(row) yield key, data 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