| 1 |
// This file is part of ICU4X. For terms of use, please see the file |
| 2 |
// called LICENSE at the top level of the ICU4X source tree |
| 3 |
// (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). |
| 4 |
|
| 5 |
use crate::provider::*; |
| 6 |
#[cfg(feature = "unstable")] |
| 7 |
use crate::scaffold::PotentiallyIllFormedUtf8; |
| 8 |
use crate::scaffold::{RuleBreakType, Utf8, Utf16}; |
| 9 |
use crate::{GraphemeClusterSegmenter, GraphemeClusterSegmenterBorrowed}; |
| 10 |
use alloc::vec::Vec; |
| 11 |
use icu_provider::prelude::*; |
| 12 |
|
| 13 |
mod dictionary; |
| 14 |
use dictionary::*; |
| 15 |
mod script; |
| 16 |
use script::*; |
| 17 |
#[cfg(feature = "lstm")] |
| 18 |
mod lstm; |
| 19 |
#[cfg(feature = "lstm")] |
| 20 |
use lstm::*; |
| 21 |
|
| 22 |
#[derive(Debug)] |
| 23 |
pub struct ComplexIterator<'data, 's, R: RuleBreakType>(ComplexIteratorInner<'data, 's, R>, usize); |
| 24 |
|
| 25 |
#[derive(Debug)] |
| 26 |
enum ComplexIteratorInner<'data, 's, R: RuleBreakType> { |
| 27 |
Dictionary(DictionaryBreakIterator<'data, 's, R>), |
| 28 |
#[cfg(feature = "lstm")] |
| 29 |
Lstm(LstmSegmenterIterator<'data, 's, R>), |
| 30 |
} |
| 31 |
|
| 32 |
impl<R: RuleBreakType> Iterator for ComplexIterator<'_, '_, R> { |
| 33 |
type Item = usize; |
| 34 |
|
| 35 |
fn next(&mut self) -> Option<Self::Item> { |
| 36 |
match self.0 { |
| 37 |
ComplexIteratorInner::Dictionary(ref mut iter) => iter.next(), |
| 38 |
#[cfg(feature = "lstm")] |
| 39 |
ComplexIteratorInner::Lstm(ref mut iter) => iter.next(), |
| 40 |
} |
| 41 |
.map(|n| n + self.1) |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
#[derive(Debug, Clone)] |
| 46 |
#[cfg_attr(feature = "lstm", expect(clippy::large_enum_variant))] |
| 47 |
enum ComplexPayload { |
| 48 |
Dict(DataPayload<UCharDictionaryBreakDataV1>), |
| 49 |
#[cfg(feature = "lstm")] |
| 50 |
Lstm(DataPayload<SegmenterLstmAutoV1>), |
| 51 |
} |
| 52 |
|
| 53 |
#[derive(Debug, Clone, Copy)] |
| 54 |
pub enum ComplexPayloadBorrowed<'data> { |
| 55 |
Dict(&'data UCharDictionaryBreakData<'data>), |
| 56 |
#[cfg(feature = "lstm")] |
| 57 |
Lstm(&'data LstmData<'data>), |
| 58 |
} |
| 59 |
|
| 60 |
impl<'data> ComplexPayloadBorrowed<'data> { |
| 61 |
pub(crate) fn segment_str<'s>( |
| 62 |
self, |
| 63 |
input: &'s str, |
| 64 |
grapheme: GraphemeClusterSegmenterBorrowed<'data>, |
| 65 |
offset: usize, |
| 66 |
) -> ComplexIterator<'data, 's, Utf8> { |
| 67 |
ComplexIterator( |
| 68 |
match self { |
| 69 |
Self::Dict(dict) => ComplexIteratorInner::Dictionary( |
| 70 |
DictionarySegmenter::new(dict, grapheme).segment_str(input), |
| 71 |
), |
| 72 |
#[cfg(feature = "lstm")] |
| 73 |
Self::Lstm(lstm) => ComplexIteratorInner::Lstm( |
| 74 |
LstmSegmenter::new(lstm, grapheme).segment_str(input), |
| 75 |
), |
| 76 |
}, |
| 77 |
offset, |
| 78 |
) |
| 79 |
} |
| 80 |
|
| 81 |
#[cfg(feature = "unstable")] |
| 82 |
pub(crate) fn segment_utf8<'s>( |
| 83 |
self, |
| 84 |
input: &'s [u8], |
| 85 |
grapheme: GraphemeClusterSegmenterBorrowed<'data>, |
| 86 |
offset: usize, |
| 87 |
) -> ComplexIterator<'data, 's, PotentiallyIllFormedUtf8> { |
| 88 |
ComplexIterator( |
| 89 |
match self { |
| 90 |
Self::Dict(dict) => ComplexIteratorInner::Dictionary( |
| 91 |
DictionarySegmenter::new(dict, grapheme).segment_utf8(input), |
| 92 |
), |
| 93 |
#[cfg(feature = "lstm")] |
| 94 |
Self::Lstm(lstm) => ComplexIteratorInner::Lstm( |
| 95 |
LstmSegmenter::new(lstm, grapheme).segment_utf8(input), |
| 96 |
), |
| 97 |
}, |
| 98 |
offset, |
| 99 |
) |
| 100 |
} |
| 101 |
|
| 102 |
pub(crate) fn segment_utf16<'s>( |
| 103 |
self, |
| 104 |
input: &'s [u16], |
| 105 |
grapheme: GraphemeClusterSegmenterBorrowed<'data>, |
| 106 |
offset: usize, |
| 107 |
) -> ComplexIterator<'data, 's, Utf16> { |
| 108 |
ComplexIterator( |
| 109 |
match self { |
| 110 |
Self::Dict(dict) => ComplexIteratorInner::Dictionary( |
| 111 |
DictionarySegmenter::new(dict, grapheme).segment_utf16(input), |
| 112 |
), |
| 113 |
#[cfg(feature = "lstm")] |
| 114 |
Self::Lstm(lstm) => ComplexIteratorInner::Lstm( |
| 115 |
LstmSegmenter::new(lstm, grapheme).segment_utf16(input), |
| 116 |
), |
| 117 |
}, |
| 118 |
offset, |
| 119 |
) |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
impl ComplexPayload { |
| 124 |
fn as_borrowed(&self) -> ComplexPayloadBorrowed<'_> { |
| 125 |
match self { |
| 126 |
ComplexPayload::Dict(dict) => ComplexPayloadBorrowed::Dict(dict.get()), |
| 127 |
#[cfg(feature = "lstm")] |
| 128 |
ComplexPayload::Lstm(lstm) => ComplexPayloadBorrowed::Lstm(lstm.get()), |
| 129 |
} |
| 130 |
} |
| 131 |
} |
| 132 |
|
| 133 |
impl ComplexPayloadBorrowed<'static> { |
| 134 |
fn static_to_owned(self) -> ComplexPayload { |
| 135 |
match self { |
| 136 |
ComplexPayloadBorrowed::Dict(dict) => { |
| 137 |
ComplexPayload::Dict(DataPayload::from_static_ref(dict)) |
| 138 |
} |
| 139 |
#[cfg(feature = "lstm")] |
| 140 |
ComplexPayloadBorrowed::Lstm(lstm) => { |
| 141 |
ComplexPayload::Lstm(DataPayload::from_static_ref(lstm)) |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
} |
| 146 |
|
| 147 |
#[derive(Debug)] |
| 148 |
pub(crate) struct ComplexPayloads { |
| 149 |
grapheme: GraphemeClusterSegmenter, |
| 150 |
my: Option<ComplexPayload>, |
| 151 |
km: Option<ComplexPayload>, |
| 152 |
lo: Option<ComplexPayload>, |
| 153 |
th: Option<ComplexPayload>, |
| 154 |
ja: Option<ComplexPayload>, |
| 155 |
} |
| 156 |
|
| 157 |
#[derive(Debug, Clone, Copy)] |
| 158 |
#[doc(hidden)] |
| 159 |
pub struct ComplexPayloadsBorrowed<'data> { |
| 160 |
pub(crate) grapheme: GraphemeClusterSegmenterBorrowed<'data>, |
| 161 |
my: Option<ComplexPayloadBorrowed<'data>>, |
| 162 |
km: Option<ComplexPayloadBorrowed<'data>>, |
| 163 |
lo: Option<ComplexPayloadBorrowed<'data>>, |
| 164 |
th: Option<ComplexPayloadBorrowed<'data>>, |
| 165 |
ja: Option<ComplexPayloadBorrowed<'data>>, |
| 166 |
} |
| 167 |
|
| 168 |
#[cfg(feature = "lstm")] |
| 169 |
const MY_LSTM: &DataMarkerAttributes = DataMarkerAttributes::from_str_or_panic("Burmese_"); |
| 170 |
#[cfg(feature = "lstm")] |
| 171 |
const KM_LSTM: &DataMarkerAttributes = DataMarkerAttributes::from_str_or_panic("Khmer_"); |
| 172 |
#[cfg(feature = "lstm")] |
| 173 |
const LO_LSTM: &DataMarkerAttributes = DataMarkerAttributes::from_str_or_panic("Lao_"); |
| 174 |
#[cfg(feature = "lstm")] |
| 175 |
const TH_LSTM: &DataMarkerAttributes = DataMarkerAttributes::from_str_or_panic("Thai_"); |
| 176 |
|
| 177 |
const MY_DICT: &DataMarkerAttributes = DataMarkerAttributes::from_str_or_panic("burmesedict"); |
| 178 |
const KM_DICT: &DataMarkerAttributes = DataMarkerAttributes::from_str_or_panic("khmerdict"); |
| 179 |
const LO_DICT: &DataMarkerAttributes = DataMarkerAttributes::from_str_or_panic("laodict"); |
| 180 |
const TH_DICT: &DataMarkerAttributes = DataMarkerAttributes::from_str_or_panic("thaidict"); |
| 181 |
const CJ_DICT: &DataMarkerAttributes = DataMarkerAttributes::from_str_or_panic("cjdict"); |
| 182 |
|
| 183 |
impl<'data> ComplexPayloadsBorrowed<'data> { |
| 184 |
pub(crate) fn select( |
| 185 |
&self, |
| 186 |
complex_script: ComplexScript, |
| 187 |
) -> Option<ComplexPayloadBorrowed<'data>> { |
| 188 |
const ERR: DataError = DataError::custom("No segmentation model for complex script"); |
| 189 |
match complex_script { |
| 190 |
ComplexScript::Myanmar => self.my.or_else(|| { |
| 191 |
ERR.with_display_context("Myanmar"); |
| 192 |
None |
| 193 |
}), |
| 194 |
ComplexScript::Khmer => self.km.or_else(|| { |
| 195 |
ERR.with_display_context("Khmer"); |
| 196 |
None |
| 197 |
}), |
| 198 |
ComplexScript::Lao => self.lo.or_else(|| { |
| 199 |
ERR.with_display_context("Lao"); |
| 200 |
None |
| 201 |
}), |
| 202 |
ComplexScript::Thai => self.th.or_else(|| { |
| 203 |
ERR.with_display_context("Thai"); |
| 204 |
None |
| 205 |
}), |
| 206 |
ComplexScript::ChineseOrJapanese => self.ja.or_else(|| { |
| 207 |
ERR.with_display_context("Chinese/Japanese"); |
| 208 |
None |
| 209 |
}), |
| 210 |
ComplexScript::None => None, |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
pub(crate) fn segment_str(&self, input: &str) -> Vec<usize> { |
| 215 |
let mut result = Vec::new(); |
| 216 |
let mut offset = 0; |
| 217 |
for (slice, complex_script) in ComplexScriptIterator::new(input) { |
| 218 |
match self.select(complex_script) { |
| 219 |
Some(d) => result.extend(d.segment_str(slice, self.grapheme, offset)), |
| 220 |
None => result.push(offset + slice.len()), |
| 221 |
} |
| 222 |
offset += slice.len(); |
| 223 |
} |
| 224 |
result |
| 225 |
} |
| 226 |
/// Return UTF-16 segment offset array using dictionary or lstm segmenter. |
| 227 |
pub(crate) fn segment_utf16(&self, input: &[u16]) -> Vec<usize> { |
| 228 |
let mut result = Vec::new(); |
| 229 |
let mut offset = 0; |
| 230 |
for (slice, complex_script) in ComplexScriptIteratorUtf16::new(input) { |
| 231 |
match self.select(complex_script) { |
| 232 |
Some(d) => result.extend(d.segment_utf16(slice, self.grapheme, offset)), |
| 233 |
None => result.push(offset + slice.len()), |
| 234 |
} |
| 235 |
offset += slice.len(); |
| 236 |
} |
| 237 |
result |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
impl ComplexPayloadsBorrowed<'static> { |
| 242 |
#[cfg(feature = "lstm")] |
| 243 |
#[cfg(feature = "compiled_data")] |
| 244 |
pub(crate) fn with_southeast_asian_lstms(&mut self) { |
| 245 |
#![expect(clippy::unwrap_used)] |
| 246 |
// try_load is infallible if the provider only returns `MissingLocale`. |
| 247 |
if self.my.is_none() { |
| 248 |
self.my = try_load_static::<SegmenterLstmAutoV1, _>(&Baked, MY_LSTM) |
| 249 |
.unwrap() |
| 250 |
.map(ComplexPayloadBorrowed::Lstm); |
| 251 |
} |
| 252 |
if self.km.is_none() { |
| 253 |
self.km = try_load_static::<SegmenterLstmAutoV1, _>(&Baked, KM_LSTM) |
| 254 |
.unwrap() |
| 255 |
.map(ComplexPayloadBorrowed::Lstm); |
| 256 |
} |
| 257 |
if self.lo.is_none() { |
| 258 |
self.lo = try_load_static::<SegmenterLstmAutoV1, _>(&Baked, LO_LSTM) |
| 259 |
.unwrap() |
| 260 |
.map(ComplexPayloadBorrowed::Lstm); |
| 261 |
} |
| 262 |
if self.th.is_none() { |
| 263 |
self.th = try_load_static::<SegmenterLstmAutoV1, _>(&Baked, TH_LSTM) |
| 264 |
.unwrap() |
| 265 |
.map(ComplexPayloadBorrowed::Lstm); |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
#[cfg(feature = "compiled_data")] |
| 270 |
pub(crate) fn with_japanese_dictionary(&mut self) { |
| 271 |
#![expect(clippy::unwrap_used)] |
| 272 |
// try_load is infallible if the provider only returns `MissingLocale`. |
| 273 |
if self.ja.is_none() { |
| 274 |
self.ja = try_load_static::<SegmenterDictionaryAutoV1, _>(&Baked, CJ_DICT) |
| 275 |
.unwrap() |
| 276 |
.map(ComplexPayloadBorrowed::Dict); |
| 277 |
} |
| 278 |
} |
| 279 |
|
| 280 |
#[cfg(feature = "compiled_data")] |
| 281 |
pub(crate) fn with_southeast_asian_dictionaries(&mut self) { |
| 282 |
#![expect(clippy::unwrap_used)] |
| 283 |
// try_load is infallible if the provider only returns `MissingLocale`. |
| 284 |
if self.my.is_none() { |
| 285 |
self.my = try_load_static::<SegmenterDictionaryExtendedV1, _>(&Baked, MY_DICT) |
| 286 |
.unwrap() |
| 287 |
.map(ComplexPayloadBorrowed::Dict); |
| 288 |
} |
| 289 |
if self.km.is_none() { |
| 290 |
self.km = try_load_static::<SegmenterDictionaryExtendedV1, _>(&Baked, KM_DICT) |
| 291 |
.unwrap() |
| 292 |
.map(ComplexPayloadBorrowed::Dict); |
| 293 |
} |
| 294 |
if self.lo.is_none() { |
| 295 |
self.lo = try_load_static::<SegmenterDictionaryExtendedV1, _>(&Baked, LO_DICT) |
| 296 |
.unwrap() |
| 297 |
.map(ComplexPayloadBorrowed::Dict); |
| 298 |
} |
| 299 |
if self.th.is_none() { |
| 300 |
self.th = try_load_static::<SegmenterDictionaryExtendedV1, _>(&Baked, TH_DICT) |
| 301 |
.unwrap() |
| 302 |
.map(ComplexPayloadBorrowed::Dict); |
| 303 |
} |
| 304 |
} |
| 305 |
|
| 306 |
#[cfg(feature = "compiled_data")] |
| 307 |
pub(crate) const fn new() -> Self { |
| 308 |
Self { |
| 309 |
grapheme: GraphemeClusterSegmenter::new(), |
| 310 |
my: None, |
| 311 |
km: None, |
| 312 |
lo: None, |
| 313 |
th: None, |
| 314 |
ja: None, |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
#[cfg(feature = "compiled_data")] |
| 319 |
#[cfg(feature = "unstable")] |
| 320 |
pub(crate) const fn new_neo() -> Self { |
| 321 |
Self { |
| 322 |
grapheme: GraphemeClusterSegmenter::new_neo(), |
| 323 |
my: None, |
| 324 |
km: None, |
| 325 |
lo: None, |
| 326 |
th: None, |
| 327 |
ja: None, |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
pub(crate) fn static_to_owned(self) -> ComplexPayloads { |
| 332 |
ComplexPayloads { |
| 333 |
grapheme: self.grapheme.static_to_owned(), |
| 334 |
my: self.my.map(ComplexPayloadBorrowed::static_to_owned), |
| 335 |
km: self.km.map(ComplexPayloadBorrowed::static_to_owned), |
| 336 |
lo: self.lo.map(ComplexPayloadBorrowed::static_to_owned), |
| 337 |
th: self.th.map(ComplexPayloadBorrowed::static_to_owned), |
| 338 |
ja: self.ja.map(ComplexPayloadBorrowed::static_to_owned), |
| 339 |
} |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
impl ComplexPayloads { |
| 344 |
pub(crate) fn as_borrowed(&self) -> ComplexPayloadsBorrowed<'_> { |
| 345 |
ComplexPayloadsBorrowed { |
| 346 |
grapheme: self.grapheme.as_borrowed(), |
| 347 |
my: self.my.as_ref().map(ComplexPayload::as_borrowed), |
| 348 |
km: self.km.as_ref().map(ComplexPayload::as_borrowed), |
| 349 |
lo: self.lo.as_ref().map(ComplexPayload::as_borrowed), |
| 350 |
th: self.th.as_ref().map(ComplexPayload::as_borrowed), |
| 351 |
ja: self.ja.as_ref().map(ComplexPayload::as_borrowed), |
| 352 |
} |
| 353 |
} |
| 354 |
|
| 355 |
#[cfg(feature = "lstm")] |
| 356 |
pub(crate) fn with_southeast_asian_lstms<D>(&mut self, provider: &D) -> Result<(), DataError> |
| 357 |
where |
| 358 |
D: DataProvider<SegmenterLstmAutoV1> + ?Sized, |
| 359 |
{ |
| 360 |
if self.my.is_none() { |
| 361 |
self.my = try_load::<SegmenterLstmAutoV1, D>(provider, MY_LSTM)? |
| 362 |
.map(DataPayload::cast) |
| 363 |
.map(ComplexPayload::Lstm); |
| 364 |
} |
| 365 |
if self.km.is_none() { |
| 366 |
self.km = try_load::<SegmenterLstmAutoV1, D>(provider, KM_LSTM)? |
| 367 |
.map(DataPayload::cast) |
| 368 |
.map(ComplexPayload::Lstm); |
| 369 |
} |
| 370 |
if self.lo.is_none() { |
| 371 |
self.lo = try_load::<SegmenterLstmAutoV1, D>(provider, LO_LSTM)? |
| 372 |
.map(DataPayload::cast) |
| 373 |
.map(ComplexPayload::Lstm); |
| 374 |
} |
| 375 |
if self.th.is_none() { |
| 376 |
self.th = try_load::<SegmenterLstmAutoV1, D>(provider, TH_LSTM)? |
| 377 |
.map(DataPayload::cast) |
| 378 |
.map(ComplexPayload::Lstm); |
| 379 |
} |
| 380 |
Ok(()) |
| 381 |
} |
| 382 |
|
| 383 |
pub(crate) fn with_japanese_dictionary<D>(&mut self, provider: &D) -> Result<(), DataError> |
| 384 |
where |
| 385 |
D: DataProvider<SegmenterDictionaryAutoV1> + ?Sized, |
| 386 |
{ |
| 387 |
self.ja = try_load::<SegmenterDictionaryAutoV1, D>(provider, CJ_DICT)? |
| 388 |
.map(DataPayload::cast) |
| 389 |
.map(ComplexPayload::Dict); |
| 390 |
Ok(()) |
| 391 |
} |
| 392 |
|
| 393 |
pub(crate) fn with_southeast_asian_dictionaries<D>( |
| 394 |
&mut self, |
| 395 |
provider: &D, |
| 396 |
) -> Result<(), DataError> |
| 397 |
where |
| 398 |
D: DataProvider<SegmenterDictionaryExtendedV1> + ?Sized, |
| 399 |
{ |
| 400 |
if self.my.is_none() { |
| 401 |
self.my = try_load::<SegmenterDictionaryExtendedV1, _>(provider, MY_DICT)? |
| 402 |
.map(DataPayload::cast) |
| 403 |
.map(ComplexPayload::Dict); |
| 404 |
} |
| 405 |
if self.km.is_none() { |
| 406 |
self.km = try_load::<SegmenterDictionaryExtendedV1, _>(provider, KM_DICT)? |
| 407 |
.map(DataPayload::cast) |
| 408 |
.map(ComplexPayload::Dict); |
| 409 |
} |
| 410 |
if self.lo.is_none() { |
| 411 |
self.lo = try_load::<SegmenterDictionaryExtendedV1, _>(provider, LO_DICT)? |
| 412 |
.map(DataPayload::cast) |
| 413 |
.map(ComplexPayload::Dict); |
| 414 |
} |
| 415 |
if self.th.is_none() { |
| 416 |
self.th = try_load::<SegmenterDictionaryExtendedV1, _>(provider, TH_DICT)? |
| 417 |
.map(DataPayload::cast) |
| 418 |
.map(ComplexPayload::Dict); |
| 419 |
} |
| 420 |
Ok(()) |
| 421 |
} |
| 422 |
|
| 423 |
pub(crate) fn try_new<D>(provider: &D) -> Result<Self, DataError> |
| 424 |
where |
| 425 |
D: DataProvider<SegmenterBreakGraphemeClusterV1> + ?Sized, |
| 426 |
{ |
| 427 |
Ok(Self { |
| 428 |
grapheme: GraphemeClusterSegmenter::try_new_unstable(provider)?, |
| 429 |
my: None, |
| 430 |
km: None, |
| 431 |
lo: None, |
| 432 |
th: None, |
| 433 |
ja: None, |
| 434 |
}) |
| 435 |
} |
| 436 |
|
| 437 |
#[cfg(feature = "unstable")] |
| 438 |
pub(crate) fn try_new_neo<D>(provider: &D) -> Result<Self, DataError> |
| 439 |
where |
| 440 |
D: DataProvider<SegmenterBreakGraphemeClusterV2> + ?Sized, |
| 441 |
{ |
| 442 |
Ok(Self { |
| 443 |
grapheme: GraphemeClusterSegmenter::try_new_neo_unstable(provider)?, |
| 444 |
my: None, |
| 445 |
km: None, |
| 446 |
lo: None, |
| 447 |
th: None, |
| 448 |
ja: None, |
| 449 |
}) |
| 450 |
} |
| 451 |
} |
| 452 |
|
| 453 |
fn try_load<M: DataMarker, P: DataProvider<M> + ?Sized>( |
| 454 |
provider: &P, |
| 455 |
model: &'static DataMarkerAttributes, |
| 456 |
) -> Result<Option<DataPayload<M>>, DataError> { |
| 457 |
provider |
| 458 |
.load(DataRequest { |
| 459 |
id: DataIdentifierBorrowed::for_marker_attributes(model), |
| 460 |
metadata: { |
| 461 |
let mut m = DataRequestMetadata::default(); |
| 462 |
m.silent = true; |
| 463 |
m.attributes_prefix_match = true; |
| 464 |
m |
| 465 |
}, |
| 466 |
}) |
| 467 |
.allow_identifier_not_found() |
| 468 |
.map(|r| r.map(|r| r.payload)) |
| 469 |
} |
| 470 |
|
| 471 |
#[cfg(feature = "compiled_data")] |
| 472 |
fn try_load_static<M: DataMarker, P: DataProvider<M> + ?Sized>( |
| 473 |
provider: &P, |
| 474 |
model: &'static DataMarkerAttributes, |
| 475 |
) -> Result<Option<&'static <M::DataStruct as yoke::Yokeable<'static>>::Output>, DataError> { |
| 476 |
provider |
| 477 |
.load(DataRequest { |
| 478 |
id: DataIdentifierBorrowed::for_marker_attributes(model), |
| 479 |
metadata: { |
| 480 |
let mut m = DataRequestMetadata::default(); |
| 481 |
m.silent = true; |
| 482 |
m.attributes_prefix_match = true; |
| 483 |
m |
| 484 |
}, |
| 485 |
}) |
| 486 |
.allow_identifier_not_found() |
| 487 |
.map(|r| r.and_then(|r| r.payload.get_static())) |
| 488 |
} |
| 489 |
|
| 490 |
#[cfg(test)] |
| 491 |
#[cfg(feature = "serde")] |
| 492 |
mod tests { |
| 493 |
use super::*; |
| 494 |
|
| 495 |
#[track_caller] |
| 496 |
fn check_complex(s: &str, expected: &[&str], segmenter: ComplexPayloadsBorrowed<'_>) { |
| 497 |
use itertools::Itertools; |
| 498 |
|
| 499 |
let segments = [0] |
| 500 |
.into_iter() |
| 501 |
.chain(segmenter.segment_str(s)) |
| 502 |
.tuple_windows() |
| 503 |
.map(|(a, b)| &s[a..b]) |
| 504 |
.collect::<Vec<_>>(); |
| 505 |
assert_eq!(segments, expected, "{s}"); |
| 506 |
|
| 507 |
// let segments = segmenter |
| 508 |
// .segment_utf8(s.as_bytes()) |
| 509 |
// .tuple_windows() |
| 510 |
// .map(|(a, b)| &s[a..b]) |
| 511 |
// .collect::<Vec<_>>(); |
| 512 |
// assert_eq!(segments, expected, "{s}"); |
| 513 |
|
| 514 |
let utf16: Vec<u16> = s.encode_utf16().collect(); |
| 515 |
let expected = expected |
| 516 |
.iter() |
| 517 |
.copied() |
| 518 |
.map(|s| s.encode_utf16().collect::<Vec<_>>()) |
| 519 |
.collect::<Vec<_>>(); |
| 520 |
let iter = [0] |
| 521 |
.into_iter() |
| 522 |
.chain(segmenter.segment_utf16(&utf16)) |
| 523 |
.tuple_windows() |
| 524 |
.map(|(a, b)| &utf16[a..b]) |
| 525 |
.collect::<Vec<_>>(); |
| 526 |
assert_eq!(iter, expected, "{s}"); |
| 527 |
} |
| 528 |
|
| 529 |
#[test] |
| 530 |
fn thai() { |
| 531 |
let mut lstm = ComplexPayloadsBorrowed::new(); |
| 532 |
lstm.with_southeast_asian_lstms(); |
| 533 |
let mut dict = ComplexPayloadsBorrowed::new(); |
| 534 |
dict.with_southeast_asian_dictionaries(); |
| 535 |
|
| 536 |
check_complex("ภาษาไทยภาษาไทย", &["ภาษา", "ไทย", "ภาษา", "ไทย"], lstm); |
| 537 |
check_complex("ภาษาไทยภาษาไทย", &["ภาษา", "ไทย", "ภาษา", "ไทย"], dict); |
| 538 |
} |
| 539 |
|
| 540 |
#[test] |
| 541 |
fn mixed() { |
| 542 |
let mut lstm = ComplexPayloadsBorrowed::new(); |
| 543 |
lstm.with_southeast_asian_lstms(); |
| 544 |
lstm.with_japanese_dictionary(); |
| 545 |
|
| 546 |
let mut dict = ComplexPayloadsBorrowed::new(); |
| 547 |
dict.with_southeast_asian_dictionaries(); |
| 548 |
dict.with_japanese_dictionary(); |
| 549 |
|
| 550 |
check_complex("ภาษาไทย龟山岛", &["ภาษา", "ไทย", "龟山岛"], lstm); |
| 551 |
check_complex("ภาษาไทย龟山岛", &["ภาษา", "ไทย", "龟山岛"], dict); |
| 552 |
|
| 553 |
check_complex( |
| 554 |
"こんにちは世界ภาษาไทย", |
| 555 |
&["こんにちは", "世界", "ภาษา", "ไทย"], |
| 556 |
lstm, |
| 557 |
); |
| 558 |
check_complex( |
| 559 |
"こんにちは世界ภาษาไทย", |
| 560 |
&["こんにちは", "世界", "ภาษา", "ไทย"], |
| 561 |
dict, |
| 562 |
); |
| 563 |
} |
| 564 |
} |