Add optional support for serde

This commit is contained in:
Dirkjan Ochtman 2021-02-04 11:00:27 +01:00
parent bacf82c8cc
commit 5127aac1ec
2 changed files with 5 additions and 0 deletions

View File

@ -12,10 +12,12 @@ documentation = "https://docs.rs/instant-segment"
[features] [features]
__test_data = ["test-cases"] __test_data = ["test-cases"]
test-cases = [] test-cases = []
with-serde = ["serde", "ahash/serde", "smartstring/serde"]
[dependencies] [dependencies]
ahash = "0.7.0" ahash = "0.7.0"
smartstring = "0.2.5" smartstring = "0.2.5"
serde = { version = "1.0.123", features = ["derive"], optional = true }
[dev-dependencies] [dev-dependencies]
bencher = "0.1.5" bencher = "0.1.5"

View File

@ -1,6 +1,8 @@
use std::ops::{Index, Range}; use std::ops::{Index, Range};
use std::str; use std::str;
#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};
use smartstring::alias::String; use smartstring::alias::String;
#[cfg(feature = "test-cases")] #[cfg(feature = "test-cases")]
@ -9,6 +11,7 @@ pub mod test_cases;
pub mod test_data; pub mod test_data;
/// Central data structure used to calculate word probabilities /// Central data structure used to calculate word probabilities
#[cfg_attr(feature = "with-serde", derive(Deserialize, Serialize))]
pub struct Segmenter { pub struct Segmenter {
unigrams: HashMap<String, f64>, unigrams: HashMap<String, f64>,
bigrams: HashMap<(String, String), f64>, bigrams: HashMap<(String, String), f64>,