faTok 0.1.0__py3-none-any.whl
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.
- fatok/__about__.py +7 -0
- fatok/__init__.py +18 -0
- fatok/cli.py +95 -0
- fatok/data/special_tokens_map.json +9 -0
- fatok/data/tokenizer.json +2075 -0
- fatok/data/tokenizer_config.json +55 -0
- fatok/data/training_stats.json +13 -0
- fatok/tokenizer.py +215 -0
- fatok-0.1.0.dist-info/METADATA +170 -0
- fatok-0.1.0.dist-info/RECORD +13 -0
- fatok-0.1.0.dist-info/WHEEL +4 -0
- fatok-0.1.0.dist-info/entry_points.txt +2 -0
- fatok-0.1.0.dist-info/licenses/LICENSE +21 -0
fatok/__about__.py
ADDED
fatok/__init__.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"""faTok — a Persian-first BPE tokenizer.
|
|
2
|
+
|
|
3
|
+
Public API
|
|
4
|
+
----------
|
|
5
|
+
from fatok import FaTok
|
|
6
|
+
|
|
7
|
+
tok = FaTok() # load the bundled Persian tokenizer
|
|
8
|
+
ids = tok.encode("سلام دنیا")
|
|
9
|
+
text = tok.decode(ids)
|
|
10
|
+
n = tok.vocab_size
|
|
11
|
+
hf = tok.to_hf() # underlying PreTrainedTokenizerFast
|
|
12
|
+
|
|
13
|
+
See ``FaTok`` for the full API.
|
|
14
|
+
"""
|
|
15
|
+
from .tokenizer import FaTok
|
|
16
|
+
from .__about__ import __version__
|
|
17
|
+
|
|
18
|
+
__all__ = ["FaTok", "__version__"]
|
fatok/cli.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"""``fatok`` command-line interface.
|
|
2
|
+
|
|
3
|
+
Examples
|
|
4
|
+
--------
|
|
5
|
+
fatok info
|
|
6
|
+
fatok encode "سلام دنیا"
|
|
7
|
+
fatok encode "سلام دنیا" --json
|
|
8
|
+
fatok decode 1234 5678 910
|
|
9
|
+
fatok count "یک متن نسبتا طولانی برای شمارش توکنها"
|
|
10
|
+
fatok normalize "سَلام كَيْف حالُك"
|
|
11
|
+
"""
|
|
12
|
+
from __future__ import annotations
|
|
13
|
+
|
|
14
|
+
import argparse
|
|
15
|
+
import json
|
|
16
|
+
import sys
|
|
17
|
+
from typing import List, Optional
|
|
18
|
+
|
|
19
|
+
from .tokenizer import FaTok, info
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
def _build_parser() -> argparse.ArgumentParser:
|
|
23
|
+
p = argparse.ArgumentParser(
|
|
24
|
+
prog="fatok",
|
|
25
|
+
description="faTok — Persian BPE tokenizer.",
|
|
26
|
+
)
|
|
27
|
+
sub = p.add_subparsers(dest="cmd", required=True)
|
|
28
|
+
|
|
29
|
+
sub.add_parser("info", help="Print metadata about the bundled tokenizer.")
|
|
30
|
+
|
|
31
|
+
enc = sub.add_parser("encode", help="Encode text into token ids.")
|
|
32
|
+
enc.add_argument("text", help="Text to encode.")
|
|
33
|
+
enc.add_argument("--special-tokens", action="store_true",
|
|
34
|
+
help="Wrap with <s> ... </s>.")
|
|
35
|
+
enc.add_argument("--json", action="store_true",
|
|
36
|
+
help="Output JSON: {ids, n, text}.")
|
|
37
|
+
enc.add_argument("--path", help="Custom tokenizer.json path.")
|
|
38
|
+
|
|
39
|
+
dec = sub.add_parser("decode", help="Decode token ids back into text.")
|
|
40
|
+
dec.add_argument("ids", nargs="+", type=int, help="Token ids to decode.")
|
|
41
|
+
dec.add_argument("--keep-special-tokens", action="store_true",
|
|
42
|
+
help="Do not skip special tokens in the output.")
|
|
43
|
+
dec.add_argument("--path", help="Custom tokenizer.json path.")
|
|
44
|
+
|
|
45
|
+
cnt = sub.add_parser("count", help="Count tokens for a text.")
|
|
46
|
+
cnt.add_argument("text", help="Text to count tokens for.")
|
|
47
|
+
cnt.add_argument("--path", help="Custom tokenizer.json path.")
|
|
48
|
+
|
|
49
|
+
norm = sub.add_parser("normalize", help="Apply Persian normalization only.")
|
|
50
|
+
norm.add_argument("text", help="Text to normalize.")
|
|
51
|
+
norm.add_argument("--path", help="Custom tokenizer.json path.")
|
|
52
|
+
|
|
53
|
+
return p
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def main(argv: Optional[List[str]] = None) -> int:
|
|
57
|
+
args = _build_parser().parse_args(argv)
|
|
58
|
+
|
|
59
|
+
if args.cmd == "info":
|
|
60
|
+
data = info()
|
|
61
|
+
print(json.dumps(data, ensure_ascii=False, indent=2))
|
|
62
|
+
return 0
|
|
63
|
+
|
|
64
|
+
tok = FaTok(path=getattr(args, "path", None))
|
|
65
|
+
|
|
66
|
+
if args.cmd == "encode":
|
|
67
|
+
ids = tok.encode(args.text, add_special_tokens=args.special_tokens)
|
|
68
|
+
if args.json:
|
|
69
|
+
print(json.dumps({
|
|
70
|
+
"ids": ids,
|
|
71
|
+
"n": len(ids),
|
|
72
|
+
"text": args.text,
|
|
73
|
+
}, ensure_ascii=False))
|
|
74
|
+
else:
|
|
75
|
+
print(" ".join(str(i) for i in ids))
|
|
76
|
+
return 0
|
|
77
|
+
|
|
78
|
+
if args.cmd == "decode":
|
|
79
|
+
text = tok.decode(args.ids, skip_special_tokens=not args.keep_special_tokens)
|
|
80
|
+
print(text)
|
|
81
|
+
return 0
|
|
82
|
+
|
|
83
|
+
if args.cmd == "count":
|
|
84
|
+
print(tok.count(args.text))
|
|
85
|
+
return 0
|
|
86
|
+
|
|
87
|
+
if args.cmd == "normalize":
|
|
88
|
+
print(tok.normalize(args.text))
|
|
89
|
+
return 0
|
|
90
|
+
|
|
91
|
+
return 1
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
if __name__ == "__main__":
|
|
95
|
+
sys.exit(main())
|