UniTok 3.5.1__tar.gz → 4.3.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- UniTok-4.3.2/LICENSE +21 -0
- UniTok-4.3.2/PKG-INFO +228 -0
- UniTok-4.3.2/README.md +213 -0
- UniTok-4.3.2/UniTok.egg-info/PKG-INFO +228 -0
- UniTok-4.3.2/UniTok.egg-info/SOURCES.txt +70 -0
- UniTok-4.3.2/UniTok.egg-info/entry_points.txt +5 -0
- {UniTok-3.5.1 → UniTok-4.3.2}/UniTok.egg-info/requires.txt +2 -0
- UniTok-4.3.2/UniTok.egg-info/top_level.txt +2 -0
- UniTok-4.3.2/UniTokv3/__main__.py +169 -0
- {UniTok-3.5.1/UniTok → UniTok-4.3.2/UniTokv3}/tok/bert_tok.py +1 -1
- {UniTok-3.5.1/UniTok → UniTok-4.3.2/UniTokv3}/tok/ent_tok.py +1 -1
- {UniTok-3.5.1/UniTok → UniTok-4.3.2/UniTokv3}/tok/id_tok.py +1 -1
- {UniTok-3.5.1/UniTok → UniTok-4.3.2/UniTokv3}/tok/number_tok.py +1 -1
- {UniTok-3.5.1/UniTok → UniTok-4.3.2/UniTokv3}/tok/seq_tok.py +1 -1
- {UniTok-3.5.1/UniTok → UniTok-4.3.2/UniTokv3}/tok/split_tok.py +1 -1
- {UniTok-3.5.1/UniTok → UniTok-4.3.2/UniTokv3}/tok/tok.py +1 -1
- {UniTok-3.5.1/UniTok → UniTok-4.3.2/UniTokv3}/unidep.py +11 -2
- {UniTok-3.5.1/UniTok → UniTok-4.3.2/UniTokv3}/unitok.py +12 -3
- {UniTok-3.5.1 → UniTok-4.3.2}/setup.py +9 -5
- UniTok-4.3.2/unitok/__init__.py +39 -0
- UniTok-4.3.2/unitok/__main__.py +108 -0
- UniTok-4.3.2/unitok/job.py +94 -0
- UniTok-4.3.2/unitok/meta.py +137 -0
- UniTok-4.3.2/unitok/selector.py +29 -0
- UniTok-4.3.2/unitok/status.py +44 -0
- UniTok-4.3.2/unitok/tokenizer/__init__.py +20 -0
- UniTok-4.3.2/unitok/tokenizer/base_tokenizer.py +89 -0
- UniTok-4.3.2/unitok/tokenizer/digit_tokenizer.py +33 -0
- UniTok-4.3.2/unitok/tokenizer/entity_tokenizer.py +13 -0
- UniTok-4.3.2/unitok/tokenizer/glove_tokenizer.py +21 -0
- UniTok-4.3.2/unitok/tokenizer/split_tokenizer.py +14 -0
- UniTok-4.3.2/unitok/tokenizer/transformers_tokenizer.py +65 -0
- UniTok-4.3.2/unitok/tokenizer/union_tokenizer.py +17 -0
- UniTok-4.3.2/unitok/tokenizer/unknown_tokenizer.py +35 -0
- UniTok-4.3.2/unitok/unitok.py +489 -0
- UniTok-4.3.2/unitok/utils/__init__.py +21 -0
- UniTok-4.3.2/unitok/utils/class_pool.py +107 -0
- UniTok-4.3.2/unitok/utils/data.py +15 -0
- UniTok-4.3.2/unitok/utils/function.py +6 -0
- UniTok-4.3.2/unitok/utils/handler/__init__.py +7 -0
- UniTok-4.3.2/unitok/utils/handler/json_handler.py +28 -0
- UniTok-4.3.2/unitok/utils/handler/pkl_handler.py +19 -0
- UniTok-4.3.2/unitok/utils/hub/__init__.py +4 -0
- UniTok-4.3.2/unitok/utils/hub/hub.py +44 -0
- UniTok-4.3.2/unitok/utils/hub/param_hub.py +6 -0
- UniTok-4.3.2/unitok/utils/index_set/__init__.py +15 -0
- UniTok-4.3.2/unitok/utils/index_set/index_set.py +71 -0
- UniTok-4.3.2/unitok/utils/index_set/job_set.py +25 -0
- UniTok-4.3.2/unitok/utils/index_set/tokenizer_set.py +19 -0
- UniTok-4.3.2/unitok/utils/index_set/vocabulary_set.py +19 -0
- UniTok-4.3.2/unitok/utils/instance.py +18 -0
- UniTok-4.3.2/unitok/utils/map.py +3 -0
- UniTok-4.3.2/unitok/utils/space.py +35 -0
- UniTok-4.3.2/unitok/utils/symbol.py +23 -0
- UniTok-4.3.2/unitok/utils/verbose.py +48 -0
- UniTok-4.3.2/unitok/vocabulary/__init__.py +11 -0
- UniTok-4.3.2/unitok/vocabulary/counter.py +85 -0
- UniTok-4.3.2/unitok/vocabulary/vocabulary.py +166 -0
- UniTok-3.5.1/PKG-INFO +0 -199
- UniTok-3.5.1/README.md +0 -185
- UniTok-3.5.1/UniTok/__main__.py +0 -14
- UniTok-3.5.1/UniTok.egg-info/PKG-INFO +0 -199
- UniTok-3.5.1/UniTok.egg-info/SOURCES.txt +0 -30
- UniTok-3.5.1/UniTok.egg-info/entry_points.txt +0 -3
- UniTok-3.5.1/UniTok.egg-info/top_level.txt +0 -1
- {UniTok-3.5.1 → UniTok-4.3.2}/UniTok.egg-info/dependency_links.txt +0 -0
- {UniTok-3.5.1/UniTok → UniTok-4.3.2/UniTokv3}/__init__.py +0 -0
- {UniTok-3.5.1/UniTok → UniTok-4.3.2/UniTokv3}/analysis/__init__.py +0 -0
- {UniTok-3.5.1/UniTok → UniTok-4.3.2/UniTokv3}/analysis/lengths.py +0 -0
- {UniTok-3.5.1/UniTok → UniTok-4.3.2/UniTokv3}/analysis/plot.py +0 -0
- {UniTok-3.5.1/UniTok → UniTok-4.3.2/UniTokv3}/cols.py +0 -0
- {UniTok-3.5.1/UniTok → UniTok-4.3.2/UniTokv3}/column.py +0 -0
- {UniTok-3.5.1/UniTok → UniTok-4.3.2/UniTokv3}/fut.py +0 -0
- {UniTok-3.5.1/UniTok → UniTok-4.3.2/UniTokv3}/global_setting.py +0 -0
- {UniTok-3.5.1/UniTok → UniTok-4.3.2/UniTokv3}/meta.py +0 -0
- {UniTok-3.5.1/UniTok → UniTok-4.3.2/UniTokv3}/tok/__init__.py +0 -0
- {UniTok-3.5.1/UniTok → UniTok-4.3.2/UniTokv3}/vocab.py +0 -0
- {UniTok-3.5.1/UniTok → UniTok-4.3.2/UniTokv3}/vocabs.py +0 -0
- {UniTok-3.5.1 → UniTok-4.3.2}/setup.cfg +0 -0
UniTok-4.3.2/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
MIT License
|
2
|
+
|
3
|
+
Copyright (c) 2024 Jyonn
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
13
|
+
copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
21
|
+
SOFTWARE.
|
UniTok-4.3.2/PKG-INFO
ADDED
@@ -0,0 +1,228 @@
|
|
1
|
+
Metadata-Version: 2.1
|
2
|
+
Name: UniTok
|
3
|
+
Version: 4.3.2
|
4
|
+
Summary: Unified Tokenizer
|
5
|
+
Home-page: https://github.com/Jyonn/UnifiedTokenizer
|
6
|
+
Author: Jyonn Liu
|
7
|
+
Author-email: liu@qijiong.work
|
8
|
+
License: MIT Licence
|
9
|
+
Keywords: token,tokenizer,NLP,transformers,glove,bert,llama
|
10
|
+
Platform: any
|
11
|
+
Description-Content-Type: text/markdown
|
12
|
+
License-File: LICENSE
|
13
|
+
|
14
|
+
# UniTok V4
|
15
|
+
|
16
|
+
The documentation for v3, old version, can be found [here](README_v3.md) in Chinese.
|
17
|
+
|
18
|
+
## Overview
|
19
|
+
|
20
|
+
[](https://badge.fury.io/py/unitok)
|
21
|
+
|
22
|
+
Welcome to the UniTok v4!
|
23
|
+
This library provides a unified preprocessing solution for machine learning datasets, handling diverse data types like text, categorical features, and numerical values.
|
24
|
+
|
25
|
+
Please refer to [UniTok Handbook](https://unitok.qijiong.work) for more detailed information.
|
26
|
+
|
27
|
+
## Road from V3 to V4
|
28
|
+
|
29
|
+
### Changes and Comparisons
|
30
|
+
|
31
|
+
| Feature | UniTok v3 | UniTok v4 | Comments |
|
32
|
+
|---------------------------------|-------------------------------------------------------------|-----------------------------------------------------|-------------------------------------------------------------------------------|
|
33
|
+
| `UniTok` class | Solely for tokenization | Manages the entire preprocessing lifecycle | |
|
34
|
+
| `UniDep` class | Data loading and combining | Removed | V4 combines the functionalities of `UniTok` and `UniDep` into a single class. |
|
35
|
+
| `Column` class | Column name is for both the original and tokenized datasets | N/A | V4 introduces a `Job` class. |
|
36
|
+
| `Job` class | N/A | Defines how a specific column should be tokenized | |
|
37
|
+
| `Tokenizer` class | Ambiguous return type definition | `return_list` parameter must be of type `bool` | |
|
38
|
+
| `Tokenizer` class | Only supports `BertTokenizer` for text processing | Supports all Tokenizers in the transformers library | New `TransformersTokenizer` class |
|
39
|
+
| `analyse` method | Supported | Not supported Currently | |
|
40
|
+
| `Meta` class | Only for human-friendly displaying | Manager for `Job`, `Tokenizer`, and `Vocab` | |
|
41
|
+
| `unitok` command | Visualization in the terminal | More colorful and detailed output | |
|
42
|
+
| `Vocab` class (unitok >= 4.1.0) | Save and load vocabulary using text files | Save and load vocabulary using pickle files | Avoids issues with special characters in text files |
|
43
|
+
|
44
|
+
### How to Migrate the Processed Data
|
45
|
+
|
46
|
+
```bash
|
47
|
+
unidep-upgrade-v4 <path>
|
48
|
+
```
|
49
|
+
|
50
|
+
## Installation
|
51
|
+
|
52
|
+
**Requirements**
|
53
|
+
|
54
|
+
- Python 3.7 or later
|
55
|
+
- Dependencies:
|
56
|
+
- pandas
|
57
|
+
- transformers
|
58
|
+
- tqdm
|
59
|
+
- rich
|
60
|
+
|
61
|
+
**Install UniTok via pip**
|
62
|
+
|
63
|
+
```bash
|
64
|
+
pip install unitok
|
65
|
+
```
|
66
|
+
|
67
|
+
## Core Concepts
|
68
|
+
|
69
|
+
**States**
|
70
|
+
|
71
|
+
- `initialized`: The initial state after creating a UniTok instance.
|
72
|
+
- `tokenized`: Achieved after applying tokenization to the dataset.
|
73
|
+
- `organized`: Reached after combining multiple datasets via operations like union.
|
74
|
+
|
75
|
+
**Components**
|
76
|
+
|
77
|
+
- UniTok: Manages the dataset preprocessing lifecycle.
|
78
|
+
- Job: Defines how a specific column should be tokenized.
|
79
|
+
- Tokenizer: Encodes data using various methods (e.g., BERT, splitting by delimiters).
|
80
|
+
- Vocabulary: Stores and manages unique tokens across datasets.
|
81
|
+
|
82
|
+
**Primary Key (key_job)**
|
83
|
+
|
84
|
+
The `key_job` acts as the primary key for operations like `getitem` and `union`, ensuring consistency across datasets.
|
85
|
+
|
86
|
+
## Usage Guide
|
87
|
+
|
88
|
+
### Loading Data
|
89
|
+
|
90
|
+
Load datasets using pandas:
|
91
|
+
|
92
|
+
```python
|
93
|
+
import pandas as pd
|
94
|
+
|
95
|
+
item = pd.read_csv(
|
96
|
+
filepath_or_buffer='news-sample.tsv',
|
97
|
+
sep='\t',
|
98
|
+
names=['nid', 'category', 'subcategory', 'title', 'abstract', 'url', 'title_entities', 'abstract_entities'],
|
99
|
+
usecols=['nid', 'category', 'subcategory', 'title', 'abstract'],
|
100
|
+
)
|
101
|
+
item['abstract'] = item['abstract'].fillna('') # Handle missing values
|
102
|
+
|
103
|
+
user = pd.read_csv(
|
104
|
+
filepath_or_buffer='user-sample.tsv',
|
105
|
+
sep='\t',
|
106
|
+
names=['uid', 'history'],
|
107
|
+
)
|
108
|
+
|
109
|
+
interaction = pd.read_csv(
|
110
|
+
filepath_or_buffer='interaction-sample.tsv',
|
111
|
+
sep='\t',
|
112
|
+
names=['uid', 'nid', 'click'],
|
113
|
+
)
|
114
|
+
```
|
115
|
+
|
116
|
+
### Defining and Adding Jobs
|
117
|
+
|
118
|
+
Define tokenization jobs for different columns:
|
119
|
+
|
120
|
+
```python
|
121
|
+
from unitok import UniTok, Vocab
|
122
|
+
from unitok.tokenizer import BertTokenizer, TransformersTokenizer, EntityTokenizer, SplitTokenizer, DigitTokenizer
|
123
|
+
|
124
|
+
item_vocab = Vocab(name='nid') # will be used across datasets
|
125
|
+
user_vocab = Vocab(name='uid') # will be used across datasets
|
126
|
+
|
127
|
+
with UniTok() as item_ut:
|
128
|
+
bert_tokenizer = BertTokenizer(vocab='bert')
|
129
|
+
llama_tokenizer = TransformersTokenizer(vocab='llama', key='huggyllama/llama-7b')
|
130
|
+
|
131
|
+
item_ut.add_job(tokenizer=EntityTokenizer(vocab=item_vocab), column='nid', key=True)
|
132
|
+
item_ut.add_job(tokenizer=bert_tokenizer, column='title', name='title@bert', truncate=20)
|
133
|
+
item_ut.add_job(tokenizer=llama_tokenizer, column='title', name='title@llama', truncate=20)
|
134
|
+
item_ut.add_job(tokenizer=bert_tokenizer, column='abstract', name='abstract@bert', truncate=50)
|
135
|
+
item_ut.add_job(tokenizer=llama_tokenizer, column='abstract', name='abstract@llama', truncate=50)
|
136
|
+
item_ut.add_job(tokenizer=EntityTokenizer(vocab='category'), column='category')
|
137
|
+
item_ut.add_job(tokenizer=EntityTokenizer(vocab='subcategory'), column='subcategory')
|
138
|
+
|
139
|
+
with UniTok() as user_ut:
|
140
|
+
user_ut.add_job(tokenizer=EntityTokenizer(vocab=user_vocab), column='uid', key=True)
|
141
|
+
user_ut.add_job(tokenizer=SplitTokenizer(vocab=item_vocab, sep=','), column='history', truncate=30)
|
142
|
+
|
143
|
+
with UniTok() as inter_ut:
|
144
|
+
inter_ut.add_index_job(name='index')
|
145
|
+
inter_ut.add_job(tokenizer=EntityTokenizer(vocab=user_vocab), column='uid')
|
146
|
+
inter_ut.add_job(tokenizer=EntityTokenizer(vocab=item_vocab), column='nid')
|
147
|
+
inter_ut.add_job(tokenizer=DigitTokenizer(vocab='click', vocab_size=2), column='click')
|
148
|
+
```
|
149
|
+
|
150
|
+
### Tokenizing Data
|
151
|
+
|
152
|
+
Tokenize and save the processed data:
|
153
|
+
|
154
|
+
```python
|
155
|
+
item_ut.tokenize(item).save('sample-ut/item')
|
156
|
+
item_vocab.deny_edit() # will raise an error if new items are detected in the user or interaction datasets
|
157
|
+
user_ut.tokenize(user).save('sample-ut/user')
|
158
|
+
inter_ut.tokenize(interaction).save('sample-ut/interaction')
|
159
|
+
```
|
160
|
+
|
161
|
+
### Combining Datasets
|
162
|
+
|
163
|
+
Combine datasets using union:
|
164
|
+
|
165
|
+
```python
|
166
|
+
# => {'category': 0, 'nid': 0, 'title@bert': [1996, 9639, 3035, 3870, ...], 'title@llama': [450, 1771, 4167, 10470, ...], 'abstract@bert': [4497, 1996, 14960, 2015, ...], 'abstract@llama': [1383, 459, 278, 451, ...], 'subcategory': 0}
|
167
|
+
print(item_ut[0])
|
168
|
+
|
169
|
+
# => {'uid': 0, 'history': [0, 1, 2]}
|
170
|
+
print(user_ut[0])
|
171
|
+
|
172
|
+
# => {'uid': 0, 'nid': 7, 'index': 0, 'click': 1}
|
173
|
+
print(inter_ut[0])
|
174
|
+
|
175
|
+
with inter_ut:
|
176
|
+
inter_ut.union(user_ut)
|
177
|
+
|
178
|
+
# => {'index': 0, 'click': 1, 'uid': 0, 'nid': 7, 'history': [0, 1, 2]}
|
179
|
+
print(inter_ut[0])
|
180
|
+
```
|
181
|
+
|
182
|
+
### Glance at the Terminal
|
183
|
+
|
184
|
+
```bash
|
185
|
+
unitok sample-ut/item
|
186
|
+
```
|
187
|
+
|
188
|
+
```text
|
189
|
+
UniTok (4beta)
|
190
|
+
Sample Size: 10
|
191
|
+
ID Column: nid
|
192
|
+
|
193
|
+
Jobs
|
194
|
+
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━┓
|
195
|
+
┃ Tokenizer ┃ Tokenizer ID ┃ Column Mapping ┃ Vocab ┃ Max Length ┃
|
196
|
+
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━┩
|
197
|
+
│ TransformersTokenizer │ auto_2VN5Ko │ abstract -> abstract@llama │ llama (size=32024) │ 50 │
|
198
|
+
├──────────────────────────────────────┼───────────────────────┼──────────────────────────────────────────────┼───────────────────────────────────┼───────────────────┤
|
199
|
+
│ EntityTokenizer │ auto_C0b9Du │ subcategory -> subcategory │ subcategory (size=8) │ N/A │
|
200
|
+
├──────────────────────────────────────┼───────────────────────┼──────────────────────────────────────────────┼───────────────────────────────────┼───────────────────┤
|
201
|
+
│ TransformersTokenizer │ auto_2VN5Ko │ title -> title@llama │ llama (size=32024) │ 20 │
|
202
|
+
├──────────────────────────────────────┼───────────────────────┼──────────────────────────────────────────────┼───────────────────────────────────┼───────────────────┤
|
203
|
+
│ EntityTokenizer │ auto_4WQYxo │ category -> category │ category (size=4) │ N/A │
|
204
|
+
├──────────────────────────────────────┼───────────────────────┼──────────────────────────────────────────────┼───────────────────────────────────┼───────────────────┤
|
205
|
+
│ BertTokenizer │ auto_Y9tADT │ abstract -> abstract@bert │ bert (size=30522) │ 46 │
|
206
|
+
├──────────────────────────────────────┼───────────────────────┼──────────────────────────────────────────────┼───────────────────────────────────┼───────────────────┤
|
207
|
+
│ BertTokenizer │ auto_Y9tADT │ title -> title@bert │ bert (size=30522) │ 16 │
|
208
|
+
├──────────────────────────────────────┼───────────────────────┼──────────────────────────────────────────────┼───────────────────────────────────┼───────────────────┤
|
209
|
+
│ EntityTokenizer │ auto_qwQALc │ nid -> nid │ nid (size=10) │ N/A │
|
210
|
+
└──────────────────────────────────────┴───────────────────────┴──────────────────────────────────────────────┴───────────────────────────────────┴───────────────────┘
|
211
|
+
```
|
212
|
+
|
213
|
+
## Contributing
|
214
|
+
|
215
|
+
We welcome contributions to UniTok! We appreciate your feedback, bug reports, and pull requests.
|
216
|
+
|
217
|
+
Our TODO list includes:
|
218
|
+
|
219
|
+
- [ ] More detailed documentation
|
220
|
+
- [ ] More examples and tutorials
|
221
|
+
- [ ] More SQL-like operations
|
222
|
+
- [ ] Analysis and visualization tools
|
223
|
+
|
224
|
+
## License
|
225
|
+
|
226
|
+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|
227
|
+
|
228
|
+
|
UniTok-4.3.2/README.md
ADDED
@@ -0,0 +1,213 @@
|
|
1
|
+
# UniTok V4
|
2
|
+
|
3
|
+
The documentation for v3, old version, can be found [here](README_v3.md) in Chinese.
|
4
|
+
|
5
|
+
## Overview
|
6
|
+
|
7
|
+
[](https://badge.fury.io/py/unitok)
|
8
|
+
|
9
|
+
Welcome to the UniTok v4!
|
10
|
+
This library provides a unified preprocessing solution for machine learning datasets, handling diverse data types like text, categorical features, and numerical values.
|
11
|
+
|
12
|
+
Please refer to [UniTok Handbook](https://unitok.qijiong.work) for more detailed information.
|
13
|
+
|
14
|
+
## Road from V3 to V4
|
15
|
+
|
16
|
+
### Changes and Comparisons
|
17
|
+
|
18
|
+
| Feature | UniTok v3 | UniTok v4 | Comments |
|
19
|
+
|---------------------------------|-------------------------------------------------------------|-----------------------------------------------------|-------------------------------------------------------------------------------|
|
20
|
+
| `UniTok` class | Solely for tokenization | Manages the entire preprocessing lifecycle | |
|
21
|
+
| `UniDep` class | Data loading and combining | Removed | V4 combines the functionalities of `UniTok` and `UniDep` into a single class. |
|
22
|
+
| `Column` class | Column name is for both the original and tokenized datasets | N/A | V4 introduces a `Job` class. |
|
23
|
+
| `Job` class | N/A | Defines how a specific column should be tokenized | |
|
24
|
+
| `Tokenizer` class | Ambiguous return type definition | `return_list` parameter must be of type `bool` | |
|
25
|
+
| `Tokenizer` class | Only supports `BertTokenizer` for text processing | Supports all Tokenizers in the transformers library | New `TransformersTokenizer` class |
|
26
|
+
| `analyse` method | Supported | Not supported Currently | |
|
27
|
+
| `Meta` class | Only for human-friendly displaying | Manager for `Job`, `Tokenizer`, and `Vocab` | |
|
28
|
+
| `unitok` command | Visualization in the terminal | More colorful and detailed output | |
|
29
|
+
| `Vocab` class (unitok >= 4.1.0) | Save and load vocabulary using text files | Save and load vocabulary using pickle files | Avoids issues with special characters in text files |
|
30
|
+
|
31
|
+
### How to Migrate the Processed Data
|
32
|
+
|
33
|
+
```bash
|
34
|
+
unidep-upgrade-v4 <path>
|
35
|
+
```
|
36
|
+
|
37
|
+
## Installation
|
38
|
+
|
39
|
+
**Requirements**
|
40
|
+
|
41
|
+
- Python 3.7 or later
|
42
|
+
- Dependencies:
|
43
|
+
- pandas
|
44
|
+
- transformers
|
45
|
+
- tqdm
|
46
|
+
- rich
|
47
|
+
|
48
|
+
**Install UniTok via pip**
|
49
|
+
|
50
|
+
```bash
|
51
|
+
pip install unitok
|
52
|
+
```
|
53
|
+
|
54
|
+
## Core Concepts
|
55
|
+
|
56
|
+
**States**
|
57
|
+
|
58
|
+
- `initialized`: The initial state after creating a UniTok instance.
|
59
|
+
- `tokenized`: Achieved after applying tokenization to the dataset.
|
60
|
+
- `organized`: Reached after combining multiple datasets via operations like union.
|
61
|
+
|
62
|
+
**Components**
|
63
|
+
|
64
|
+
- UniTok: Manages the dataset preprocessing lifecycle.
|
65
|
+
- Job: Defines how a specific column should be tokenized.
|
66
|
+
- Tokenizer: Encodes data using various methods (e.g., BERT, splitting by delimiters).
|
67
|
+
- Vocabulary: Stores and manages unique tokens across datasets.
|
68
|
+
|
69
|
+
**Primary Key (key_job)**
|
70
|
+
|
71
|
+
The `key_job` acts as the primary key for operations like `getitem` and `union`, ensuring consistency across datasets.
|
72
|
+
|
73
|
+
## Usage Guide
|
74
|
+
|
75
|
+
### Loading Data
|
76
|
+
|
77
|
+
Load datasets using pandas:
|
78
|
+
|
79
|
+
```python
|
80
|
+
import pandas as pd
|
81
|
+
|
82
|
+
item = pd.read_csv(
|
83
|
+
filepath_or_buffer='news-sample.tsv',
|
84
|
+
sep='\t',
|
85
|
+
names=['nid', 'category', 'subcategory', 'title', 'abstract', 'url', 'title_entities', 'abstract_entities'],
|
86
|
+
usecols=['nid', 'category', 'subcategory', 'title', 'abstract'],
|
87
|
+
)
|
88
|
+
item['abstract'] = item['abstract'].fillna('') # Handle missing values
|
89
|
+
|
90
|
+
user = pd.read_csv(
|
91
|
+
filepath_or_buffer='user-sample.tsv',
|
92
|
+
sep='\t',
|
93
|
+
names=['uid', 'history'],
|
94
|
+
)
|
95
|
+
|
96
|
+
interaction = pd.read_csv(
|
97
|
+
filepath_or_buffer='interaction-sample.tsv',
|
98
|
+
sep='\t',
|
99
|
+
names=['uid', 'nid', 'click'],
|
100
|
+
)
|
101
|
+
```
|
102
|
+
|
103
|
+
### Defining and Adding Jobs
|
104
|
+
|
105
|
+
Define tokenization jobs for different columns:
|
106
|
+
|
107
|
+
```python
|
108
|
+
from unitok import UniTok, Vocab
|
109
|
+
from unitok.tokenizer import BertTokenizer, TransformersTokenizer, EntityTokenizer, SplitTokenizer, DigitTokenizer
|
110
|
+
|
111
|
+
item_vocab = Vocab(name='nid') # will be used across datasets
|
112
|
+
user_vocab = Vocab(name='uid') # will be used across datasets
|
113
|
+
|
114
|
+
with UniTok() as item_ut:
|
115
|
+
bert_tokenizer = BertTokenizer(vocab='bert')
|
116
|
+
llama_tokenizer = TransformersTokenizer(vocab='llama', key='huggyllama/llama-7b')
|
117
|
+
|
118
|
+
item_ut.add_job(tokenizer=EntityTokenizer(vocab=item_vocab), column='nid', key=True)
|
119
|
+
item_ut.add_job(tokenizer=bert_tokenizer, column='title', name='title@bert', truncate=20)
|
120
|
+
item_ut.add_job(tokenizer=llama_tokenizer, column='title', name='title@llama', truncate=20)
|
121
|
+
item_ut.add_job(tokenizer=bert_tokenizer, column='abstract', name='abstract@bert', truncate=50)
|
122
|
+
item_ut.add_job(tokenizer=llama_tokenizer, column='abstract', name='abstract@llama', truncate=50)
|
123
|
+
item_ut.add_job(tokenizer=EntityTokenizer(vocab='category'), column='category')
|
124
|
+
item_ut.add_job(tokenizer=EntityTokenizer(vocab='subcategory'), column='subcategory')
|
125
|
+
|
126
|
+
with UniTok() as user_ut:
|
127
|
+
user_ut.add_job(tokenizer=EntityTokenizer(vocab=user_vocab), column='uid', key=True)
|
128
|
+
user_ut.add_job(tokenizer=SplitTokenizer(vocab=item_vocab, sep=','), column='history', truncate=30)
|
129
|
+
|
130
|
+
with UniTok() as inter_ut:
|
131
|
+
inter_ut.add_index_job(name='index')
|
132
|
+
inter_ut.add_job(tokenizer=EntityTokenizer(vocab=user_vocab), column='uid')
|
133
|
+
inter_ut.add_job(tokenizer=EntityTokenizer(vocab=item_vocab), column='nid')
|
134
|
+
inter_ut.add_job(tokenizer=DigitTokenizer(vocab='click', vocab_size=2), column='click')
|
135
|
+
```
|
136
|
+
|
137
|
+
### Tokenizing Data
|
138
|
+
|
139
|
+
Tokenize and save the processed data:
|
140
|
+
|
141
|
+
```python
|
142
|
+
item_ut.tokenize(item).save('sample-ut/item')
|
143
|
+
item_vocab.deny_edit() # will raise an error if new items are detected in the user or interaction datasets
|
144
|
+
user_ut.tokenize(user).save('sample-ut/user')
|
145
|
+
inter_ut.tokenize(interaction).save('sample-ut/interaction')
|
146
|
+
```
|
147
|
+
|
148
|
+
### Combining Datasets
|
149
|
+
|
150
|
+
Combine datasets using union:
|
151
|
+
|
152
|
+
```python
|
153
|
+
# => {'category': 0, 'nid': 0, 'title@bert': [1996, 9639, 3035, 3870, ...], 'title@llama': [450, 1771, 4167, 10470, ...], 'abstract@bert': [4497, 1996, 14960, 2015, ...], 'abstract@llama': [1383, 459, 278, 451, ...], 'subcategory': 0}
|
154
|
+
print(item_ut[0])
|
155
|
+
|
156
|
+
# => {'uid': 0, 'history': [0, 1, 2]}
|
157
|
+
print(user_ut[0])
|
158
|
+
|
159
|
+
# => {'uid': 0, 'nid': 7, 'index': 0, 'click': 1}
|
160
|
+
print(inter_ut[0])
|
161
|
+
|
162
|
+
with inter_ut:
|
163
|
+
inter_ut.union(user_ut)
|
164
|
+
|
165
|
+
# => {'index': 0, 'click': 1, 'uid': 0, 'nid': 7, 'history': [0, 1, 2]}
|
166
|
+
print(inter_ut[0])
|
167
|
+
```
|
168
|
+
|
169
|
+
### Glance at the Terminal
|
170
|
+
|
171
|
+
```bash
|
172
|
+
unitok sample-ut/item
|
173
|
+
```
|
174
|
+
|
175
|
+
```text
|
176
|
+
UniTok (4beta)
|
177
|
+
Sample Size: 10
|
178
|
+
ID Column: nid
|
179
|
+
|
180
|
+
Jobs
|
181
|
+
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━┓
|
182
|
+
┃ Tokenizer ┃ Tokenizer ID ┃ Column Mapping ┃ Vocab ┃ Max Length ┃
|
183
|
+
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━┩
|
184
|
+
│ TransformersTokenizer │ auto_2VN5Ko │ abstract -> abstract@llama │ llama (size=32024) │ 50 │
|
185
|
+
├──────────────────────────────────────┼───────────────────────┼──────────────────────────────────────────────┼───────────────────────────────────┼───────────────────┤
|
186
|
+
│ EntityTokenizer │ auto_C0b9Du │ subcategory -> subcategory │ subcategory (size=8) │ N/A │
|
187
|
+
├──────────────────────────────────────┼───────────────────────┼──────────────────────────────────────────────┼───────────────────────────────────┼───────────────────┤
|
188
|
+
│ TransformersTokenizer │ auto_2VN5Ko │ title -> title@llama │ llama (size=32024) │ 20 │
|
189
|
+
├──────────────────────────────────────┼───────────────────────┼──────────────────────────────────────────────┼───────────────────────────────────┼───────────────────┤
|
190
|
+
│ EntityTokenizer │ auto_4WQYxo │ category -> category │ category (size=4) │ N/A │
|
191
|
+
├──────────────────────────────────────┼───────────────────────┼──────────────────────────────────────────────┼───────────────────────────────────┼───────────────────┤
|
192
|
+
│ BertTokenizer │ auto_Y9tADT │ abstract -> abstract@bert │ bert (size=30522) │ 46 │
|
193
|
+
├──────────────────────────────────────┼───────────────────────┼──────────────────────────────────────────────┼───────────────────────────────────┼───────────────────┤
|
194
|
+
│ BertTokenizer │ auto_Y9tADT │ title -> title@bert │ bert (size=30522) │ 16 │
|
195
|
+
├──────────────────────────────────────┼───────────────────────┼──────────────────────────────────────────────┼───────────────────────────────────┼───────────────────┤
|
196
|
+
│ EntityTokenizer │ auto_qwQALc │ nid -> nid │ nid (size=10) │ N/A │
|
197
|
+
└──────────────────────────────────────┴───────────────────────┴──────────────────────────────────────────────┴───────────────────────────────────┴───────────────────┘
|
198
|
+
```
|
199
|
+
|
200
|
+
## Contributing
|
201
|
+
|
202
|
+
We welcome contributions to UniTok! We appreciate your feedback, bug reports, and pull requests.
|
203
|
+
|
204
|
+
Our TODO list includes:
|
205
|
+
|
206
|
+
- [ ] More detailed documentation
|
207
|
+
- [ ] More examples and tutorials
|
208
|
+
- [ ] More SQL-like operations
|
209
|
+
- [ ] Analysis and visualization tools
|
210
|
+
|
211
|
+
## License
|
212
|
+
|
213
|
+
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.
|