Skip to content

Commit 05ceb49

Browse files
committed
feat: update general-sam to 0.5.2
1 parent 8ab4b9f commit 05ceb49

10 files changed

Lines changed: 54 additions & 48 deletions

File tree

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "general-sam-py"
3-
version = "0.4.2"
3+
version = "0.5.2-dev"
44
edition = "2021"
55
license = "MIT OR Apache-2.0"
66
description = "Python bindings for general-sam and some utilities"
@@ -14,7 +14,7 @@ name = "general_sam"
1414
crate-type = ["cdylib"]
1515

1616
[dependencies]
17-
general-sam = { version = "0.4.2", features = ["all"] }
17+
general-sam = { version = "0.5.2", features = ["all"] }
1818
pyo3 = { version = "0.20.0", features = [
1919
"extension-module",
2020
"abi3-py38",

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ pip install general-sam
4545
from general_sam import GeneralSAM
4646

4747

48-
sam = GeneralSAM.construct_from_bytes(b'abcbc')
48+
sam = GeneralSAM.from_bytes(b'abcbc')
4949

5050
# "cbc" is a suffix of "abcbc"
5151
state = sam.get_root_state()
@@ -62,7 +62,7 @@ assert not state.is_accepting()
6262
from general_sam import GeneralSAM
6363

6464

65-
sam = GeneralSAM.construct_from_chars('abcbc')
65+
sam = GeneralSAM.from_chars('abcbc')
6666
state = sam.get_root_state()
6767

6868
# "b" is not a suffix but at least a substring of "abcbc"
@@ -83,11 +83,11 @@ assert not state.is_accepting() and state.is_nil()
8383
```
8484

8585
```python
86-
from general_sam import GeneralSAM, GeneralSAMState, construct_trie_from_chars
86+
from general_sam import GeneralSAM, GeneralSAMState, build_trie_from_chars
8787

8888

89-
trie, _ = construct_trie_from_chars(['hello', 'Chielo'])
90-
sam = GeneralSAM.construct_from_trie(trie)
89+
trie, _ = build_trie_from_chars(['hello', 'Chielo'])
90+
sam = GeneralSAM.from_trie(trie)
9191

9292
def fetch_state(s: str) -> GeneralSAMState:
9393
state = sam.get_root_state()

general_sam/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
from .trie_utils import (
88
CountInfo,
99
SortResult,
10-
construct_trie_from_bytes,
11-
construct_trie_from_chars,
10+
build_trie_from_bytes,
11+
build_trie_from_chars,
1212
sort_bytes,
1313
sort_chars,
1414
sort_seq_via_trie,
@@ -25,8 +25,8 @@
2525
'TrieNode',
2626
'CountInfo',
2727
'SortResult',
28-
'construct_trie_from_chars',
29-
'construct_trie_from_bytes',
28+
'build_trie_from_chars',
29+
'build_trie_from_bytes',
3030
'sort_chars',
3131
'sort_bytes',
3232
'sort_seq_via_trie',

general_sam/general_sam.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,11 @@ class GeneralSAMState:
7272

7373
class GeneralSAM:
7474
@staticmethod
75-
def construct_from_chars(s: str) -> 'GeneralSAM': ...
75+
def from_chars(s: str) -> 'GeneralSAM': ...
7676
@staticmethod
77-
def construct_from_bytes(s: bytes) -> 'GeneralSAM': ...
77+
def from_bytes(s: bytes) -> 'GeneralSAM': ...
7878
@staticmethod
79-
def construct_from_trie(trie: Trie) -> 'GeneralSAM': ...
79+
def from_trie(trie: Trie) -> 'GeneralSAM': ...
8080
def is_in_str(self) -> bool: ...
8181
def is_in_bytes(self) -> bool: ...
8282
def num_of_nodes(self) -> int: ...

general_sam/trie_utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44
from .general_sam import Trie
55

66

7-
def construct_trie_from_chars(
7+
def build_trie_from_chars(
88
strings: Collection[str],
99
) -> Tuple[Trie, Sequence[int]]:
1010
trie = Trie.in_chars()
1111
node_ids = [trie.insert_chars(s) for s in strings]
1212
return trie, node_ids
1313

1414

15-
def construct_trie_from_bytes(
15+
def build_trie_from_bytes(
1616
strings: Collection[bytes],
1717
) -> Tuple[Trie, Sequence[int]]:
1818
trie = Trie.in_bytes()
@@ -38,12 +38,12 @@ class SortResult:
3838

3939

4040
def sort_chars(strings: Collection[str]) -> SortResult:
41-
trie, node_ids = construct_trie_from_chars(strings)
41+
trie, node_ids = build_trie_from_chars(strings)
4242
return sort_seq_via_trie(trie, node_ids)
4343

4444

4545
def sort_bytes(strings: Collection[bytes]) -> SortResult:
46-
trie, node_ids = construct_trie_from_bytes(strings)
46+
trie, node_ids = build_trie_from_bytes(strings)
4747
return sort_seq_via_trie(trie, node_ids)
4848

4949

general_sam/vocab_prefix.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
from .trie_utils import (
1616
CountInfo,
1717
SortResult,
18-
construct_trie_from_bytes,
19-
construct_trie_from_chars,
18+
build_trie_from_bytes,
19+
build_trie_from_chars,
2020
sort_bytes,
2121
sort_chars,
2222
)
@@ -53,17 +53,17 @@ def __init__(
5353

5454
self.vocab_rev: Sequence[Union[str, bytes]] = list(s[::-1] for s in vocab)
5555

56-
sort_seq, construct_trie = {
57-
VocabPrefixBytesOrChars.BYTES: (sort_bytes, construct_trie_from_bytes),
58-
VocabPrefixBytesOrChars.CHARS: (sort_chars, construct_trie_from_chars),
56+
sort_seq, trie_builder = {
57+
VocabPrefixBytesOrChars.BYTES: (sort_bytes, build_trie_from_bytes),
58+
VocabPrefixBytesOrChars.CHARS: (sort_chars, build_trie_from_chars),
5959
}[self.bytes_or_chars]
6060
self.vocab_sort_res = cast(SortResult, sort_seq(self.vocab))
6161
self.trie_rev, self.trie_rev_node_ids = cast(
6262
Tuple[Trie, Sequence[int]],
63-
construct_trie(self.vocab_rev),
63+
trie_builder(self.vocab_rev),
6464
)
6565

66-
self.sam_rev = GeneralSAM.construct_from_trie(self.trie_rev)
66+
self.sam_rev = GeneralSAM.from_trie(self.trie_rev)
6767
self._gen_cnt_info_in_sam()
6868

6969
@property

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "maturin"
44

55
[project]
66
name = "general_sam"
7-
requires-python = ">=3.7"
7+
requires-python = ">=3.8"
88
classifiers = [
99
"Programming Language :: Rust",
1010
"Programming Language :: Python :: Implementation :: CPython",

src/sam.rs

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@ extern crate general_sam as general_sam_rs;
33
use crate::trie::Trie;
44
use crate::utils::{char_or_byte_type, for_both, ByteSide, CharSide};
55

6-
use general_sam_rs::{sam as sam_rs, trie as trie_rs, trie_alike::TravelEvent};
6+
use general_sam_rs::{
7+
sam as sam_rs, trie as trie_rs, BTreeTransTable, BoxBisectTable, TransitionTable, TravelEvent,
8+
};
79
use pyo3::{prelude::*, types::PyDict};
810
use std::{str::from_utf8, sync::Arc};
911

10-
type RustGeneralSAM = char_or_byte_type!(sam_rs::GeneralSAM);
11-
type RustGeneralSAMState<'s> = char_or_byte_type!(sam_rs::GeneralSAMState; 's);
12+
type RustBoxBisectGeneralSAM<T> = sam_rs::GeneralSAM<BoxBisectTable<T>>;
13+
type RustBoxBisectGeneralSAMState<'s, T> = sam_rs::GeneralSAMState<'s, BoxBisectTable<T>>;
14+
type RustGeneralSAM = char_or_byte_type!(RustBoxBisectGeneralSAM);
15+
type RustGeneralSAMState<'s> = char_or_byte_type!(RustBoxBisectGeneralSAMState; 's);
1216

1317
#[pyclass]
1418
pub struct GeneralSAM(pub Arc<RustGeneralSAM>);
@@ -56,7 +60,7 @@ impl GeneralSAMState {
5660
for_both!(self.get_state().as_ref(), state => {
5761
Python::with_gil(|py| {
5862
if let Some(node) = state.get_node() {
59-
node.get_trans().clone().into_py(py)
63+
BTreeTransTable::from_kv_iter(node.get_trans().iter()).into_py(py)
6064
} else {
6165
PyDict::new(py).into_py(py)
6266
}
@@ -215,27 +219,30 @@ impl GeneralSAMState {
215219
#[pymethods]
216220
impl GeneralSAM {
217221
#[staticmethod]
218-
pub fn construct_from_chars(s: &str) -> Self {
222+
pub fn from_chars(s: &str) -> Self {
219223
GeneralSAM(Arc::new(CharSide(
220-
sam_rs::GeneralSAM::construct_from_chars(s.chars()),
224+
sam_rs::GeneralSAM::<BTreeTransTable<_>>::from_chars(s.chars())
225+
.alter_trans_table_into(),
221226
)))
222227
}
223228

224229
#[staticmethod]
225-
pub fn construct_from_bytes(s: &[u8]) -> Self {
230+
pub fn from_bytes(s: &[u8]) -> Self {
226231
GeneralSAM(Arc::new(ByteSide(
227-
sam_rs::GeneralSAM::construct_from_bytes(s),
232+
sam_rs::GeneralSAM::<BTreeTransTable<_>>::from_bytes(s).alter_trans_table_into(),
228233
)))
229234
}
230235

231236
#[staticmethod]
232-
pub fn construct_from_trie(trie: &Trie) -> Self {
237+
pub fn from_trie(trie: &Trie) -> Self {
233238
match trie.0.as_ref() {
234239
CharSide(trie_chars) => GeneralSAM(Arc::new(CharSide(
235-
sam_rs::GeneralSAM::construct_from_trie(trie_chars.get_root_state()),
240+
sam_rs::GeneralSAM::<BTreeTransTable<_>>::from_trie(trie_chars.get_root_state())
241+
.alter_trans_table_into(),
236242
))),
237243
ByteSide(trie_bytes) => GeneralSAM(Arc::new(ByteSide(
238-
sam_rs::GeneralSAM::construct_from_trie(trie_bytes.get_root_state()),
244+
sam_rs::GeneralSAM::<BTreeTransTable<_>>::from_trie(trie_bytes.get_root_state())
245+
.alter_trans_table_into(),
239246
))),
240247
}
241248
}

src/trie.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@ extern crate general_sam as general_sam_rs;
22

33
use crate::utils::{char_or_byte_type, for_both, ByteSide, CharSide};
44

5-
use general_sam_rs::{
6-
trie as trie_rs,
7-
trie_alike::{TravelEvent, TrieNodeAlike},
8-
};
5+
use general_sam_rs::{trie as trie_rs, BTreeTransTable, TravelEvent, TrieNodeAlike};
96
use pyo3::prelude::*;
107
use std::{convert::Infallible, str::from_utf8};
118

12-
type RustTrie = char_or_byte_type!(trie_rs::Trie);
13-
type RustTrieNode = char_or_byte_type!(trie_rs::TrieNode);
9+
type RustBTreeTrie<T> = trie_rs::Trie<BTreeTransTable<T>>;
10+
type RustBTreeTrieNode<T> = trie_rs::TrieNode<BTreeTransTable<T>>;
11+
type RustTrie = char_or_byte_type!(RustBTreeTrie);
12+
type RustTrieNode = char_or_byte_type!(RustBTreeTrieNode);
1413

1514
#[pyclass]
1615
pub struct Trie(pub RustTrie);

tests/test_general_sam.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
from general_sam import GeneralSAM, GeneralSAMState, construct_trie_from_chars
1+
from general_sam import GeneralSAM, GeneralSAMState, build_trie_from_chars
22

33

44
def test_bytes_abcbc():
5-
sam = GeneralSAM.construct_from_bytes(b'abcbc')
5+
sam = GeneralSAM.from_bytes(b'abcbc')
66

77
state = sam.get_root_state()
88
state.feed_bytes(b'cbc')
@@ -14,7 +14,7 @@ def test_bytes_abcbc():
1414

1515

1616
def test_chars_abcbc():
17-
sam = GeneralSAM.construct_from_chars('abcbc')
17+
sam = GeneralSAM.from_chars('abcbc')
1818
state = sam.get_root_state()
1919

2020
state.feed_chars('b')
@@ -28,8 +28,8 @@ def test_chars_abcbc():
2828

2929

3030
def test_simple_sam_from_trie():
31-
trie, _ = construct_trie_from_chars(['hello', 'Chielo'])
32-
sam = GeneralSAM.construct_from_trie(trie)
31+
trie, _ = build_trie_from_chars(['hello', 'Chielo'])
32+
sam = GeneralSAM.from_trie(trie)
3333

3434
def fetch_state(s: str) -> GeneralSAMState:
3535
state = sam.get_root_state()

0 commit comments

Comments
 (0)