diff --git a/bindings-python/src/kosha/entries.rs b/bindings-python/src/kosha/entries.rs index 6b1d01d7..94bc72f2 100644 --- a/bindings-python/src/kosha/entries.rs +++ b/bindings-python/src/kosha/entries.rs @@ -37,6 +37,14 @@ pub struct PyDhatuEntry { /// - `vidi~` --> `vind` pub(crate) clean_text: String, + /// The Dhatupatha code for this dhatu. + /// + /// Examples: + /// + /// - `BU` --> `01.0001` + /// - `eD` --> `01.0002` + pub(crate) code: Option, + /// The meaning of this dhatu's *mūla* as an SLP1 string. /// /// We have meaning strings only for the ~2000 *mūla* dhatus from the Dhatupatha. Any roots @@ -59,11 +67,12 @@ pub struct PyDhatuEntry { impl PyDhatuEntry { /// Create a new `DhatuEntry`. #[new] - #[pyo3(signature = (dhatu, clean_text, *, artha_sa = None, artha_en = None, artha_hi = None, + #[pyo3(signature = (dhatu, clean_text, *, code = None, artha_sa = None, artha_en = None, artha_hi = None, karmatva = None, ittva = None, pada = None))] fn new( dhatu: PyDhatu, clean_text: String, + code: Option, artha_sa: Option, artha_en: Option, artha_hi: Option, @@ -74,6 +83,7 @@ impl PyDhatuEntry { Self { dhatu, clean_text, + code, artha_sa, artha_en, artha_hi, @@ -85,9 +95,10 @@ impl PyDhatuEntry { fn __repr__(&self) -> String { format!( - "DhatuEntry(dhatu={}, clean_text={}, artha_sa={})", + "DhatuEntry(dhatu={}, clean_text={}, code={}, artha_sa={})", self.dhatu.__repr__(), py_repr_string(&self.clean_text), + py_repr_option_string(&self.code), py_repr_option_string(&self.artha_sa), ) } @@ -103,6 +114,7 @@ impl<'a> From<&DhatuEntry<'a>> for PyDhatuEntry { Self { dhatu: val.dhatu().into(), clean_text: val.clean_text().to_string(), + code: val.code().map(|x| x.to_string()), artha_sa: val.artha_sa().map(|x| x.to_string()), artha_en: val.artha_en().map(|x| x.to_string()), artha_hi: val.artha_hi().map(|x| x.to_string()), diff --git a/bindings-python/test/unit/kosha/test_entries.py b/bindings-python/test/unit/kosha/test_entries.py index eb4d5104..87fb19ae 100644 --- a/bindings-python/test/unit/kosha/test_entries.py +++ b/bindings-python/test/unit/kosha/test_entries.py @@ -31,6 +31,20 @@ def test_dhatu_entry(): assert results == {"gam"} +def test_dhatu_entry_with_code(): + gam = Dhatu.mula("ga\\mx~", Gana.Bhvadi) + entry = DhatuEntry(dhatu=gam, clean_text="gam", code="01.1065", artha_sa="gatO") + + assert entry.dhatu == gam + assert entry.clean_text == "gam" + assert entry.code == "01.1065" + assert entry.artha_sa == "gatO" + + # Test without code + entry_no_code = DhatuEntry(dhatu=gam, clean_text="gam", artha_sa="gatO") + assert entry_no_code.code is None + + def test_dhatu_entry__dunders(): gam = Dhatu.mula("ga\\mx~", Gana.Bhvadi) entry_gam = DhatuEntry(dhatu=gam, clean_text="gam", artha_sa="gatO") @@ -48,7 +62,7 @@ def test_dhatu_entry__dunders(): # __repr__ assert repr(entry_gam) == ( "DhatuEntry(dhatu=Dhatu(aupadeshika='ga\\mx~', gana=Gana.Bhvadi), " - "clean_text='gam', artha_sa='gatO')" + "clean_text='gam', code=None, artha_sa='gatO')" ) @@ -101,7 +115,7 @@ def test_pratipadika_entry__dunders(): assert repr(gata_entry) == ( "PratipadikaEntry.Krdanta(dhatu_entry=DhatuEntry(dhatu=" - "Dhatu(aupadeshika='ga\\mx~', gana=Gana.Bhvadi), clean_text='gam', artha_sa=None), " + "Dhatu(aupadeshika='ga\\mx~', gana=Gana.Bhvadi), clean_text='gam', code=None, artha_sa=None), " "krt=Krt.kta, prayoga=None, lakara=None)" ) @@ -204,7 +218,7 @@ def test_pada_entry__dunders(): assert repr(gacchati_pada) == ( "PadaEntry.Tinanta(dhatu_entry=DhatuEntry(dhatu=" - "Dhatu(aupadeshika='ga\\mx~', gana=Gana.Bhvadi), clean_text='gam', artha_sa=None), " + "Dhatu(aupadeshika='ga\\mx~', gana=Gana.Bhvadi), clean_text='gam', code=None, artha_sa=None), " "prayoga=Prayoga.Kartari, lakara=Lakara.Lat, purusha=Purusha.Prathama, vacana=Vacana.Eka)" ) diff --git a/vidyut-kosha/src/entries.rs b/vidyut-kosha/src/entries.rs index f60a93ec..d7b53801 100644 --- a/vidyut-kosha/src/entries.rs +++ b/vidyut-kosha/src/entries.rs @@ -24,6 +24,7 @@ pub struct DhatuEntry<'a> { #[derive(Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd, Serialize, Deserialize)] pub struct DhatuMeta { pub(crate) clean_text: String, + pub(crate) code: Option, pub(crate) artha_sa: Option, pub(crate) artha_hi: Option, pub(crate) artha_en: Option, @@ -36,6 +37,7 @@ pub struct DhatuMeta { #[derive(Clone, Debug, Default, Eq, Hash, PartialEq)] pub struct DhatuMetaBuilder { clean_text: Option, + code: Option, artha_sa: Option, artha_hi: Option, artha_en: Option, @@ -122,6 +124,18 @@ impl<'a> DhatuEntry<'a> { self.meta.map_or("", |x| &x.clean_text) } + /// Returns the Dhatupatha code for this dhatu. + /// + /// Examples: + /// + /// - `BU` --> `01.0001` + /// - `eD` --> `01.0002` + /// + /// Data is sourced from . + pub fn code(&self) -> Option<&str> { + self.meta.and_then(|x| x.code.as_deref()) + } + /// Returns the Sanskrit meaning of this dhatu's *mūla* as an SLP1 string. All of these /// meaning strings come directly from the Dhatupatha. /// @@ -192,6 +206,12 @@ impl DhatuMetaBuilder { self } + /// (Optional) Sets `code`. + pub fn code(mut self, code: String) -> Self { + self.code = Some(code); + self + } + /// (Optional) Sets `artha_sa`. pub fn artha_sa(mut self, artha: String) -> Self { self.artha_sa = Some(artha); @@ -235,6 +255,7 @@ impl DhatuMetaBuilder { Some(x) => x, _ => return Err(Error::UnsupportedType), }, + code: self.code, artha_sa: self.artha_sa, artha_en: self.artha_en, artha_hi: self.artha_hi,