Skip to content

Commit

Permalink
[prakriya] Add validation for input strings
Browse files Browse the repository at this point in the history
Sutra tests:
- Add 3.4.97
- Add 3.4.94
- Add 3.4.95
- Enable 6.4.55
- Enable 6.4.51
- Patch 7.4.10

`create_all_krdantas` example:
- Output sya-sat and karmani-sat.
- Switch krt output to enum variant name.

Other:
- Remove outdated integration tests.
  • Loading branch information
akprasad committed Dec 13, 2024
1 parent b602039 commit 560f82b
Show file tree
Hide file tree
Showing 60 changed files with 889 additions and 1,166 deletions.
38 changes: 19 additions & 19 deletions vidyut-prakriya/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -62,85 +62,85 @@ test_tinantas:
../target/release/test_results \
--data-type tinanta \
--test-cases test-files/tinantas-basic-kartari.csv \
--hash "51d6017bcc2516e2af0d028d251b72165e2f19bafb0839e35110f157934609db"
--hash "2dfbc74c55529c9b2ce8dc3202b7c7dca844a4c8f207c2d729ce1fed921fe1b2"
../target/release/test_results \
--data-type tinanta \
--test-cases test-files/tinantas-nic-kartari.csv \
--hash "503fe8ace23a847f8ea02c23263112a0bbb484aa1971fdba5a811a5096dd2681"
--hash "2901d9a7323bd662909ff4ad75d4e8237dca576a92dae36d3059431584c77f9c"
../target/release/test_results \
--data-type tinanta \
--test-cases test-files/tinantas-san-kartari.csv \
--hash "9145fff93b263223e270a2cde065bb3fb0c811df24ab42a043eae39e2f756234"
--hash "f54f89f6652176d48781baab2ea006f4ffc3dc637da1b7fa32b53c0bd7775ff2"
../target/release/test_results \
--data-type tinanta \
--test-cases test-files/tinantas-yan-kartari.csv \
--hash "1998c701f314c8bcdcd48d59300debabd39d3ba586341e5a515815d6a1c2503f"
--hash "69fa8583b872d0c8f21f42815515a7457050a583fad99c062831d817a6094db4"
../target/release/test_results \
--data-type tinanta \
--test-cases test-files/tinantas-yan-luk-kartari.csv \
--hash "8339c74d2414ee47e76623accfe9064b050164fadb0fa113a469df9b8181e074"
--hash "0c4776f55b80bdb4e69e137c258d7365efa1f4e6f1e8c58753846646041428ec"
../target/release/test_results \
--data-type tinanta \
--test-cases test-files/tinantas-san-nic-kartari.csv \
--hash "c5977158acab3b00601ef4b78c29c045a6d9586fb9aa0502856fdab73e630bf5"
--hash "58ccd63ddcb785e84253990bb379aad51d93e68b421adf2e0be9c70edf454bd3"
../target/release/test_results \
--data-type tinanta \
--test-cases test-files/tinantas-nic-san-kartari.csv \
--hash "c292a9b75da8feaa4c4f318900bc257ab703ece640f691ac9c7ac52a683679d2"
--hash "6618699600fdd272c6cfbe1ffad7567f9bdd8950dfabeb859e6cfffed83836f1"
../target/release/test_results \
--data-type tinanta \
--test-cases test-files/tinantas-basic-karmani.csv \
--hash "a30032f2b32cd8008c91c95b7e03e3e9f11386179bb70fd26f5eccf8748d0afe"
--hash "66fa96d66a1fb52d639cc6516c89c54883d71cad2e3558c9984cfdc2d75a6b5e"
../target/release/test_results \
--data-type tinanta \
--test-cases test-files/tinantas-nic-karmani.csv \
--hash "7a5358df11a14351e771b917742a83df10d4013e844419ddeadc91c8cc206548"
--hash "218964795f2d8dba96cefa0f49578f0c636575325bd23412bc10d80fbbb1545a"
../target/release/test_results \
--data-type tinanta \
--test-cases test-files/tinantas-san-karmani.csv \
--hash "725dfe8657b9543aede4a73798b50a89a389c0d74ce5c0c9ecb91f4447d60495"
--hash "a682c39d420379ebc77598154a4804d94f6c35632877f5cc508b46ddbd548ba9"
../target/release/test_results \
--data-type tinanta \
--test-cases test-files/tinantas-yan-karmani.csv \
--hash "6167063aed3186aae0a01d635156618f6cf7ba09c7d7c2ed93e076dc5ef46841"
--hash "f4c2dedc816bec356e97c46831ce35c2272ffeb2d5bb29bfa332cff393e67c9f"
../target/release/test_results \
--data-type tinanta \
--test-cases test-files/tinantas-san-nic-karmani.csv \
--hash "8c3925ad25154ad44ceb3f726843fcb0a453a71b93fb2d4a1c55d20a905dd292"
--hash "ef958478945d449bbd093b02a7f4b84f14939c365c67cec65481b168c2ffdc97"
../target/release/test_results \
--data-type tinanta \
--test-cases test-files/tinantas-nic-san-karmani.csv \
--hash "d679a4a2d3487a99899cb2793c8d890f570016a60d8b5460a62cde92a0dfec76"
--hash "e6ce97fcae9a5855dad2486737e8e28f4a7581190d7b10f9e9592603374504c8"

test_krdantas:
cargo build --release --bin test_results
../target/release/test_results \
--data-type krdanta \
--test-cases test-files/krdantas-basic.csv \
--hash "769a763ab754ee5accf0edefa09b38b1084a069c6bf4e183c3f7039c295511fe"
--hash "90bf0b99a2b641c74a77bbfc1864d904a5ce05ba5aa011f69ea0b12b6e2693e4"
../target/release/test_results \
--data-type krdanta \
--test-cases test-files/krdantas-nic.csv \
--hash "7be488ee87ad8402487d39cc6e3c0fe8be7a9480e88b02a5eec5e05c4a790d0c"
--hash "58fb5d4eef55f5b0d8475e87665cba0038d6daf882399c60a03972e5abc7ed28"
../target/release/test_results \
--data-type krdanta \
--test-cases test-files/krdantas-san.csv \
--hash "a2de7869bf2c6b534c7075ac37803c232f1c24f6fcc58713d85c4f89bbe79d55"
--hash "a689755607b5b634f36ffdb9f01aba212df68eab3d1ddf5cf1cbd8b25ffafaa3"
../target/release/test_results \
--data-type krdanta \
--test-cases test-files/krdantas-yan.csv \
--hash "038ac65be234cfde608106385964b3a67e66fe508d6f91a14b8bff9b6a1c017f"
--hash "7d14c2e8d6f83eff87b6cbdcfb3fedf66bde3e880a555a836b0feaaecee3bc41"
../target/release/test_results \
--data-type krdanta \
--test-cases test-files/krdantas-yan-luk.csv \
--hash "6bdaea4c974026fa7bfab4e874b1c3f0368d6a2f35bbf94ad626ab573bf8282d"
--hash "49cfda9501ba6076e7285a131d9efb1f2d0964fabb71b2e176d94a151e6695c9"

test_dhatus:
cargo build --release --bin test_results
../target/release/test_results \
--data-type dhatu \
--test-cases test-files/dhatus.csv \
--hash "813ceb212c3d500ed4e4a2b892c8c72504be8c0f3602831135698bcd54e62b5f"
--hash "bc7d210cc44e1231e0f89d3f6e39de4288b0c75fa108a1168003fc7c33a16965"


test_subantas:
Expand Down
2 changes: 1 addition & 1 deletion vidyut-prakriya/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ use vidyut_prakriya::args::*;

let v = Vyakarana::new();

let dhatu = Dhatu::mula("BU", Gana::Bhvadi);
let dhatu = Dhatu::mula(Slp1String::from("BU").expect("ok"), Gana::Bhvadi);
let args = Tinanta::builder()
.dhatu(dhatu)
.lakara(Lakara::Lat)
Expand Down
2 changes: 1 addition & 1 deletion vidyut-prakriya/data/dhatupatha.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,7 @@ code dhatu artha
01.0780 Iza~ uYCe
01.0781 kaza~ hiMsAyAm
01.0782 Kaza~ hiMsAyAm
01.0783 Si\\za~ hiMsAyAm
01.0783 Si\za~ hiMsAyAm
01.0784 jaza~ hiMsAyAm
01.0785 Jaza~ hiMsAyAm
01.0786 Saza~ hiMsAyAm
Expand Down
66 changes: 44 additions & 22 deletions vidyut-prakriya/examples/create_all_krdantas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use serde::Serialize;
use std::error::Error;
use std::io;
use vidyut_lipi::{Lipika, Scheme};
use vidyut_prakriya::args::{BaseKrt, Krdanta, Sanadi};
use vidyut_prakriya::args::{BaseKrt, Krdanta, Lakara, Prayoga, Sanadi};
use vidyut_prakriya::{Dhatupatha, Vyakarana};

/// Command line arguments.
Expand All @@ -38,7 +38,9 @@ struct Row<'a> {
gana: &'static str,
number: u16,
sanadi: String,
krt: &'static str,
prayoga: Option<Prayoga>,
lakara: Option<Lakara>,
krt: BaseKrt,
}

fn create_output_string(
Expand Down Expand Up @@ -73,33 +75,53 @@ fn run(dhatupatha: Dhatupatha, args: Args) -> Result<(), Box<dyn Error>> {
None => Scheme::Slp1,
};

let sat_prayoga_lakara = &[
(Some(Prayoga::Kartari), Some(Lakara::Lat)),
(Some(Prayoga::Kartari), Some(Lakara::Lrt)),
(Some(Prayoga::Karmani), Some(Lakara::Lat)),
(Some(Prayoga::Karmani), Some(Lakara::Lrt)),
];

for sanadis in &sanadi_choices {
for entry in &dhatupatha {
let dhatu = entry.dhatu().clone().with_sanadi(&sanadis);
for krt in BaseKrt::iter() {
let krdanta = Krdanta::builder().dhatu(dhatu.clone()).krt(krt).build()?;
let prayoga_lakara: &[(Option<Prayoga>, Option<Lakara>)] = match krt {
BaseKrt::Satf | BaseKrt::SAnac => sat_prayoga_lakara,
_ => &[(None, None)],
};

let prakriyas = v.derive_krdantas(&krdanta);
if prakriyas.is_empty() {
continue;
}
for (prayoga, lakara) in prayoga_lakara.iter().copied() {
let mut builder = Krdanta::builder().dhatu(dhatu.clone()).krt(krt);
if let (Some(prayoga), Some(lakara)) = (prayoga, lakara) {
builder = builder.prayoga(prayoga).lakara(lakara);
}
let krdanta = builder.build()?;

let dhatu_text = &dhatu.aupadeshika().expect("ok");
let krdantas: Vec<_> = prakriyas.iter().map(|p| p.text()).collect();
let krdantas = create_output_string(&mut lipika, krdantas, output_scheme);
let sanadi_text: Vec<_> = sanadis.iter().map(|x| x.as_str()).collect();
let sanadi_text = sanadi_text.join("-");

let row = Row {
krdantas,
dhatu: dhatu_text,
gana: dhatu.gana().expect("ok").as_str(),
number: entry.number(),
sanadi: sanadi_text,
krt: krt.as_str(),
};
let prakriyas = v.derive_krdantas(&krdanta);
if prakriyas.is_empty() {
continue;
}

let dhatu_text = &dhatu.aupadeshika().expect("ok");
let krdantas: Vec<_> = prakriyas.iter().map(|p| p.text()).collect();
let krdantas = create_output_string(&mut lipika, krdantas, output_scheme);
let sanadi_text: Vec<_> = sanadis.iter().map(|x| x.as_str()).collect();
let sanadi_text = sanadi_text.join("-");

wtr.serialize(row)?;
let row = Row {
krdantas,
dhatu: dhatu_text,
gana: dhatu.gana().expect("ok").as_str(),
number: entry.number(),
sanadi: sanadi_text,
prayoga,
lakara,
krt,
};

wtr.serialize(row)?;
}
}
}
}
Expand Down
16 changes: 8 additions & 8 deletions vidyut-prakriya/examples/create_all_tinantas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,10 @@ struct Row<'a> {
gana: &'static str,
number: u16,
sanadi: String,
prayoga: &'static str,
lakara: &'static str,
purusha: &'static str,
vacana: &'static str,
prayoga: Prayoga,
lakara: Lakara,
purusha: Purusha,
vacana: Vacana,
}

fn create_output_string(
Expand Down Expand Up @@ -111,10 +111,10 @@ fn run(dhatupatha: Dhatupatha, args: Args) -> Result<(), Box<dyn Error>> {
gana: dhatu.gana().expect("ok").as_str(),
number: entry.number(),
sanadi: sanadi_text.clone(),
lakara: lakara.as_str(),
purusha: purusha.as_str(),
vacana: vacana.as_str(),
prayoga: prayoga.as_str(),
lakara,
purusha,
vacana,
prayoga,
};

wtr.serialize(row)?;
Expand Down
6 changes: 2 additions & 4 deletions vidyut-prakriya/src/ac_sandhi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ fn apply_ac_sandhi_at_term_boundary(p: &mut Prakriya, i: usize) -> Option<()> {
Some(())
}

fn try_sut_kat_purva(p: &mut Prakriya) -> Option<()> {
pub fn try_sut_kat_purva(p: &mut Prakriya) -> Option<()> {
let i_dhatu = p.find_first_with_tag(T::Dhatu)?;
let dhatu = p.get(i_dhatu)?;

Expand All @@ -333,7 +333,7 @@ fn try_sut_kat_purva(p: &mut Prakriya) -> Option<()> {
let prev = p.get(i_prev)?;

let optional_add_sut_agama = |rule, p: &mut Prakriya, i_dhatu: usize| {
if p.optional_run(rule, |p| p.insert_before(i_dhatu, A::suw)) {
if p.optional_run(rule, |p| p.insert(i_dhatu, A::suw)) {
it_samjna::run(p, i_dhatu).expect("ok");
}
};
Expand Down Expand Up @@ -416,7 +416,5 @@ pub fn run_common(p: &mut Prakriya) -> Option<()> {
apply_general_ac_sandhi(p, 0, p.len() - 1);
hacky_apply_ni_asiddhavat_rules(p);

try_sut_kat_purva(p);

Some(())
}
6 changes: 4 additions & 2 deletions vidyut-prakriya/src/angasya.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ pub fn maybe_do_jha_adesha(p: &mut Prakriya) -> Option<()> {

let base = p.get(i_base)?;
if base.is_dhatu() && p.has(i, |t| t.is_atmanepada()) {
let add_rut = |p: &mut Prakriya| p.insert_before(i, A::ruw);
let add_rut = |p: &mut Prakriya| p.insert(i, A::ruw);
if base.is_u(Au::SIN) {
// Serate
p.run("7.1.6", add_rut);
Expand Down Expand Up @@ -176,7 +176,6 @@ fn try_pratyaya_adesha_at_index(p: &mut Prakriya, i_anga: usize) -> Option<()> {

/// Runs rules that change one or more letters in the anga to a 't'.
fn try_anga_changes_before_t(p: &mut Prakriya, i_anga: usize) -> Option<()> {
p.debug("anga before t");
let anga = p.get(i_anga)?;

if anga.is_dhatu() {
Expand Down Expand Up @@ -1241,6 +1240,9 @@ pub fn run_before_dvitva(p: &mut Prakriya, is_lun: bool, skip_at_agama: bool) ->
Some(())
});

// Must run before guna for saYcaskaratuH, etc.
ac_sandhi::try_sut_kat_purva(p);

p.debug("==== Guna-vrddhi ====");
guna_vrddhi::run(p);

Expand Down
4 changes: 2 additions & 2 deletions vidyut-prakriya/src/angasya/abhyasasya.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,15 +412,15 @@ fn try_rules_for_yan(p: &mut Prakriya, i_abhyasa: usize) -> Option<()> {
}

let optional_add_agama = |rule, p: &mut Prakriya, i_dhatu, agama: Agama| -> bool {
let added = p.optional_run(rule, |p| p.insert_before(i_dhatu, agama));
let added = p.optional_run(rule, |p| p.insert(i_dhatu, agama));
if added {
it_samjna::run(p, i_dhatu).ok();
}
added
};

let add_agama = |rule, p: &mut Prakriya, i_dhatu, agama| -> bool {
p.run(rule, |p| p.insert_before(i_dhatu, agama));
p.run(rule, |p| p.insert(i_dhatu, agama));
it_samjna::run(p, i_dhatu).ok();
true
};
Expand Down
8 changes: 5 additions & 3 deletions vidyut-prakriya/src/angasya/asiddhavat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ pub fn try_cinvat_for_bhave_and_karmani_prayoga(p: &mut Prakriya) -> Option<()>
t.add_tag(T::Rit);
t.add_tag(T::Cinvat);
});
p.insert_before(i_n, A::iw);
p.insert(i_n, A::iw);
});
if ran {
it_samjna::run(p, i_n).ok();
Expand Down Expand Up @@ -836,9 +836,11 @@ pub fn run_for_ni_at_index(p: &mut Prakriya, i_ni: usize) -> Option<()> {

if n.last().is_ardhadhatuka() {
let dhatu = p.get(i_dhatu)?;
let next = n.first();

if n.first()
.has_text_in(&["Am", "anta", "Alu", "Ayya", "itnu", "iznu"])
if next.has_text_in(&["Am"])
|| next.is_any_krt(&[K::Aluc, K::izRuc])
|| next.is_any_unadi(&[U::Jac, U::Ayya, U::itnuc, U::izRuc])
{
// corayAm, spfhayAlu, etc.
p.run_at("6.4.55", i_ni, op::antya("ay"));
Expand Down
10 changes: 7 additions & 3 deletions vidyut-prakriya/src/angasya/guna_vrddhi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,12 +156,15 @@ fn try_taddhita_vrddhi(p: &mut Prakriya, i_anga: usize, i_n: usize) -> Option<()
return None;
};

if anga.is_any_phit(&["kekaya", "mitrayu", "pralaya"]) {
p.run_at("7.3.2", i_anga, |t| t.find_and_replace_text("y", "iy"));
}

let anga = p.get(i_anga)?;
if anga.is_any_phit(&["devikA", "SiMSapA", "dityavAh", "dIrGasatra", "Sreyas"]) {
// dAvikA, ...
let adi_ac = anga.text.find(al::is_ac)?;
p.run_at("7.3.1", i_anga, |t| t.set_at(adi_ac, "A"));
} else if anga.is_any_phit(&["kekaya", "mitrayu", "pralaya"]) {
p.run_at("7.3.2", i_anga, |t| t.find_and_replace_text("y", "iy"));
} else if anga.starts_with("vy") {
// HACK: should properly be only with vi-upasarga.
// TODO: also apply for sv-, .etc.
Expand Down Expand Up @@ -446,7 +449,8 @@ fn try_r_guna_before_lit(p: &mut Prakriya, i: usize) -> Option<()> {
};

let anga = p.get(i)?;
if anga.has_antya('f') && anga.is_samyogadi() {
let is_skr = || anga.has_u("qukf\\Y") && i > 0 && p.has(i - 1, |t| t.is(A::suw));
if anga.has_antya('f') && (anga.is_samyogadi() || is_skr()) {
p.run_at("7.4.10", i, do_ar_guna);
} else if anga.has_antya('F') || (anga.has_u_in(&["fCa~", "f\\"]) && anga.has_adi('f')) {
if anga.has_u("fCa~") {
Expand Down
2 changes: 1 addition & 1 deletion vidyut-prakriya/src/angasya/subanta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ fn try_add_nit_agamas(p: &mut Prakriya, i_anga: usize) -> Option<()> {
// tasyE
p.run("7.3.114", |p| {
p.set(i_anga, op::antya("a"));
p.insert_before(i_sup, A::syAw)
p.insert(i_sup, A::syAw)
});
it_samjna::run(p, i_sup).ok()?;
} else {
Expand Down
2 changes: 2 additions & 0 deletions vidyut-prakriya/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ mod krt;
mod pada;
mod pratipadika;
mod samasa;
mod slp1_string;
mod sup;
mod taddhita;
mod tin;
Expand All @@ -32,6 +33,7 @@ pub use krt::*;
pub use pada::*;
pub use pratipadika::*;
pub use samasa::*;
pub use slp1_string::Slp1String;
pub use sup::*;
pub use taddhita::*;
pub use tin::*;
Expand Down
Loading

0 comments on commit 560f82b

Please sign in to comment.