Skip to main content

Python bindings for general-sam and some utilities

Project description

general-sam-py

PyPI version License Build status

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

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

general_sam-1.0.5.dev3.tar.gz (21.1 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

general_sam-1.0.5.dev3-cp310-abi3-win_amd64.whl (198.5 kB view details)

Uploaded CPython 3.10+Windows x86-64

general_sam-1.0.5.dev3-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (316.4 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ x86-64

general_sam-1.0.5.dev3-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (311.1 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

general_sam-1.0.5.dev3-cp310-abi3-macosx_11_0_arm64.whl (289.9 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

File details

Details for the file general_sam-1.0.5.dev3.tar.gz.

File metadata

  • Download URL: general_sam-1.0.5.dev3.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

Hashes for general_sam-1.0.5.dev3.tar.gz
Algorithm Hash digest
SHA256 280d1fc7ad3cea950122dd2407e297f82d667a75846724bd0fe0588ae4988102
MD5 84085f925bf270e69b7fb0e4bc8cdfe0
BLAKE2b-256 7d712effb7db0c61f7d63d9235633249e35ee8974595e9a3f25a4a7ae86631a9

See more details on using hashes here.

Provenance

The following attestation bundles were made for general_sam-1.0.5.dev3.tar.gz:

Publisher: ci.yml on ModelTC/general-sam-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file general_sam-1.0.5.dev3-cp310-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for general_sam-1.0.5.dev3-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 7f46e0909dfdbc937b4c8ff84072dd21eb970ea121d0be1941328e3492c8a865
MD5 665d5385212022ab0db895a145264beb
BLAKE2b-256 7d118ed95d5e500af4f15465ba03e3f5ead107cb7a9a13d3078fc3e171a97e11

See more details on using hashes here.

Provenance

The following attestation bundles were made for general_sam-1.0.5.dev3-cp310-abi3-win_amd64.whl:

Publisher: ci.yml on ModelTC/general-sam-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file general_sam-1.0.5.dev3-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for general_sam-1.0.5.dev3-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 73899330ac24d8f1cb136f1c987e4388cc7bf42218c639c104fbe06f1c15f5bf
MD5 8d6b8f61418cfe7b4bc865048238aa59
BLAKE2b-256 2a00978da704cffe0bc08ebad3d21ae1d01cfd49daf21748f636c5b32ade3f01

See more details on using hashes here.

Provenance

The following attestation bundles were made for general_sam-1.0.5.dev3-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: ci.yml on ModelTC/general-sam-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file general_sam-1.0.5.dev3-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for general_sam-1.0.5.dev3-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 3f982057b345a6aa871d4f5f7a464f87060eb8be5d6ecb56bbf468843fa24217
MD5 70bfe20939f2a5ad8e3c5d72c30b1f8a
BLAKE2b-256 524a28c204874901119ba4574302d03ddfc7493c56f3c64e66784e5cd13c8763

See more details on using hashes here.

Provenance

The following attestation bundles were made for general_sam-1.0.5.dev3-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: ci.yml on ModelTC/general-sam-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file general_sam-1.0.5.dev3-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for general_sam-1.0.5.dev3-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8499cebcc1694a2b6b1bd1dbada9e56fca316918c252342b5b227a9fd046f842
MD5 6de3e842e862e364f2b489bfd8fde665
BLAKE2b-256 9e15c96c9ec85f6255afe1500a413f86067f35c8eda64c1aec3d834b3030b4ed

See more details on using hashes here.

Provenance

The following attestation bundles were made for general_sam-1.0.5.dev3-cp310-abi3-macosx_11_0_arm64.whl:

Publisher: ci.yml on ModelTC/general-sam-py

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page