#![allow(clippy::exhaustive_structs, clippy::exhaustive_enums)]
mod lstm;
pub use lstm::*;
use crate::WordType;
use icu_collections::codepointtrie::CodePointTrie;
use icu_provider::prelude::*;
use zerovec::ZeroVec;
#[cfg(feature = "compiled_data")]
#[derive(Debug)]
pub struct Baked;
#[cfg(feature = "compiled_data")]
#[allow(unused_imports)]
const _: () = {
use icu_segmenter_data::*;
pub mod icu {
pub use crate as segmenter;
pub use icu_collections as collections;
pub use icu_segmenter_data::icu_locale as locale;
}
make_provider!(Baked);
impl_dictionary_for_word_only_auto_v1_marker!(Baked);
impl_dictionary_for_word_line_extended_v1_marker!(Baked);
impl_grapheme_cluster_break_data_v2_marker!(Baked);
impl_line_break_data_v2_marker!(Baked);
#[cfg(feature = "lstm")]
impl_lstm_for_word_line_auto_v1_marker!(Baked);
impl_sentence_break_data_override_v1_marker!(Baked);
impl_sentence_break_data_v2_marker!(Baked);
impl_word_break_data_override_v1_marker!(Baked);
impl_word_break_data_v2_marker!(Baked);
};
#[cfg(feature = "datagen")]
pub const MARKERS: &[DataMarkerInfo] = &[
DictionaryForWordLineExtendedV1Marker::INFO,
DictionaryForWordOnlyAutoV1Marker::INFO,
GraphemeClusterBreakDataV2Marker::INFO,
LineBreakDataV2Marker::INFO,
LstmForWordLineAutoV1Marker::INFO,
SentenceBreakDataOverrideV1Marker::INFO,
SentenceBreakDataV2Marker::INFO,
WordBreakDataOverrideV1Marker::INFO,
WordBreakDataV2Marker::INFO,
];
#[icu_provider::data_struct(
marker(LineBreakDataV2Marker, "segmenter/line@2", singleton),
marker(WordBreakDataV2Marker, "segmenter/word@2", singleton),
marker(GraphemeClusterBreakDataV2Marker, "segmenter/grapheme@2", singleton),
marker(SentenceBreakDataV2Marker, "segmenter/sentence@2", singleton)
)]
#[derive(Debug, PartialEq, Clone)]
#[cfg_attr(feature = "datagen", derive(serde::Serialize, databake::Bake))]
#[cfg_attr(feature = "datagen", databake(path = icu_segmenter::provider))]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
pub struct RuleBreakDataV2<'data> {
#[cfg_attr(feature = "serde", serde(borrow))]
pub property_table: CodePointTrie<'data, u8>,
#[cfg_attr(feature = "serde", serde(borrow))]
pub break_state_table: ZeroVec<'data, BreakState>,
#[cfg_attr(feature = "serde", serde(borrow, rename = "rule_status_table"))]
pub word_type_table: ZeroVec<'data, WordType>,
pub property_count: u8,
pub last_codepoint_property: u8,
pub sot_property: u8,
pub eot_property: u8,
pub complex_property: u8,
}
#[icu_provider::data_struct(
marker(
DictionaryForWordOnlyAutoV1Marker,
"segmenter/dictionary/w_auto@1",
attributes_domain = "segmenter"
),
marker(
DictionaryForWordLineExtendedV1Marker,
"segmenter/dictionary/wl_ext@1",
attributes_domain = "segmenter"
)
)]
#[derive(Debug, PartialEq, Clone)]
#[cfg_attr(feature = "datagen", derive(serde::Serialize, databake::Bake))]
#[cfg_attr(feature = "datagen", databake(path = icu_segmenter::provider))]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
pub struct UCharDictionaryBreakDataV1<'data> {
#[cfg_attr(feature = "serde", serde(borrow))]
pub trie_data: ZeroVec<'data, u16>,
}
pub(crate) struct UCharDictionaryBreakDataV1Marker;
impl DynamicDataMarker for UCharDictionaryBreakDataV1Marker {
type DataStruct = UCharDictionaryBreakDataV1<'static>;
}
#[icu_provider::data_struct(
marker(SentenceBreakDataOverrideV1Marker, "segmenter/sentence/override@1",),
marker(WordBreakDataOverrideV1Marker, "segmenter/word/override@1")
)]
#[derive(Debug, PartialEq, Clone)]
#[cfg_attr(
feature = "datagen",
derive(serde::Serialize,databake::Bake),
databake(path = icu_segmenter::provider),
)]
#[cfg_attr(feature = "serde", derive(serde::Deserialize))]
pub struct RuleBreakDataOverrideV1<'data> {
#[cfg_attr(feature = "serde", serde(borrow))]
pub property_table_override: CodePointTrie<'data, u8>,
}
#[derive(Clone, Copy, PartialEq, Debug)]
#[cfg_attr(feature = "datagen", derive(databake::Bake))]
#[cfg_attr(feature = "datagen", databake(path = icu_segmenter::provider))]
pub enum BreakState {
Break,
Keep,
NoMatch,
Intermediate(u8),
Index(u8),
}
#[cfg(feature = "datagen")]
impl serde::Serialize for BreakState {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
if serializer.is_human_readable() {
i8::from_le_bytes([zerovec::ule::AsULE::to_unaligned(*self)]).serialize(serializer)
} else {
zerovec::ule::AsULE::to_unaligned(*self).serialize(serializer)
}
}
}
#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for BreakState {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
if deserializer.is_human_readable() {
Ok(zerovec::ule::AsULE::from_unaligned(
i8::deserialize(deserializer)?.to_le_bytes()[0],
))
} else {
u8::deserialize(deserializer).map(zerovec::ule::AsULE::from_unaligned)
}
}
}
impl zerovec::ule::AsULE for BreakState {
type ULE = u8;
fn to_unaligned(self) -> Self::ULE {
match self {
BreakState::Break => 253,
BreakState::Keep => 255,
BreakState::NoMatch => 254,
BreakState::Intermediate(i) => i + 120,
BreakState::Index(i) => i,
}
}
fn from_unaligned(unaligned: Self::ULE) -> Self {
match unaligned {
253 => BreakState::Break,
255 => BreakState::Keep,
254 => BreakState::NoMatch,
i if (120..253).contains(&i) => BreakState::Intermediate(i - 120),
i => BreakState::Index(i),
}
}
}
#[cfg(feature = "datagen")]
impl serde::Serialize for WordType {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
if serializer.is_human_readable() {
(*self as u8).serialize(serializer)
} else {
unreachable!("only used as ULE")
}
}
}
#[cfg(feature = "datagen")]
impl databake::Bake for WordType {
fn bake(&self, _crate_env: &databake::CrateEnv) -> databake::TokenStream {
unreachable!("only used as ULE")
}
}
#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for WordType {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
if deserializer.is_human_readable() {
use serde::de::Error;
match u8::deserialize(deserializer) {
Ok(0) => Ok(WordType::None),
Ok(1) => Ok(WordType::Number),
Ok(2) => Ok(WordType::Letter),
Ok(_) => Err(D::Error::custom("invalid value")),
Err(e) => Err(e),
}
} else {
unreachable!("only used as ULE")
}
}
}