|
| 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 std::collections::{BTreeMap, BTreeSet}; |
| 6 | + |
| 7 | +use icu_experimental::displaynames::provider::RegionDisplayNamesV1; |
| 8 | +use icu_provider_source::SourceDataProvider; |
| 9 | +use icu_provider::prelude::*; |
| 10 | +use icu::locale::{fallback::{LocaleFallbackConfig, LocaleFallbackPriority, LocaleFallbacker}, locale}; |
| 11 | +use ndarray::{Array2, Axis}; |
| 12 | +use tinystr::TinyAsciiStr; |
| 13 | + |
| 14 | +#[test] |
| 15 | +fn dnametest() { |
| 16 | + let provider = SourceDataProvider::new(); |
| 17 | + |
| 18 | + let locales: BTreeMap<DataIdentifierCow<'_>, usize> = IterableDataProvider::<RegionDisplayNamesV1>::iter_ids(&provider).unwrap().into_iter().enumerate().map(|(i, v)| (v, i)).collect(); |
| 19 | + |
| 20 | + let payloads: BTreeMap<DataIdentifierCow, DataPayload<RegionDisplayNamesV1>> = locales.keys().map(|locale| { |
| 21 | + let payload = provider.load(DataRequest { |
| 22 | + id: locale.as_borrowed(), |
| 23 | + ..Default::default() |
| 24 | + }) |
| 25 | + .unwrap() |
| 26 | + .payload; |
| 27 | + (locale.clone(), payload) |
| 28 | + }).collect(); |
| 29 | + |
| 30 | + let en_names = payloads.get(&DataIdentifierCow::from_locale(locale!("en").into())).unwrap(); |
| 31 | + |
| 32 | + let regions = en_names.get().names.iter_keys().map(|s| s.try_into_tinystr().unwrap()).collect::<BTreeSet<TinyAsciiStr<3>>>(); |
| 33 | + |
| 34 | + let mut dense_matrix = Array2::<Option<&str>>::default((locales.len(), regions.len())); |
| 35 | + |
| 36 | + for (i, (_locale, payload)) in payloads.iter().enumerate() { |
| 37 | + for (j, region) in regions.iter().enumerate() { |
| 38 | + dense_matrix[(i, j)] = payload.get().names.get(®ion.to_unvalidated()); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + let fallbacker = LocaleFallbacker::try_new_unstable(&provider).unwrap(); |
| 43 | + let mut config = LocaleFallbackConfig::default(); |
| 44 | + config.priority = LocaleFallbackPriority::Script; |
| 45 | + let fallbacker = fallbacker.for_config(config); |
| 46 | + |
| 47 | + for (i, (locale, _payload)) in payloads.iter().enumerate() { |
| 48 | + 'j: for (j, _region) in regions.iter().enumerate() { |
| 49 | + let Some(value) = dense_matrix[(i, j)] else { |
| 50 | + continue |
| 51 | + }; |
| 52 | + let mut fallback_iterator = fallbacker.fallback_for(locale.locale); |
| 53 | + loop { |
| 54 | + fallback_iterator.step(); |
| 55 | + let parent_locale = *fallback_iterator.get(); |
| 56 | + if parent_locale.is_unknown() { |
| 57 | + break; |
| 58 | + } |
| 59 | + if let Some(k) = locales.get(&DataIdentifierCow::from_locale(parent_locale)) { |
| 60 | + if let Some(parent_value) = dense_matrix[(*k, j)] { |
| 61 | + if parent_value == value { |
| 62 | + dense_matrix[(i, j)] = None; |
| 63 | + } |
| 64 | + continue 'j; |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + let large_small = dense_matrix.map_axis(Axis(1), |values| values.iter().filter(|v| v.is_some()).count()); |
| 72 | + |
| 73 | + for (i, locale) in locales.keys().enumerate() { |
| 74 | + println!("{locale:<3}: {}", large_small[i]); |
| 75 | + } |
| 76 | +} |
0 commit comments