indic-shift 0.0.1__tar.gz → 1.0.0__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.
Files changed (45) hide show
  1. indic_shift-1.0.0/.gitignore +11 -0
  2. indic_shift-1.0.0/CHANGELOG.md +17 -0
  3. indic_shift-1.0.0/LICENSE +202 -0
  4. indic_shift-1.0.0/LICENSE-UNICODE +39 -0
  5. indic_shift-1.0.0/NOTICE +9 -0
  6. indic_shift-1.0.0/PKG-INFO +245 -0
  7. indic_shift-1.0.0/README.md +195 -0
  8. indic_shift-1.0.0/SPEC.md +268 -0
  9. indic_shift-1.0.0/examples/count_tokens.py +13 -0
  10. indic_shift-1.0.0/examples/prompt_runs.py +27 -0
  11. indic_shift-1.0.0/examples/quickstart.py +16 -0
  12. indic_shift-1.0.0/examples/streaming.py +17 -0
  13. indic_shift-1.0.0/examples/transformers_usage.py +13 -0
  14. indic_shift-1.0.0/pyproject.toml +95 -0
  15. indic_shift-1.0.0/src/indic_shift/__init__.py +26 -0
  16. indic_shift-1.0.0/src/indic_shift/__main__.py +54 -0
  17. indic_shift-1.0.0/src/indic_shift/_registry.py +22 -0
  18. indic_shift-1.0.0/src/indic_shift/_tokenizer.py +261 -0
  19. indic_shift-1.0.0/src/indic_shift/_transform.py +290 -0
  20. indic_shift-1.0.0/src/indic_shift/data/indic-shift-131k.tokenizer.json +1 -0
  21. indic_shift-1.0.0/src/indic_shift/data/indic-shift-262k.tokenizer.json +1 -0
  22. indic_shift-1.0.0/src/indic_shift/data/transform.json +5907 -0
  23. indic_shift-1.0.0/src/indic_shift/hf.py +120 -0
  24. indic_shift-1.0.0/src/indic_shift/py.typed +0 -0
  25. indic_shift-1.0.0/tests/_fuzz.py +42 -0
  26. indic_shift-1.0.0/tests/conftest.py +31 -0
  27. indic_shift-1.0.0/tests/golden/golden.jsonl +257 -0
  28. indic_shift-1.0.0/tests/test_api.py +66 -0
  29. indic_shift-1.0.0/tests/test_golden.py +68 -0
  30. indic_shift-1.0.0/tests/test_hf.py +53 -0
  31. indic_shift-1.0.0/tests/test_integrity.py +38 -0
  32. indic_shift-1.0.0/tests/test_lead_space.py +108 -0
  33. indic_shift-1.0.0/tests/test_nesting.py +53 -0
  34. indic_shift-1.0.0/tests/test_reserved.py +56 -0
  35. indic_shift-1.0.0/tests/test_roundtrip.py +37 -0
  36. indic_shift-1.0.0/tests/test_stream.py +100 -0
  37. indic_shift-1.0.0/tests/test_unicode_freeze.py +34 -0
  38. indic_shift-1.0.0/tools/build_transform.py +516 -0
  39. indic_shift-1.0.0/tools/check_dist.py +83 -0
  40. indic_shift-1.0.0/tools/make_golden.py +73 -0
  41. indic_shift-1.0.0/tools/verify/adversarial.py +148 -0
  42. indic_shift-1.0.0/tools/verify/compare.py +228 -0
  43. indic_shift-0.0.1/PKG-INFO +0 -13
  44. indic_shift-0.0.1/README.md +0 -3
  45. indic_shift-0.0.1/pyproject.toml +0 -16
@@ -0,0 +1,11 @@
1
+ __pycache__/
2
+ *.py[cod]
3
+ *.egg-info/
4
+ build/
5
+ dist/
6
+ .venv/
7
+ .pytest_cache/
8
+ .mypy_cache/
9
+ .ruff_cache/
10
+ .coverage
11
+ htmlcov/
@@ -0,0 +1,17 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project are documented here. The format follows
4
+ [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) and the package uses
5
+ [Semantic Versioning](https://semver.org/). Token ids never change within a major version.
6
+
7
+ ## [1.0.0] - unreleased
8
+
9
+ ### Added
10
+
11
+ - Tokenizers `indic-shift-262k` (262,144 ids) and `indic-shift-131k` (131,072 ids), bundled in the
12
+ package with pinned sha256 values.
13
+ - Python API: `indic_shift.load`, `Tokenizer.encode` / `encode_batch` / `decode` / `decode_batch`,
14
+ `StreamDecoder` for incremental decoding, `Transform`, `data_path`, `available`.
15
+ - Hugging Face `transformers` integration: `indic_shift.hf.IndicShiftTokenizer`.
16
+ - Command line tool `indic-shift encode | decode | inspect`.
17
+ - `SPEC.md` for ports, golden test vectors, and verification tools under `tools/`.
@@ -0,0 +1,202 @@
1
+
2
+ Apache License
3
+ Version 2.0, January 2004
4
+ http://www.apache.org/licenses/
5
+
6
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
7
+
8
+ 1. Definitions.
9
+
10
+ "License" shall mean the terms and conditions for use, reproduction,
11
+ and distribution as defined by Sections 1 through 9 of this document.
12
+
13
+ "Licensor" shall mean the copyright owner or entity authorized by
14
+ the copyright owner that is granting the License.
15
+
16
+ "Legal Entity" shall mean the union of the acting entity and all
17
+ other entities that control, are controlled by, or are under common
18
+ control with that entity. For the purposes of this definition,
19
+ "control" means (i) the power, direct or indirect, to cause the
20
+ direction or management of such entity, whether by contract or
21
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
22
+ outstanding shares, or (iii) beneficial ownership of such entity.
23
+
24
+ "You" (or "Your") shall mean an individual or Legal Entity
25
+ exercising permissions granted by this License.
26
+
27
+ "Source" form shall mean the preferred form for making modifications,
28
+ including but not limited to software source code, documentation
29
+ source, and configuration files.
30
+
31
+ "Object" form shall mean any form resulting from mechanical
32
+ transformation or translation of a Source form, including but
33
+ not limited to compiled object code, generated documentation,
34
+ and conversions to other media types.
35
+
36
+ "Work" shall mean the work of authorship, whether in Source or
37
+ Object form, made available under the License, as indicated by a
38
+ copyright notice that is included in or attached to the work
39
+ (an example is provided in the Appendix below).
40
+
41
+ "Derivative Works" shall mean any work, whether in Source or Object
42
+ form, that is based on (or derived from) the Work and for which the
43
+ editorial revisions, annotations, elaborations, or other modifications
44
+ represent, as a whole, an original work of authorship. For the purposes
45
+ of this License, Derivative Works shall not include works that remain
46
+ separable from, or merely link (or bind by name) to the interfaces of,
47
+ the Work and Derivative Works thereof.
48
+
49
+ "Contribution" shall mean any work of authorship, including
50
+ the original version of the Work and any modifications or additions
51
+ to that Work or Derivative Works thereof, that is intentionally
52
+ submitted to Licensor for inclusion in the Work by the copyright owner
53
+ or by an individual or Legal Entity authorized to submit on behalf of
54
+ the copyright owner. For the purposes of this definition, "submitted"
55
+ means any form of electronic, verbal, or written communication sent
56
+ to the Licensor or its representatives, including but not limited to
57
+ communication on electronic mailing lists, source code control systems,
58
+ and issue tracking systems that are managed by, or on behalf of, the
59
+ Licensor for the purpose of discussing and improving the Work, but
60
+ excluding communication that is conspicuously marked or otherwise
61
+ designated in writing by the copyright owner as "Not a Contribution."
62
+
63
+ "Contributor" shall mean Licensor and any individual or Legal Entity
64
+ on behalf of whom a Contribution has been received by Licensor and
65
+ subsequently incorporated within the Work.
66
+
67
+ 2. Grant of Copyright License. Subject to the terms and conditions of
68
+ this License, each Contributor hereby grants to You a perpetual,
69
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
70
+ copyright license to reproduce, prepare Derivative Works of,
71
+ publicly display, publicly perform, sublicense, and distribute the
72
+ Work and such Derivative Works in Source or Object form.
73
+
74
+ 3. Grant of Patent License. Subject to the terms and conditions of
75
+ this License, each Contributor hereby grants to You a perpetual,
76
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
77
+ (except as stated in this section) patent license to make, have made,
78
+ use, offer to sell, sell, import, and otherwise transfer the Work,
79
+ where such license applies only to those patent claims licensable
80
+ by such Contributor that are necessarily infringed by their
81
+ Contribution(s) alone or by combination of their Contribution(s)
82
+ with the Work to which such Contribution(s) was submitted. If You
83
+ institute patent litigation against any entity (including a
84
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
85
+ or a Contribution incorporated within the Work constitutes direct
86
+ or contributory patent infringement, then any patent licenses
87
+ granted to You under this License for that Work shall terminate
88
+ as of the date such litigation is filed.
89
+
90
+ 4. Redistribution. You may reproduce and distribute copies of the
91
+ Work or Derivative Works thereof in any medium, with or without
92
+ modifications, and in Source or Object form, provided that You
93
+ meet the following conditions:
94
+
95
+ (a) You must give any other recipients of the Work or
96
+ Derivative Works a copy of this License; and
97
+
98
+ (b) You must cause any modified files to carry prominent notices
99
+ stating that You changed the files; and
100
+
101
+ (c) You must retain, in the Source form of any Derivative Works
102
+ that You distribute, all copyright, patent, trademark, and
103
+ attribution notices from the Source form of the Work,
104
+ excluding those notices that do not pertain to any part of
105
+ the Derivative Works; and
106
+
107
+ (d) If the Work includes a "NOTICE" text file as part of its
108
+ distribution, then any Derivative Works that You distribute must
109
+ include a readable copy of the attribution notices contained
110
+ within such NOTICE file, excluding those notices that do not
111
+ pertain to any part of the Derivative Works, in at least one
112
+ of the following places: within a NOTICE text file distributed
113
+ as part of the Derivative Works; within the Source form or
114
+ documentation, if provided along with the Derivative Works; or,
115
+ within a display generated by the Derivative Works, if and
116
+ wherever such third-party notices normally appear. The contents
117
+ of the NOTICE file are for informational purposes only and
118
+ do not modify the License. You may add Your own attribution
119
+ notices within Derivative Works that You distribute, alongside
120
+ or as an addendum to the NOTICE text from the Work, provided
121
+ that such additional attribution notices cannot be construed
122
+ as modifying the License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright [yyyy] [name of copyright owner]
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,39 @@
1
+ UNICODE LICENSE V3
2
+
3
+ COPYRIGHT AND PERMISSION NOTICE
4
+
5
+ Copyright © 1991-2026 Unicode, Inc.
6
+
7
+ NOTICE TO USER: Carefully read the following legal agreement. BY
8
+ DOWNLOADING, INSTALLING, COPYING OR OTHERWISE USING DATA FILES, AND/OR
9
+ SOFTWARE, YOU UNEQUIVOCALLY ACCEPT, AND AGREE TO BE BOUND BY, ALL OF THE
10
+ TERMS AND CONDITIONS OF THIS AGREEMENT. IF YOU DO NOT AGREE, DO NOT
11
+ DOWNLOAD, INSTALL, COPY, DISTRIBUTE OR USE THE DATA FILES OR SOFTWARE.
12
+
13
+ Permission is hereby granted, free of charge, to any person obtaining a
14
+ copy of data files and any associated documentation (the "Data Files") or
15
+ software and any associated documentation (the "Software") to deal in the
16
+ Data Files or Software without restriction, including without limitation
17
+ the rights to use, copy, modify, merge, publish, distribute, and/or sell
18
+ copies of the Data Files or Software, and to permit persons to whom the
19
+ Data Files or Software are furnished to do so, provided that either (a)
20
+ this copyright and permission notice appear with all copies of the Data
21
+ Files or Software, or (b) this copyright and permission notice appear in
22
+ associated Documentation.
23
+
24
+ THE DATA FILES AND SOFTWARE ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
25
+ KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT OF
27
+ THIRD PARTY RIGHTS.
28
+
29
+ IN NO EVENT SHALL THE COPYRIGHT HOLDER OR HOLDERS INCLUDED IN THIS NOTICE
30
+ BE LIABLE FOR ANY CLAIM, OR ANY SPECIAL INDIRECT OR CONSEQUENTIAL DAMAGES,
31
+ OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
32
+ WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
33
+ ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THE DATA
34
+ FILES OR SOFTWARE.
35
+
36
+ Except as contained in this notice, the name of a copyright holder shall
37
+ not be used in advertising or otherwise to promote the sale, use or other
38
+ dealings in these Data Files or Software without prior written
39
+ authorization of the copyright holder.
@@ -0,0 +1,9 @@
1
+ Indic Shift
2
+ Copyright 2026 Chariot Artificial Intelligence Private Limited
3
+
4
+ This product includes software and tokenizer data developed by Chariot Artificial Intelligence Private Limited.
5
+ Licensed under the Apache License, Version 2.0 (see LICENSE).
6
+
7
+ This product includes data derived from the Unicode Character Database, version 15.0.0 (the
8
+ transform tables in src/indic_shift/data/transform.json). Unicode data is Copyright (c) Unicode, Inc.
9
+ and is used under the Unicode License V3 (see LICENSE-UNICODE).
@@ -0,0 +1,245 @@
1
+ Metadata-Version: 2.5
2
+ Name: indic-shift
3
+ Version: 1.0.0
4
+ Summary: Indic Shift: an Indic-first byte-level BPE tokenizer with exact, reversible script sharing
5
+ Project-URL: Homepage, https://github.com/Chariot-AI/indic-shift
6
+ Project-URL: Source, https://github.com/Chariot-AI/indic-shift
7
+ Project-URL: Issues, https://github.com/Chariot-AI/indic-shift/issues
8
+ Project-URL: Changelog, https://github.com/Chariot-AI/indic-shift/blob/main/CHANGELOG.md
9
+ Author: Chariot Artificial Intelligence Private Limited
10
+ License-Expression: Apache-2.0 AND Unicode-3.0
11
+ License-File: LICENSE
12
+ License-File: LICENSE-UNICODE
13
+ License-File: NOTICE
14
+ Keywords: bpe,indic,llm,multilingual,nlp,tokenizer
15
+ Classifier: Development Status :: 5 - Production/Stable
16
+ Classifier: Intended Audience :: Developers
17
+ Classifier: Intended Audience :: Science/Research
18
+ Classifier: Natural Language :: Bengali
19
+ Classifier: Natural Language :: English
20
+ Classifier: Natural Language :: Hindi
21
+ Classifier: Natural Language :: Marathi
22
+ Classifier: Natural Language :: Panjabi
23
+ Classifier: Natural Language :: Tamil
24
+ Classifier: Natural Language :: Telugu
25
+ Classifier: Natural Language :: Urdu
26
+ Classifier: Operating System :: OS Independent
27
+ Classifier: Programming Language :: Python :: 3
28
+ Classifier: Programming Language :: Python :: 3 :: Only
29
+ Classifier: Programming Language :: Python :: 3.10
30
+ Classifier: Programming Language :: Python :: 3.11
31
+ Classifier: Programming Language :: Python :: 3.12
32
+ Classifier: Programming Language :: Python :: 3.13
33
+ Classifier: Programming Language :: Python :: 3.14
34
+ Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
35
+ Classifier: Topic :: Text Processing :: Linguistic
36
+ Classifier: Typing :: Typed
37
+ Requires-Python: >=3.10
38
+ Requires-Dist: tokenizers<1,>=0.22
39
+ Provides-Extra: dev
40
+ Requires-Dist: build>=1.2; extra == 'dev'
41
+ Requires-Dist: mypy>=1.13; extra == 'dev'
42
+ Requires-Dist: pytest>=8; extra == 'dev'
43
+ Requires-Dist: ruff>=0.8; extra == 'dev'
44
+ Requires-Dist: twine>=6.1; extra == 'dev'
45
+ Provides-Extra: test
46
+ Requires-Dist: pytest>=8; extra == 'test'
47
+ Provides-Extra: transformers
48
+ Requires-Dist: transformers>=4.56; extra == 'transformers'
49
+ Description-Content-Type: text/markdown
50
+
51
+ # Indic Shift
52
+
53
+ An Indic-first byte-level BPE tokenizer with exact, reversible script sharing. Two vocabulary sizes:
54
+ 262K and 131K.
55
+
56
+ ```python
57
+ import indic_shift
58
+
59
+ tok = indic_shift.load("262k") # or "131k"
60
+ ids = tok.encode("भारत एक विशाल देश है। আমি বাংলায় কথা বলি।")
61
+ assert tok.decode(ids) == "भारत एक विशाल देश है। আমি বাংলায় কথা বলি।"
62
+ ```
63
+
64
+ ## What it is
65
+
66
+ Indic Shift is byte-level BPE plus a small reversible text transform that runs before BPE and after
67
+ decoding:
68
+
69
+ - The Brahmic scripts share one set of letter symbols, and a script marker records which script is
70
+ active. `क`, `ক`, `ਕ`, `ક`, `କ`, `க`, `క`, `ಕ` and `ക` all become the same letter symbol, so BPE
71
+ can learn word pieces once for several scripts.
72
+ - Decimal digits of every script share ten digit symbols, with a marker for the digit set.
73
+ - Decoding restores the exact original text, for every Unicode string.
74
+
75
+ It covers all of Unicode (byte-level, so there are no unknown tokens) and is built for the scheduled
76
+ languages of India, English and code. Scripts that share letter symbols: Devanagari,
77
+ Bengali-Assamese, Gurmukhi, Gujarati, Odia, Tamil, Telugu, Kannada, Malayalam and Meetei Mayek.
78
+
79
+ ## Install
80
+
81
+ ```bash
82
+ pip install indic-shift # Python 3.10+, depends only on tokenizers
83
+ pip install "indic-shift[transformers]" # optional Hugging Face transformers integration
84
+ ```
85
+
86
+ The tokenizer files ship inside the package. They are checked against pinned sha256 values when they
87
+ load, and loading needs no network access. Token ids are the same on every supported Python version
88
+ and platform.
89
+
90
+ ## Quick start
91
+
92
+ ```python
93
+ import indic_shift
94
+
95
+ tok = indic_shift.load("262k")
96
+ ids = tok.encode("আমি বাংলায় কথা বলি") # [26162, 211090, 5103, 20496]; no BOS/EOS is added
97
+ text = tok.decode(ids) # "আমি বাংলায় কথা বলি"
98
+ tok.vocab_size # 262144
99
+
100
+ batch = tok.encode_batch(["नमस्ते", "வணக்கம்", "Hello, world", "سلام"])
101
+ tok.decode_batch(batch) # ["नमस्ते", "வணக்கம்", "Hello, world", "سلام"]
102
+ ```
103
+
104
+ `indic_shift.available()` lists the bundled tokenizers (`indic-shift-131k`, `indic-shift-262k`).
105
+ There is also a small command line tool:
106
+
107
+ ```bash
108
+ indic-shift encode "नमस्ते दुनिया" # [182427, 15851]
109
+ indic-shift decode 182427 15851 # नमस्ते दुनिया
110
+ indic-shift -t 131k inspect "আমি বাংলায় কথা বলি" # transformed text and tokens
111
+ ```
112
+
113
+ ## The leading-space rule
114
+
115
+ Read this before you build datasets or serve a model.
116
+
117
+ Every `encode()` call is one **text run**. The tokenizer adds **one** space in front of the run if
118
+ the text starts with a space, or with a letter or mark of a script that separates words with spaces
119
+ (Latin, Indic, Arabic, Cyrillic, Hangul, Myanmar, ...). It adds none if the text starts with a
120
+ digit, punctuation, a symbol or emoji, a line break or a tab, or with Thai, Lao, Khmer, Chinese or
121
+ Japanese characters. `decode()` removes exactly that one space, so the round trip is exact.
122
+
123
+ | text | space added | BPE sees (◌ = Indic Shift symbol) | tokens (262K) |
124
+ |---|---|---|---|
125
+ | `Hello` | yes | `␠Hello` | `␠Hello` |
126
+ | `␠Hello` | yes | `␠␠Hello` | `␠` `␠Hello` |
127
+ | `hello world` | yes | `␠hello␠world` | `␠hello` `␠world` |
128
+ | `日本語` | no | `日本語` | `日本語` |
129
+ | `ภาษาไทย` | no | `ภาษาไทย` | `ภาษาไทย` |
130
+ | `한국어` | yes | `␠한국어` | `␠한국어` |
131
+ | `தமிழ்` | yes | `␠◌◌◌◌◌◌` | `␠◌◌◌◌◌◌` |
132
+ | `{"a": 1}` | no | `{"a":␠◌}` | `{"` `a` `":` `␠◌` `}` |
133
+ | `\nx` | no | `\nx` | `\n` `x` |
134
+ | (empty) | no | (empty) | (none) |
135
+
136
+ What follows from the rule:
137
+
138
+ - **Encode a document whole.** Encoding chunks separately and joining their ids gives different ids,
139
+ because each chunk gets its own leading space and starts from the initial script state.
140
+ `encode("नमस्ते दुनिया")` is `[182427, 15851]`, but `encode("नमस्ते") + encode(" दुनिया")` is
141
+ `[182427, 476, 15851]`.
142
+ - **Encode each piece of a prompt on its own.** If your model's prompt format puts control tokens
143
+ between pieces of text (roles, tool calls, ...), encode each piece of text between two control
144
+ tokens with its own `encode()` call and put your control-token ids between them. Decode each piece
145
+ on its own too (or call `StreamDecoder.flush()` at each control token).
146
+ - **Continue a run with all of its text.** When a prompt ends inside a run (an assistant prefill, a
147
+ resumed generation), encode that run once with all of its text so far. Never encode the
148
+ continuation separately and append its ids.
149
+
150
+ ## Streaming decode
151
+
152
+ Decoding is stateful: the active script and the leading space are carried from the start of the
153
+ run. Slices of ids decoded separately do not join up to the right text:
154
+
155
+ ```python
156
+ ids = tok.encode("আমি বাংলায় কথা বলি")
157
+ tok.decode(ids[:2]) + tok.decode(ids[2:]) # "আমি বাংলায়कथा बलि" (wrong: script and space lost)
158
+ ```
159
+
160
+ For incremental output use a `StreamDecoder`. It carries the state and holds back incomplete
161
+ characters:
162
+
163
+ ```python
164
+ dec = tok.stream_decoder()
165
+ for token_id in generated_ids: # e.g. from a model, one id at a time
166
+ print(dec.step(token_id), end="", flush=True)
167
+ print(dec.flush()) # end of the run; ready for the next run
168
+ ```
169
+
170
+ Everything returned by `step` / `feed` plus `flush` equals `tok.decode(ids)`. Serving stacks with
171
+ their own incremental detokenizer (for example the sliding-window detokenizers of vLLM, SGLang or
172
+ TGI) must decode with these semantics; see [SPEC.md](https://github.com/Chariot-AI/indic-shift/blob/main/SPEC.md).
173
+
174
+ ## Hugging Face transformers
175
+
176
+ ```python
177
+ from indic_shift.hf import IndicShiftTokenizer
178
+
179
+ hf_tok = IndicShiftTokenizer.from_name("262k")
180
+ batch = hf_tok(["नमस्ते दुनिया", "Hello"])
181
+ hf_tok.save_pretrained("my-tokenizer") # IndicShiftTokenizer.from_pretrained("my-tokenizer")
182
+ ```
183
+
184
+ Tokens you add with `add_special_tokens` get new ids after the vocabulary, and the text between them
185
+ is encoded and decoded run by run, as described above. Offsets (`return_offsets_mapping`) are not
186
+ supported.
187
+
188
+ ## Using `tokenizer.json` directly
189
+
190
+ `tokenizer.json` on its own gives the wrong ids: text must go through the transform first, and the
191
+ decoded text must go back through it.
192
+
193
+ ```python
194
+ import indic_shift
195
+ from tokenizers import Tokenizer
196
+
197
+ transform = indic_shift.load("262k").transform
198
+ raw = Tokenizer.from_file(str(indic_shift.data_path("262k")))
199
+ wire = transform.encode("আমি বাংলায় কথা বলি") # the transform (includes the leading space)
200
+ ids = raw.encode(wire, add_special_tokens=False).ids
201
+ assert transform.decode(raw.decode(ids)) == "আমি বাংলায় কথা বলি"
202
+ ```
203
+
204
+ To port Indic Shift to another language or serving stack, see [SPEC.md](https://github.com/Chariot-AI/indic-shift/blob/main/SPEC.md). It describes the
205
+ transform, the data files and the decoding rules, and `tests/golden/golden.jsonl` has test vectors
206
+ with exact ids.
207
+
208
+ ## Two sizes
209
+
210
+ | | `indic-shift-262k` | `indic-shift-131k` |
211
+ |---|---|---|
212
+ | vocabulary | 262,144 ids | 131,072 ids |
213
+ | suited to | larger models | small or latency-bound models |
214
+
215
+ The 131K tokenizer is the first part of the 262K: its 131,072 ids are the same tokens in both, its
216
+ merges are the first merges of the 262K, and both use the same transform and split rule. Any 131K id
217
+ sequence decodes to the same text with either tokenizer. The two encode a text identically whenever
218
+ the 262K encoding uses only ids below 131,072.
219
+
220
+ ## How it works
221
+
222
+ 1. **Escape.** Private-use characters U+E000..U+F8FF in the input are escaped, because the transform
223
+ uses that range for its own symbols.
224
+ 2. **Map.** Four passes replace characters with symbols: decimal digits (ten shared digit symbols plus
225
+ a digit-set marker), Brahmic letters and signs (shared letter symbols plus a script marker), kana
226
+ (shared symbols for hiragana/katakana pairs) and Arabic-script letters. A marker is written only
227
+ when the script changes. Each pass starts in a default state (Devanagari, ASCII digits, Arabic),
228
+ so text in the default scripts carries no markers. The danda `।` and double danda `॥` belong to
229
+ every Brahmic script and return the pass to its default state.
230
+ 3. **Leading space** (see above), then byte-level BPE. The split rule keeps each Indic word together
231
+ and cuts long numbers into groups of three digits, aligned from the right.
232
+
233
+ Decoding inverts each step. Each step is lossless, so `decode(encode(text)) == text` for every
234
+ string. The tests check this on every Unicode code point and on random text.
235
+
236
+ ## Versioning
237
+
238
+ Token ids never change within a major version: `indic-shift` 1.x always gives the same ids for the
239
+ same text. The sha256 of every bundled file is pinned in the package and listed in the release notes.
240
+
241
+ ## License
242
+
243
+ Apache License 2.0, for the code and the tokenizer files. See [LICENSE](https://github.com/Chariot-AI/indic-shift/blob/main/LICENSE) and [NOTICE](https://github.com/Chariot-AI/indic-shift/blob/main/NOTICE).
244
+ The transform tables are derived from the Unicode Character Database and are also covered by the
245
+ Unicode License V3 ([LICENSE-UNICODE](https://github.com/Chariot-AI/indic-shift/blob/main/LICENSE-UNICODE)).