| import json |
|
|
| import datasets |
| import pandas as pd |
|
|
| class ChemblSMConfig(datasets.BuilderConfig): |
|
|
| def __init__(self, features, data_url, **kwargs): |
| super(ChemblSMConfig, self).__init__(**kwargs) |
| self.features = features |
| self.data_url = data_url |
|
|
| class ChemblSM(datasets.GeneratorBasedBuilder): |
|
|
| BUILDER_CONFIGS = [ |
| ChemblSMConfig( |
| name="pairs", |
| features={ |
| "column1": datasets.Value("string"), |
| "column2": datasets.Value("string"), |
| "label" : datasets.Value("bool") |
| }, |
| data_url="https://huggingface.co/datasets/matchbench/ChEMBL-SM/resolve/main/matches.txt" |
| ), |
| ChemblSMConfig( |
| name="source", |
| features={ |
| "json": datasets.Value("string") |
| }, |
| data_url="https://huggingface.co/datasets/matchbench/ChEMBL-SM/resolve/main/chembl-sm-1.csv" |
| ), |
| ChemblSMConfig( |
| name="target", |
| features={ |
| "json": datasets.Value("string") |
| }, |
| data_url="https://huggingface.co/datasets/matchbench/ChEMBL-SM/resolve/main/chembl-sm-2.csv" |
| ), |
| ] |
|
|
| def _info(self): |
| return datasets.DatasetInfo( |
| features=datasets.Features(self.config.features) |
| ) |
|
|
| def _split_generators(self, dl_manager): |
| path = dl_manager.download(self.config.data_url) |
| if self.config.name == "pairs": |
| return [ |
| datasets.SplitGenerator( |
| name=split, |
| gen_kwargs={ |
| "file_path": path |
| } |
| ) |
| for split in [ |
| datasets.Split.TEST |
| ] |
| ] |
| elif self.config.name == "source": |
| return [ |
| datasets.SplitGenerator( |
| name="source", |
| gen_kwargs={ |
| "file_path": path |
| } |
| ) |
| ] |
| elif self.config.name == "target": |
| return [ |
| datasets.SplitGenerator( |
| name="target", |
| gen_kwargs={ |
| "file_path": path |
| } |
| ) |
| ] |
|
|
| def _generate_examples(self, file_path): |
| if self.config.name == "pairs": |
| with open(file_path, "r") as f: |
| md = {} |
| for _, line in enumerate(f): |
| t = line.strip().split(',') |
| if t[0] not in md: |
| md[t[0]] = {t[1]} |
| else: |
| md[t[0]].add(t[1]) |
| id = -1 |
| for k, v in md.items(): |
| for i in v: |
| id = id + 1 |
| yield id, {"column1": k, "column2": i, "label": True} |
| elif self.config.name == "source": |
| with open(file_path, "r") as f: |
| source = pd.read_csv(file_path) |
| source = source.to_json() |
| yield 0, {"json": source} |
| elif self.config.name == "target": |
| with open(file_path, "r") as f: |
| target = pd.read_csv(file_path) |
| target = target.to_json() |
| yield 0, {"json": target} |