File size: 4,935 Bytes
648963d
 
 
 
 
 
 
 
 
 
 
 
afecc1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
648963d
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
---
license: mit
task_categories:
- text-classification
- text-generation
language:
- en
size_categories:
- 10K<n<100K
---


# UKS Seed Dataset Generator

Generates preprocessed training data for the UKS Energy System from the FineWeb dataset.

## Overview

This pipeline processes text from HuggingFace's FineWeb dataset and produces two output formats:

1. **pre_tokenized.jsonl.gz**: Ready for direct model training
2. **intermediate.parquet**: For exploration and custom tokenization workflows

## Dependencies

```bash
pip install spacy transformers datasets tqdm
python -m spacy download en_core_web_sm
pip install pyarrow  # for parquet output
```

## Tokenizer

### XLNetTokenizer

Preprocessed data uses **[XLNetTokenizer](https://huggingface.co/xlnet/xlnet-base-cased)** for subword tokenization.

**Model**: `xlnet/xlnet-base-cased`  
**Vocab size**: 32,000 tokens  
**Features**:
- SentencePiece subword tokenization
- Punctuation-aware splitting
- Multilingual support built-in

**Example tokenization**:
```python
from transformers import XLNetTokenizer

tokenizer = XLNetTokenizer.from_pretrained("xlnet/xlnet-base-cased")
text = "Don't forget the meeting at 5pm."

ids = tokenizer.encode(text, add_special_tokens=False)
tokens = tokenizer.convert_ids_to_tokens(ids)
# tokens: ["Don", "'", "t", "forget", "the", "meeting", "at", "5", "pm", "."]
# ids:   [13645, 9, 24, 12087, 5, 4857, 19, 821, 131, 10]
```

### spaCy Features

Linguistic annotations are extracted using **spaCy** (`en_core_web_sm`):

| Feature | Description |
|---------|-------------|
| `pos` | Part-of-speech tags (NOUN, VERB, ADJ, etc.) |
| `dep` | Dependency relations (nsubj, dobj, prep, etc.) |
| `lemma` | Lemmatized forms |
| `ent_type` | Named entity types (PERSON, ORG, DATE, etc.) |
| `head_dist` | Distance to head token in dependency tree |

## Output Schema

Both output files contain the following columns:

| Column | Type | Description | Example |
|--------|------|-------------|---------|
| `id` | string | SHA256 hash (first 16 chars) | `"a1b2c3d4e5f6g7h8"` |
| `text` | string | Original sentence | `"The cat sat on the mat."` |
| `tokens` | string (JSON list) | XLNet token strings | `["The", "cat", "sat", ...]` |
| `token_ids` | string (JSON list) | XLNet integer IDs | `[44, 245, 1234, ...]` |
| `pos` | string (JSON list) | spaCy POS tags | `["DET", "NOUN", "VERB", ...]` |
| `dep` | string (JSON list) | Dependency labels | `["det", "nsubj", "ROOT", ...]` |
| `lemma` | string (JSON list) | Lemmas | `["the", "cat", "sit", ...]` |
| `ent_type` | string (JSON list) | Entity types | `["O", "O", "O", "DATE"]` |
| `head_dist` | string (JSON list) | Head distances | `[1, -2, 0, ...]` |

### JSON List Format

All list columns are stored as JSON strings for compatibility:

```python
import json

# Loading tokens from file
record = json.loads(line)
tokens = json.loads(record["tokens"])  # ["The", "cat", "sat", ...]
```

## Output Files

### pre_tokenized.jsonl.gz

Gzipped JSON Lines format for direct training consumption:

```python
import gzip
import json

with gzip.open("output/pre_tokenized.jsonl.gz", "rt") as f:
    for line in f:
        record = json.loads(line)
        # Use directly in training
```

### intermediate.parquet

Apache Parquet for data exploration:

```python
import pyarrow.parquet as pq

table = pq.read_table("output/intermediate.parquet")
df = table.to_pandas()
# Explore with pandas
```

## Usage

### Basic Generation

```bash
cd hf_dataset
python generate.py
```

### Custom Options

```bash
python generate.py \
    --max-samples 50000 \
    --batch-size 200 \
    --output-dir ./my_output \
    --cache-dir ./my_cache
```

### Python API

```python
from generate import SEEDDatasetGenerator, SEEDConfig

config = SEEDConfig(
    max_samples=10000,
    batch_size=100,
    output_dir=Path("output"),
)

generator = SEEDDatasetGenerator(config)
paths = generator.generate()

print(f"Generated: {paths}")
```

## Configuration

| Parameter | Default | Description |
|-----------|---------|-------------|
| `max_samples` | 10,000 | Maximum sentences to process |
| `batch_size` | 100 | Documents per spaCy batch |
| `min_sentence_len` | 5 | Minimum sentence characters |
| `max_sentence_len` | 512 | Maximum sentence characters |
| `output_dir` | `hf_dataset/output` | Output directory |
| `cache_dir` | `hf_dataset/.cache` | Cache directory |
| `hf_dataset_name` | `HuggingFaceFW/fineweb` | Source dataset |
| `spacy_model` | `en_core_web_sm` | spaCy model name |
| `xlnet_model` | `xlnet/xlnet-base-cased` | HuggingFace model path |

## Cache

The generator tracks processed samples in `cache_dir/processed_ids.json` to avoid duplicates and enable resuming interrupted runs.

## Notes

- Sentences shorter than 5 characters are skipped
- Sentences longer than 512 characters are truncated
- Duplicates are detected via SHA256 hash
- Processsing is streamed from HuggingFace (memory efficient)