Skip to main content

Python bindings for general-sam and some utilities

Project description

general-sam-py

License

Python bindings for general-sam and some utilities.

the suffix automaton of abcbc
The suffix automaton of abcbc, image from 后缀自动机 - OI Wiki.

Usage

GeneralSAM

from general_sam import GeneralSAM


sam = GeneralSAM.construct_from_bytes(b'abcbc')

state = sam.get_root_state()
state.feed_bytes(b'cbc')
assert state.is_accepting()

state = sam.get_root_state()
state.feed_bytes(b'bcb')
assert not state.is_accepting()
from general_sam import GeneralSAM


sam = GeneralSAM.construct_from_chars('abcbc')
state = sam.get_root_state()

state.feed_chars('b')
assert not state.is_accepting()
state.feed_chars('c')
assert state.is_accepting()
state.feed_chars('bc')
assert state.is_accepting()
state.feed_chars('bc')
assert not state.is_accepting() and state.is_nil()
from general_sam import GeneralSAM, GeneralSAMState, construct_trie_from_chars


trie, _ = construct_trie_from_chars(['hello', 'Chielo'])
sam = GeneralSAM.construct_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 VocabPrefixAutomaton, CountInfo


vocab = ['歌曲', '聆听歌曲', '播放歌曲', '歌词', '查看歌词']
automaton = VocabPrefixAutomaton(vocab, bytes_or_chars='chars')

# NOTE: CountInfo is related to the sorted vocab:
_ = ['播放歌曲', '查看歌词', '歌曲', '歌词', '聆听歌曲']

# 一起 | 聆 | 听 | 歌
state = automaton.get_root_state()

# feed 歌
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
)

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) == {'歌曲', '歌词'}

# feed 听
cnt_info = automaton.prepend_feed(state, '听')
assert cnt_info is None
assert not state.is_nil()

# feed 聆
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
)

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) == {'聆听歌曲'}

# feed 一起
assert not state.is_nil()
cnt_info = automaton.prepend_feed(state, '一起')
assert state.is_nil()

# 来 | 查看 | 歌词
state = automaton.get_root_state()

# feed 歌词
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
)

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) == {'歌词'}

# feed 查看
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
)

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) == {'查看歌词'}

# feed 来
assert not state.is_nil()
cnt_info = automaton.prepend_feed(state, '来')
assert state.is_nil()

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.1.1.tar.gz (17.1 kB view hashes)

Uploaded Source

Built Distributions

general_sam-0.1.1-cp38-abi3-win_amd64.whl (182.9 kB view hashes)

Uploaded CPython 3.8+ Windows x86-64

general_sam-0.1.1-cp38-abi3-win32.whl (173.5 kB view hashes)

Uploaded CPython 3.8+ Windows x86

general_sam-0.1.1-cp38-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (1.2 MB view hashes)

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

general_sam-0.1.1-cp38-abi3-manylinux_2_17_s390x.manylinux2014_s390x.whl (1.4 MB view hashes)

Uploaded CPython 3.8+ manylinux: glibc 2.17+ s390x

general_sam-0.1.1-cp38-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (1.3 MB view hashes)

Uploaded CPython 3.8+ manylinux: glibc 2.17+ ppc64le

general_sam-0.1.1-cp38-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (1.2 MB view hashes)

Uploaded CPython 3.8+ manylinux: glibc 2.17+ ARMv7l

general_sam-0.1.1-cp38-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (1.2 MB view hashes)

Uploaded CPython 3.8+ manylinux: glibc 2.17+ ARM64

general_sam-0.1.1-cp38-abi3-manylinux_2_5_i686.manylinux1_i686.whl (1.2 MB view hashes)

Uploaded CPython 3.8+ manylinux: glibc 2.5+ i686

general_sam-0.1.1-cp38-abi3-macosx_11_0_arm64.whl (316.7 kB view hashes)

Uploaded CPython 3.8+ macOS 11.0+ ARM64

general_sam-0.1.1-cp38-abi3-macosx_10_7_x86_64.whl (331.4 kB view hashes)

Uploaded CPython 3.8+ macOS 10.7+ x86-64

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