Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions bindings-python/src/kosha/entries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,

/// 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
Expand All @@ -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<String>,
artha_sa: Option<String>,
artha_en: Option<String>,
artha_hi: Option<String>,
Expand All @@ -74,6 +83,7 @@ impl PyDhatuEntry {
Self {
dhatu,
clean_text,
code,
artha_sa,
artha_en,
artha_hi,
Expand All @@ -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),
)
}
Expand All @@ -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()),
Expand Down
20 changes: 17 additions & 3 deletions bindings-python/test/unit/kosha/test_entries.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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')"
)


Expand Down Expand Up @@ -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)"
)

Expand Down Expand Up @@ -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)"
)

Expand Down
21 changes: 21 additions & 0 deletions vidyut-kosha/src/entries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
pub(crate) artha_sa: Option<String>,
pub(crate) artha_hi: Option<String>,
pub(crate) artha_en: Option<String>,
Expand All @@ -36,6 +37,7 @@ pub struct DhatuMeta {
#[derive(Clone, Debug, Default, Eq, Hash, PartialEq)]
pub struct DhatuMetaBuilder {
clean_text: Option<String>,
code: Option<String>,
artha_sa: Option<String>,
artha_hi: Option<String>,
artha_en: Option<String>,
Expand Down Expand Up @@ -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 <https://ashtadhyayi.com>.
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.
///
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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,
Expand Down