Skip to content

Commit 8a9be28

Browse files
Added pickling support to lexicons
1 parent 2c890ae commit 8a9be28

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

python/tests/test_mg.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import pytest
2+
import pickle
23

34
from python_mg import Lexicon, Continuation
45

@@ -13,6 +14,12 @@ def test_lexicon():
1314
)
1415

1516

17+
def test_pickling():
18+
x = Lexicon("a::b= a\nb::b")
19+
x_pickle = pickle.dumps(x)
20+
assert pickle.loads(x_pickle) == x
21+
22+
1623
def test_memory_load():
1724
grammar = Lexicon("a::b= c= +a +e C\nb::b -a\nc::c -e")
1825
parse = grammar.parse("c b a", "C")[0]

src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ impl PySyntacticStructure {
117117
}
118118
}
119119

120-
#[pyclass(name = "Lexicon", str, eq, frozen)]
120+
#[pyclass(name = "Lexicon", str, eq, frozen, module = "python_mg")]
121121
#[derive(Debug, Clone, Eq, PartialEq)]
122122
struct PyLexicon {
123123
lexicon: Lexicon<String, String>,
@@ -332,6 +332,10 @@ impl PyLexicon {
332332

333333
#[pymethods]
334334
impl PyLexicon {
335+
fn __getnewargs__(&self) -> (String,) {
336+
(self.lexicon.to_string(),)
337+
}
338+
335339
fn mdl(&self, n_phonemes: u16) -> PyResult<f64> {
336340
Ok(self.lexicon.mdl_score(n_phonemes).map_err(|e| anyhow!(e))?)
337341
}

0 commit comments

Comments
 (0)