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-0.6.1.tar.gz (21.4 kB view details)

Uploaded Source

Built Distributions

general_sam-0.6.1-cp38-abi3-win_amd64.whl (216.5 kB view details)

Uploaded CPython 3.8+ Windows x86-64

general_sam-0.6.1-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.3 MB view details)

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

general_sam-0.6.1-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (1.3 MB view details)

Uploaded CPython 3.8+ manylinux: glibc 2.17+ ARMv7l

general_sam-0.6.1-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.3 MB view details)

Uploaded CPython 3.8+ manylinux: glibc 2.17+ ARM64

general_sam-0.6.1-cp38-abi3-macosx_11_0_arm64.whl (355.6 kB view details)

Uploaded CPython 3.8+ macOS 11.0+ ARM64

general_sam-0.6.1-cp38-abi3-macosx_10_7_x86_64.whl (370.0 kB view details)

Uploaded CPython 3.8+ macOS 10.7+ x86-64

File details

Details for the file general_sam-0.6.1.tar.gz.

File metadata

  • Download URL: general_sam-0.6.1.tar.gz
  • Upload date:
  • Size: 21.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: maturin/1.3.2

File hashes

Hashes for general_sam-0.6.1.tar.gz
Algorithm Hash digest
SHA256 f16059cf63a3f84d14ca901f46a248406fa2437bcdbd9f653abdd56992df368a
MD5 d4db478775c8b38f187aadfd5751a6fd
BLAKE2b-256 d880d10efd567e741877eeec345779759dc099362f62b078cda2a2edea68b399

See more details on using hashes here.

File details

Details for the file general_sam-0.6.1-cp38-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for general_sam-0.6.1-cp38-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 a717f13282a151d35ccab9058683848ab83c26b9df7bb764b64f423943777022
MD5 486ae6bdbc18520ee884de41ee84424a
BLAKE2b-256 152aeef76e2f5ed32885eab2a091911c929b9454e06da7146ac4f76a211f2bd9

See more details on using hashes here.

File details

Details for the file general_sam-0.6.1-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for general_sam-0.6.1-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 68688fe20773526922a7d930c8c1afaa5510a4638d7a81b55161823729e4610a
MD5 fcdff95c2f9ad2de36a2e7515c3d6014
BLAKE2b-256 4eec65182cf102e30b04df7fc928b9b86c8d34f8d33f1d75501c46ae1bb399fe

See more details on using hashes here.

File details

Details for the file general_sam-0.6.1-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl.

File metadata

File hashes

Hashes for general_sam-0.6.1-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 46df65112d06234fcef32e92cd52018f9f776740739019e37b23fc396e1c2d82
MD5 296b71f1b889938f6136947f328f09ab
BLAKE2b-256 3a3798ec7f86a6bda831cc01c1df3b1b2e9c85c46371dc460ae86a12a9ad3083

See more details on using hashes here.

File details

Details for the file general_sam-0.6.1-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for general_sam-0.6.1-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 5e6d035c7269dcb500a6744610b0a95fab123e26f96b526a2875ba11a855af3d
MD5 55eb91a186c28e75a58117894f8a4e68
BLAKE2b-256 54b5906a0a822eb27bcde6ae0ef8f7fd359251c15dfa164af7ca99a0a53f0ef7

See more details on using hashes here.

File details

Details for the file general_sam-0.6.1-cp38-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for general_sam-0.6.1-cp38-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 edb340f9e5f94cc8c7239c586d352ec37489b68eb5e88f5ed3ab10e7b039a33c
MD5 2f9ea80cad315ab14b218299cb9057e9
BLAKE2b-256 91e6c6af0881a343e8c914fc8b3e29a5885589a16c72bebc285b382e3ec2510f

See more details on using hashes here.

File details

Details for the file general_sam-0.6.1-cp38-abi3-macosx_10_7_x86_64.whl.

File metadata

File hashes

Hashes for general_sam-0.6.1-cp38-abi3-macosx_10_7_x86_64.whl
Algorithm Hash digest
SHA256 17b3df39b68171c6ce24e6cc179c64ffa3234c79f667eadc412021ac3137d7a1
MD5 a3253e57881580c4ad02fcc2a9165d43
BLAKE2b-256 94f8248be779051336d34727d514d16b71f0b8661f8308f63df13131c3ed72d7

See more details on using hashes here.

Supported by

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