Python bindings for general-sam and some utilities
Project description
general-sam-py
Python bindings for general-sam
and some utilities.
flowchart LR
init((ε))
a((a))
b((b))
ab((ab))
bc(((bc)))
abc((abc))
abcb((abcb))
abcbc(((abcbc)))
init -- a --> a
init -- b --> b
a -- b --> ab
b -- c --> bc
init -- c --> bc
ab -- c --> abc
bc -- b --> abcb
abc -- b --> abcb
abcb -- c --> abcbc
The suffix automaton of abcbc.
Installation
pip install general-sam
Usage
GeneralSam
from general_sam import GeneralSam
sam = GeneralSam.from_bytes(b"abcbc")
# "cbc" is a suffix of "abcbc"
state = sam.get_root_state()
state.feed_bytes(b"cbc")
assert state.is_accepting()
# "bcb" is not a suffix of "abcbc"
state = sam.get_root_state()
state.feed_bytes(b"bcb")
assert not state.is_accepting()
from general_sam import GeneralSam
sam = GeneralSam.from_chars("abcbc")
state = sam.get_root_state()
# "b" is not a suffix but at least a substring of "abcbc"
state.feed_chars("b")
assert not state.is_accepting()
# "bc" is a suffix of "abcbc"
state.feed_chars("c")
assert state.is_accepting()
# "bcbc" is a suffix of "abcbc"
state.feed_chars("bc")
assert state.is_accepting()
# "bcbcbc" is not a substring, much less a suffix of "abcbc"
state.feed_chars("bc")
assert not state.is_accepting() and state.is_nil()
from general_sam import GeneralSam, GeneralSamState, build_trie_from_chars
trie, _ = build_trie_from_chars(["hello", "Chielo"])
sam = GeneralSam.from_trie(trie)
def fetch_state(s: str) -> GeneralSamState:
state = sam.get_root_state()
state.feed_chars(s)
return state
assert fetch_state("lo").is_accepting()
assert fetch_state("ello").is_accepting()
assert fetch_state("elo").is_accepting()
state = fetch_state("el")
assert not state.is_accepting() and not state.is_nil()
state = fetch_state("bye")
assert not state.is_accepting() and state.is_nil()
VocabPrefixAutomaton
from general_sam import CountInfo, VocabPrefixAutomaton
vocab = ["歌曲", "聆听歌曲", "播放歌曲", "歌词", "查看歌词"]
automaton = VocabPrefixAutomaton(vocab, bytes_or_chars="chars")
# NOTE: CountInfo instances are actually related to the sorted `vocab`:
_ = ["播放歌曲", "查看歌词", "歌曲", "歌词", "聆听歌曲"]
# Case 1:
# 一起 | 聆 | 听 | 歌
state = automaton.get_root_state()
# prepend '歌'
cnt_info = automaton.prepend_feed(state, "歌")
assert cnt_info is not None and cnt_info == CountInfo(
str_cnt=2, tot_cnt_lower=2, tot_cnt_upper=4
)
# found '歌曲' at the index 0 and '歌词' at the index 3 prefixed with '歌'
selected_idx = automaton.get_order_slice(cnt_info)
assert frozenset(selected_idx) == {0, 3}
selected_vocab = [vocab[i] for i in selected_idx]
assert frozenset(selected_vocab) == {"歌曲", "歌词"}
# prepend 听
cnt_info = automaton.prepend_feed(state, "听")
# found nothing prefixed with '听歌'
assert cnt_info is None
assert not state.is_nil()
# prepend 聆
cnt_info = automaton.prepend_feed(state, "聆")
assert cnt_info is not None and cnt_info == CountInfo(
str_cnt=1, tot_cnt_lower=4, tot_cnt_upper=5
)
# found '聆听歌曲' at the index 1 prefixed with '聆听歌'
selected_idx = automaton.get_order_slice(cnt_info)
assert frozenset(selected_idx) == {1}
selected_vocab = [vocab[i] for i in selected_idx]
assert frozenset(selected_vocab) == {"聆听歌曲"}
# prepend 一起
assert not state.is_nil()
# found nothing prefixed with '一起聆听歌'
cnt_info = automaton.prepend_feed(state, "一起")
assert state.is_nil()
# Case 2:
# 来 | 查看 | 歌词
state = automaton.get_root_state()
# prepend 歌词
cnt_info = automaton.prepend_feed(state, "歌词")
assert cnt_info is not None and cnt_info == CountInfo(
str_cnt=1, tot_cnt_lower=3, tot_cnt_upper=4
)
# found '歌词' at the index 3 prefixed with '歌词'
selected_idx = automaton.get_order_slice(cnt_info)
assert frozenset(selected_idx) == {3}
selected_vocab = [vocab[i] for i in selected_idx]
assert frozenset(selected_vocab) == {"歌词"}
# prepend 查看
cnt_info = automaton.prepend_feed(state, "查看")
assert cnt_info is not None and cnt_info == CountInfo(
str_cnt=1, tot_cnt_lower=1, tot_cnt_upper=2
)
# found '查看歌词' at the index 4 prefixed with '查看歌词'
selected_idx = automaton.get_order_slice(cnt_info)
assert frozenset(selected_idx) == {4}
selected_vocab = [vocab[i] for i in selected_idx]
assert frozenset(selected_vocab) == {"查看歌词"}
# prepend 来
assert not state.is_nil()
# found nothing prefixed with '来查看歌词'
cnt_info = automaton.prepend_feed(state, "来")
assert state.is_nil()
GreedyTokenizer
from general_sam import GeneralSam, GreedyTokenizer, build_trie_from_chars
vocab = ["a", "ab", "b", "bc", "c", "d", "e", "f", "cd", "abcde"]
trie, token_to_trie_node = build_trie_from_chars(vocab)
trie_node_to_token = [-1] * trie.num_of_nodes()
for i, j in enumerate(token_to_trie_node):
trie_node_to_token[j] = i
sam = GeneralSam.from_trie(trie)
tokenizer = GreedyTokenizer.from_sam_and_trie(sam, trie)
def tokenize(s: str):
return [(trie_node_to_token[i], j) for i, j in tokenizer.tokenize_str(s)]
assert tokenize("abcde") == [(9, 5)]
assert tokenize("abcdf") == [(1, 2), (8, 2), (7, 1)]
assert tokenize("abca") == [(1, 2), (4, 1), (0, 1)]
License
- © 2023-2026 ModelTC Team (Chielo mail@chielo.org)
This project is licensed under either of
at your option.
The SPDX license identifier for this project is MIT OR Apache-2.0.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file general_sam-1.0.4.post0.tar.gz.
File metadata
- Download URL: general_sam-1.0.4.post0.tar.gz
- Upload date:
- Size: 21.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2f9ac7fbc4337a982045797262f1be30bd24dea67658f24745ff44b217d6b6c2
|
|
| MD5 |
d66c2e61a6e7d16ac4d2ecff5dfd6d57
|
|
| BLAKE2b-256 |
0411cc964a53c41249fd0a5071e5ca77873a94d917be7fb9f760dea363a6f708
|
Provenance
The following attestation bundles were made for general_sam-1.0.4.post0.tar.gz:
Publisher:
ci.yml on ModelTC/general-sam-py
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
general_sam-1.0.4.post0.tar.gz -
Subject digest:
2f9ac7fbc4337a982045797262f1be30bd24dea67658f24745ff44b217d6b6c2 - Sigstore transparency entry: 2180097776
- Sigstore integration time:
-
Permalink:
ModelTC/general-sam-py@6811d425e28a71626174353961bebb86ec94633b -
Branch / Tag:
refs/tags/v1.0.4.post0 - Owner: https://github.com/ModelTC
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@6811d425e28a71626174353961bebb86ec94633b -
Trigger Event:
push
-
Statement type:
File details
Details for the file general_sam-1.0.4.post0-cp310-abi3-win_amd64.whl.
File metadata
- Download URL: general_sam-1.0.4.post0-cp310-abi3-win_amd64.whl
- Upload date:
- Size: 198.4 kB
- Tags: CPython 3.10+, Windows x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
513d1d0d428678d7c605844a2afe1da08ea5b34460b2ac7d97a6d13dc4a1dfa2
|
|
| MD5 |
4600b323ba9f80d563ad2e6da9e298d4
|
|
| BLAKE2b-256 |
459ed15754ce15505ee433e5fc5ecea7c357a77da5fe70e88bb07f854b9200d1
|
Provenance
The following attestation bundles were made for general_sam-1.0.4.post0-cp310-abi3-win_amd64.whl:
Publisher:
ci.yml on ModelTC/general-sam-py
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
general_sam-1.0.4.post0-cp310-abi3-win_amd64.whl -
Subject digest:
513d1d0d428678d7c605844a2afe1da08ea5b34460b2ac7d97a6d13dc4a1dfa2 - Sigstore transparency entry: 2180097872
- Sigstore integration time:
-
Permalink:
ModelTC/general-sam-py@6811d425e28a71626174353961bebb86ec94633b -
Branch / Tag:
refs/tags/v1.0.4.post0 - Owner: https://github.com/ModelTC
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@6811d425e28a71626174353961bebb86ec94633b -
Trigger Event:
push
-
Statement type:
File details
Details for the file general_sam-1.0.4.post0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: general_sam-1.0.4.post0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 316.5 kB
- Tags: CPython 3.10+, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5147df9c72bd5b1c149de19939502c9a212aad5de4a6c583ef2216b289e5c196
|
|
| MD5 |
903b8ecc71e4bbd7697f6c55ace32bb6
|
|
| BLAKE2b-256 |
e7b8cedf5294e293427f003b608cb4b7eeea7281dafa886b533192868ee175ba
|
Provenance
The following attestation bundles were made for general_sam-1.0.4.post0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:
Publisher:
ci.yml on ModelTC/general-sam-py
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
general_sam-1.0.4.post0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl -
Subject digest:
5147df9c72bd5b1c149de19939502c9a212aad5de4a6c583ef2216b289e5c196 - Sigstore transparency entry: 2180098096
- Sigstore integration time:
-
Permalink:
ModelTC/general-sam-py@6811d425e28a71626174353961bebb86ec94633b -
Branch / Tag:
refs/tags/v1.0.4.post0 - Owner: https://github.com/ModelTC
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@6811d425e28a71626174353961bebb86ec94633b -
Trigger Event:
push
-
Statement type:
File details
Details for the file general_sam-1.0.4.post0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.
File metadata
- Download URL: general_sam-1.0.4.post0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
- Upload date:
- Size: 311.2 kB
- Tags: CPython 3.10+, manylinux: glibc 2.17+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d66af109bca1785d1715ed9fef0b08aff9e062d9a70d95b16d6915f96d7fd280
|
|
| MD5 |
8cc6b400c31444bcd7c2316c45ba7ba2
|
|
| BLAKE2b-256 |
97e75f967e3c4b031eba333e359eaee0dd192a8ab2dbb6f9776dafd13653ecf2
|
Provenance
The following attestation bundles were made for general_sam-1.0.4.post0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:
Publisher:
ci.yml on ModelTC/general-sam-py
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
general_sam-1.0.4.post0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl -
Subject digest:
d66af109bca1785d1715ed9fef0b08aff9e062d9a70d95b16d6915f96d7fd280 - Sigstore transparency entry: 2180098163
- Sigstore integration time:
-
Permalink:
ModelTC/general-sam-py@6811d425e28a71626174353961bebb86ec94633b -
Branch / Tag:
refs/tags/v1.0.4.post0 - Owner: https://github.com/ModelTC
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@6811d425e28a71626174353961bebb86ec94633b -
Trigger Event:
push
-
Statement type:
File details
Details for the file general_sam-1.0.4.post0-cp310-abi3-macosx_11_0_arm64.whl.
File metadata
- Download URL: general_sam-1.0.4.post0-cp310-abi3-macosx_11_0_arm64.whl
- Upload date:
- Size: 290.0 kB
- Tags: CPython 3.10+, macOS 11.0+ ARM64
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7126a8f448ba09639c16bd8c1ac0a08843e37e8260a9218c073004720a3ee521
|
|
| MD5 |
c550d33b5b365b44f04f200d8a58994a
|
|
| BLAKE2b-256 |
db21a8fb040efec3e30be52339a0399160b0c9a62645caacb97d73b3b3d59592
|
Provenance
The following attestation bundles were made for general_sam-1.0.4.post0-cp310-abi3-macosx_11_0_arm64.whl:
Publisher:
ci.yml on ModelTC/general-sam-py
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
general_sam-1.0.4.post0-cp310-abi3-macosx_11_0_arm64.whl -
Subject digest:
7126a8f448ba09639c16bd8c1ac0a08843e37e8260a9218c073004720a3ee521 - Sigstore transparency entry: 2180097988
- Sigstore integration time:
-
Permalink:
ModelTC/general-sam-py@6811d425e28a71626174353961bebb86ec94633b -
Branch / Tag:
refs/tags/v1.0.4.post0 - Owner: https://github.com/ModelTC
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
ci.yml@6811d425e28a71626174353961bebb86ec94633b -
Trigger Event:
push
-
Statement type: