-
Notifications
You must be signed in to change notification settings - Fork 263
Data struct with codepoint builder for radicals #7805
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| // This file is part of ICU4X. For terms of use, please see the file | ||
| // called LICENSE at the top level of the ICU4X source tree | ||
| // (online at: https://github.com/unicode-org/icu4x/blob/main/LICENSE ). | ||
|
|
||
| //! Data provider struct definitions for radicals. | ||
|
|
||
| use icu_collections::codepointtrie::CodePointTrie; | ||
| use icu_provider::prelude::*; | ||
|
|
||
| /// Data for Unihan IRG sources (Radicals). | ||
| #[derive(Debug, PartialEq, Clone, yoke::Yokeable, zerofrom::ZeroFrom)] | ||
| #[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 UnihanIrgData<'data> { | ||
| /// Trie mapping code points to their IRG source radical ID (u8). | ||
| #[cfg_attr(feature = "serde", serde(borrow))] | ||
| pub trie: CodePointTrie<'data, u8>, | ||
| } | ||
|
|
||
| icu_provider::data_struct!( | ||
| UnihanIrgData<'_>, | ||
| #[cfg(feature = "datagen")] | ||
| ); | ||
|
|
||
| icu_provider::data_marker!( | ||
| /// `SegmenterUnihanRadicalV1` | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @robertbastian commented on the old PR about these docs. Please write what the marker represents. |
||
| SegmenterUnihanRadicalV1, | ||
| "segmenter/unihan/radical/v1", | ||
| UnihanIrgData<'static>, | ||
| is_singleton = true | ||
| ); | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,59 +4,38 @@ | |
|
|
||
| #![allow(dead_code)] | ||
|
|
||
| use icu_segmenter::provider::{Baked, UnihanIrgData}; | ||
| use std::collections::HashMap; | ||
|
|
||
| fn load_irg_from_baked() -> &'static UnihanIrgData<'static> { | ||
| Baked::SINGLETON_SEGMENTER_UNIHAN_RADICAL_V1 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. inline. you can even inline this all the way into |
||
| } | ||
|
|
||
| static MODEL_FOR_TEST: &str = include_str!("model.json"); | ||
| static MODEL_FOR_TEST_THAI: &str = include_str!("model_thai.json"); | ||
|
|
||
| static CODEPOINTS: &[u16] = &[ | ||
| 20008, 20022, 20031, 20057, 20101, 20108, 20128, 20154, 20799, 20837, 20843, 20866, 20886, | ||
| 20907, 20960, 20981, 20992, 21147, 21241, 21269, 21274, 21304, 21313, 21340, 21353, 21378, | ||
| 21430, 21448, 21475, 22231, 22303, 22763, 22786, 22794, 22805, 22823, 22899, 23376, 23424, | ||
| 23544, 23567, 23586, 23608, 23662, 23665, 24027, 24037, 24049, 24062, 24178, 24186, 24191, | ||
| 24308, 24318, 24331, 24339, 24400, 24417, 24435, 24515, 25096, 25142, 25163, 25903, 25908, | ||
| 25991, 26007, 26020, 26041, 26080, 26085, 26352, 26376, 26408, 27424, 27490, 27513, 27571, | ||
| 27595, 27604, 27611, 27663, 27668, 27700, 28779, 29226, 29238, 29243, 29247, 29255, 29273, | ||
| 29275, 29356, 29572, 29577, 29916, 29926, 29976, 29983, 29992, 30000, 30091, 30098, 30326, | ||
| 30333, 30382, 30399, 30446, 30683, 30690, 30707, 31034, 31160, 31166, 31348, 31435, 31481, | ||
| 31859, 31992, 32566, 32593, 32650, 32701, 32769, 32780, 32786, 32819, 32895, 32905, 33251, | ||
| 33258, 33267, 33276, 33292, 33307, 33311, 33390, 33394, 33400, 34381, 34411, 34880, 34892, | ||
| 34915, 35198, 35211, 35282, 35328, 35895, 35910, 35925, 35960, 35997, 36196, 36208, 36275, | ||
| 36523, 36554, 36763, 36784, 36789, 37009, 37193, 37318, 37324, 37329, 38263, 38272, 38428, | ||
| 38582, 38585, 38632, 38737, 38750, 38754, 38761, 38859, 38893, 38899, 38913, 39080, 39131, | ||
| 39135, 39318, 39321, 39340, 39592, 39640, 39647, 39717, 39727, 39730, 39740, 39770, 40165, | ||
| 40565, 40575, 40613, 40635, 40643, 40653, 40657, 40697, 40701, 40718, 40723, 40736, 40763, | ||
| 40778, 40786, 40845, 40860, 40864, | ||
| ]; | ||
|
|
||
| pub(crate) fn get_radical(ch: char) -> u8 { | ||
| let id = ch as u32; | ||
|
|
||
| if !(19968..=40869).contains(&id) { | ||
| return 0; | ||
| } | ||
|
|
||
| let idx = CODEPOINTS.partition_point(|&b| (b as u32) <= id); | ||
| (idx as u8) + 1 | ||
| pub(crate) fn get_radical(irg: &UnihanIrgData<'_>, ch: char) -> u8 { | ||
| irg.trie.get(ch) | ||
| } | ||
|
|
||
| pub(crate) struct Predictor { | ||
| pub(crate) struct Predictor<'a> { | ||
| pub(crate) model: HashMap<String, HashMap<String, i16>>, | ||
| irg: &'a UnihanIrgData<'a>, | ||
| } | ||
|
|
||
| impl Predictor { | ||
| pub(crate) fn from_json(json: &str) -> Self { | ||
| impl<'a> Predictor<'a> { | ||
| pub(crate) fn from_json(json: &str, irg: &'a UnihanIrgData<'a>) -> Self { | ||
| let model: HashMap<String, HashMap<String, i16>> = | ||
| serde_json::from_str(json).unwrap_or_default(); | ||
| Self { model } | ||
| Self { model, irg } | ||
| } | ||
|
|
||
| pub(crate) fn for_test() -> Self { | ||
| Self::from_json(MODEL_FOR_TEST) | ||
| pub(crate) fn for_test(irg: &'a UnihanIrgData<'a>) -> Self { | ||
| Self::from_json(MODEL_FOR_TEST, irg) | ||
| } | ||
|
|
||
| pub(crate) fn for_test_thai() -> Self { | ||
| Self::from_json(MODEL_FOR_TEST_THAI) | ||
| pub(crate) fn for_test_thai(irg: &'a UnihanIrgData<'a>) -> Self { | ||
| Self::from_json(MODEL_FOR_TEST_THAI, irg) | ||
| } | ||
|
|
||
| pub(crate) fn predict(&self, sentence: &str) -> Vec<i16> { | ||
|
|
@@ -73,15 +52,15 @@ impl Predictor { | |
|
|
||
| let mut score: i16 = 4; | ||
|
|
||
| let rad4 = get_radical(c); | ||
| let rad4 = get_radical(self.irg, c); | ||
| if rad4 != 0 { | ||
| if let Some(map) = self.model.get("RSRID") { | ||
| let key = format!("{}:{}", c_prev, rad4); | ||
| score += map.get(&key).copied().unwrap_or(0); | ||
| } | ||
| } | ||
|
|
||
| let rad3 = get_radical(c_prev); | ||
| let rad3 = get_radical(self.irg, c_prev); | ||
| if rad3 != 0 { | ||
| if let Some(map) = self.model.get("LSRID") { | ||
| let key = format!("{}:{}", rad3, c); | ||
|
|
@@ -247,6 +226,18 @@ impl Predictor { | |
| } | ||
| breakpoints | ||
| } | ||
|
|
||
| pub(crate) fn predict_thai_breakpoints(&self, sentence: &str) -> Vec<usize> { | ||
| let mut breakpoints = vec![0]; | ||
| let mut offset = 0; | ||
| for (&score, ch) in self.predict_thai(sentence).iter().zip(sentence.chars()) { | ||
| offset += ch.len_utf8(); | ||
| if score > 0 { | ||
| breakpoints.push(offset); | ||
| } | ||
| } | ||
| breakpoints | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
|
|
@@ -271,7 +262,8 @@ fn python_test_output_thai() -> Vec<i16> { | |
|
|
||
| #[test] | ||
| fn main() { | ||
| let predictor = Predictor::for_test(); | ||
| let irg = load_irg_from_baked(); | ||
| let predictor = Predictor::for_test(irg); | ||
|
|
||
| let sentence = | ||
| "根据最新的财报数据显示,该公司的市盈率已经达到了历史最低点,但是其核心竞争力依然保持稳定增长的态势。" | ||
|
|
@@ -286,8 +278,9 @@ fn main() { | |
| fn rust_matches_python_probs() { | ||
| let python = python_test_output(); | ||
| let python_thai = python_test_output_thai(); | ||
| let predictor = Predictor::for_test(); | ||
| let predictor_thai = Predictor::for_test_thai(); | ||
| let irg = load_irg_from_baked(); | ||
| let predictor = Predictor::for_test(irg); | ||
| let predictor_thai = Predictor::for_test_thai(irg); | ||
|
|
||
| let sentence = | ||
| "根据最新的财报数据显示,该公司的市盈率已经达到了历史最低点,但是其核心竞争力依然保持稳定增长的态势。" | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,7 +24,7 @@ | |
| 2404 | ||
| -1604 | ||
| 2824 | ||
| 440 | ||
| 320 | ||
| -525 | ||
| 5881 | ||
| 4892 | ||
|
|
@@ -46,4 +46,4 @@ | |
| 4605 | ||
| 4324 | ||
| -2139 | ||
| 5215 | ||
| 5215 | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Large diffs are not rendered by default.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Large diffs are not rendered by default.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.