File size: 2,697 Bytes
a555f47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7af3a7b
 
a555f47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import os
import json
import datasets

_DESCRIPTION = """
TableSense Dataset - Spreadsheet Table Detection task from the VEnron2, VEUSES, and VFUSE datasets.
"""

VERSION = datasets.Version("1.0.0")

def parse_range(range_str):
    start, end = range_str.split(':')
    
    start_col = ''.join(c for c in start if c.isalpha())
    start_row = int(''.join(c for c in start if c.isdigit()))
    
    end_col = ''.join(c for c in end if c.isalpha())
    end_row = int(''.join(c for c in end if c.isdigit()))
    
    return {"start_col": start_col, "start_row": start_row, "end_col": end_col, "end_row": end_row}


class Tablesense(datasets.GeneratorBasedBuilder):
    """Dataset for loading arbitrary .xlsx files with a .jsonl metadata file."""

    def _info(self):
        features = {
            "filepath": datasets.Value("string"),
            'sheet_name': datasets.Value("string"),
            'split': datasets.Value("string"),
            'table_regions':[
                {
                    "start_col": datasets.Value("string"),
                    "start_row": datasets.Value("int32"),
                    "end_col": datasets.Value("string"),
                    "end_row": datasets.Value("int32")
                }
            ]
        }

        return datasets.DatasetInfo(
            description=_DESCRIPTION,
            features=datasets.Features(features)
        )
    
    def _split_generators(self, dl_manager):
        """Define splits."""
        # Load your dataset folder
        data_path = "data.tar.gz"
        xlsx_dir = os.path.join(dl_manager.download_and_extract(data_path), "data")
        metadata_file = "annotations.jsonl"
        return [
            datasets.SplitGenerator(
                name="train",
                gen_kwargs={
                    "files_dir": xlsx_dir,
                    "metadata_file": metadata_file,
                },
            )
        ]

    def _generate_examples(self, files_dir, metadata_file):
        """Yields examples."""
        # Load metadata from the .jsonl file
        with open(metadata_file, "r") as f:
            metadata = [json.loads(line.strip()) for line in f]
        
        for i, meta in enumerate(metadata):
            file_name = meta.get("clean_file")

            # Load corresponding .xlsx file
            xlsx_path = os.path.join(files_dir, file_name)
            if not os.path.exists(xlsx_path):
                continue
            yield i, {
                "filepath": xlsx_path,
                "sheet_name": meta.get("sheet_name"),
                "split": meta.get("split"),
                "table_regions": [parse_range(rg) for rg in meta.get("table_regions")]
            }