Skip to content

Commit

Permalink
[prakriya] Fix various UI and prakriya bugs
Browse files Browse the repository at this point in the history
- Fix GitHub issue #113 (UI: add help text)
- Fix GitHub issue #114 (add prakriya generation example)
- Fix GitHub issue #117 (UI: search by normal dhatu)
- Fix GitHub issue #119 (yasya halaH)
- Fix GitHub issue #125 (baBUva)
- Fix small typos in comments
  • Loading branch information
akprasad committed May 29, 2024
1 parent 440ede1 commit 8e01e35
Show file tree
Hide file tree
Showing 10 changed files with 240 additions and 75 deletions.
73 changes: 73 additions & 0 deletions vidyut-prakriya/examples/print_prakriyas.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//! Prints some basic prakriyas. Usage:
//!
//! cargo run --example print_prakriyas
//!
//! In debug mode, prakriyas will also include debug information. To exclude this debug
//! information, build with `--release` instead:
//!
//! cargo run --release --example print_prakriyas
use vidyut_prakriya::args::*;
use vidyut_prakriya::{Prakriya, Vyakarana};

/// Prints the `prakriyas` provided.
fn print_prakriyas(prakriyas: &[Prakriya]) {
for p in prakriyas {
// `p.text()` contains the final output.
println!("{}", p.text());
println!("---------------------------");
// `p.history()` contains each rule that was applied as well as the rule's results.
for step in p.history() {
// `step.rule().code()` is a string that identifies the rule (e.g. "1.3.1").
let code = step.rule().code();
// `step.result()` contains all of the *terms* that are part of this step. These
// include dhatus, agamas, pratyayas, etc.
//
// Here, we create a single string to show all of the results from each term.
let terms: Vec<_> = step
.result()
.iter()
.map(|x| x.text())
.filter(|x| !x.is_empty())
.collect();
let result = terms.join(" + ");
println!("{:<10} | {}", code, result);
}
println!("---------------------------");
println!("\n");
}
}

fn main() {
// For extra options, see `Vyakarana::builder()`.
let v = Vyakarana::new();

// Create a basic dhatu with `Dhatu::mula`
//
// For supported dhatus, see `dhatupatha.tsv`.
let bhu = Dhatu::mula("BU", Gana::Bhvadi);

let args = Tinanta::builder()
.dhatu(bhu)
.lakara(Lakara::Lat)
.prayoga(Prayoga::Kartari)
.purusha(Purusha::Prathama)
.vacana(Vacana::Eka)
.build()
.unwrap();
let prakriyas = v.derive_tinantas(&args);
print_prakriyas(&prakriyas);

// Create a sannanta dhatu with `with_sanadi`.
let jijnasa = Dhatu::mula("jYA\\", Gana::Kryadi).with_sanadi(&[Sanadi::san]);

let args = Tinanta::builder()
.dhatu(jijnasa)
.lakara(Lakara::Lat)
.prayoga(Prayoga::Kartari)
.purusha(Purusha::Prathama)
.vacana(Vacana::Eka)
.build()
.unwrap();
let prakriyas = v.derive_tinantas(&args);
print_prakriyas(&prakriyas);
}
1 change: 0 additions & 1 deletion vidyut-prakriya/src/angasya.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ fn add_num(t: &mut Term) {

/// Runs rules that lengthen a vowel in the anga.
fn try_do_dirgha(p: &mut Prakriya, i_anga: usize) -> Option<()> {
p.debug(format!("TRY DO HALAH {i_anga}"));
let anga = p.get_if(i_anga, |t| t.is_anga())?;
// Also include yAsut-Agama for ji + yAs + t --> jIyAt.
// TODO: extend?
Expand Down
2 changes: 1 addition & 1 deletion vidyut-prakriya/src/angasya/abhyasasya.rs
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ fn try_rules_for_lit(p: &mut Prakriya, i: usize) -> Option<()> {
//
// We check gana 1 for `BU` and gana 2 for `as` replaced by `BU`. This check excludes BU
// with gana 10.
p.run_at("7.4.73", i, op::text("ba"));
p.run_at("7.4.73", i, op::text("Ba"));
// TODO: 7.4.74
}

Expand Down
11 changes: 5 additions & 6 deletions vidyut-prakriya/src/angasya/asiddhavat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -491,12 +491,11 @@ fn try_ardhadhatuke(p: &mut Prakriya, i: usize) -> Option<()> {
p.optional_run_at("6.4.47", i, op::text("Barj"));
} else if anga.ends_with("ya") && is_halah(p, i) && !anga.has_u("kyac") {
// TODO: why block kyac? SK mentions the "sannipAta-pariBAzA" in 2658
p.run_at("6.4.49", i, |t| {
t.set_antya("");
t.set_antya("");
t.add_tag(T::FlagAtLopa);
});
} else if has_antya_a_asiddhavat(anga) {
p.run_at("6.4.49", i, |t| t.set_adi(""));
}

let anga = p.get(i)?;
if has_antya_a_asiddhavat(anga) {
p.run_at("6.4.48", i, |t| {
t.set_antya("");
t.add_tag(T::FlagAtLopa);
Expand Down
4 changes: 2 additions & 2 deletions vidyut-prakriya/src/vyakarana.rs
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ impl VyakaranaBuilder {
self
}

/// *(default: folse)* Controls whether or not to allow rules marked "chandasi," "mantre," etc..
/// *(default: false)* Controls whether or not to allow rules marked "chandasi," "mantre," etc..
///
/// - If `true`, each `Prakriya` will have access to chAndasa rules.
///
Expand All @@ -388,7 +388,7 @@ impl VyakaranaBuilder {
self
}

/// *(default: folse)* Controls whether or not to run svara rules.
/// *(default: false)* Controls whether or not to run svara rules.
///
/// - If `true`, each `Prakriya` will have its svaras marked.
///
Expand Down
14 changes: 14 additions & 0 deletions vidyut-prakriya/src/wasm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,18 @@ impl Vidyut {
serde_wasm_bindgen::to_value(&Vec::<WebPrakriya>::new()).expect("wasm")
}
}

/// Wrapper for `Vyakarana::derive_dhatus`.
#[allow(non_snake_case)]
pub fn deriveDhatus(&self, code: &str) -> JsValue {
if let Some(dhatu) = self.dhatupatha.get(code) {
let v = Vyakarana::new();
let prakriyas = v.derive_dhatus(&dhatu);
let web_prakriyas = to_web_prakriyas(&prakriyas);
serde_wasm_bindgen::to_value(&web_prakriyas).expect("wasm")
} else {
error(&format!("[vidyut] Dhatu code not found: {code}"));
serde_wasm_bindgen::to_value(&Vec::<WebPrakriya>::new()).expect("wasm")
}
}
}
104 changes: 103 additions & 1 deletion vidyut-prakriya/tests/prakriyas.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Tests that verify that a prakriya has a specific format.
//! Tests to verify that a prakriya has a specific format.
//!
//! TODO: add tests from the पाणिनीयव्याकरणोदाहरणकोषः
extern crate test_utils;
Expand All @@ -7,6 +7,7 @@ use vidyut_prakriya::args::Gana::*;
use vidyut_prakriya::args::*;
use vidyut_prakriya::Rule;

// Sample test for `Bavati`.
#[test]
fn bhavati() {
let bhu = d("BU", Bhvadi);
Expand Down Expand Up @@ -36,6 +37,77 @@ fn bhavati() {
);
}

/// Fixes https://github.com/ambuda-org/vidyut/issues/118
///
/// This test verifies the following:
/// - We correctly apply 6.4.108.
/// - We lengthen the dhatu's vowel with 6.4.2.
#[test]
fn jiyat() {
let jya = d("jyA\\", Kryadi);
let args = Tinanta::builder()
.dhatu(jya)
.prayoga(Prayoga::Kartari)
.purusha(Purusha::Prathama)
.vacana(Vacana::Eka)
.lakara(Lakara::AshirLin)
.build()
.unwrap();
let t = Tester::default();
let ps = t.derive_tinantas(&args);
let p = ps.iter().find(|p| p.text() == "jIyAt").unwrap();

use Rule::Ashtadhyayi as A;

assert_matches_prakriya(
p,
&[
(A("1.3.1"), vec!["jyA\\"]),
(A("3.4.116"), vec!["jyA", "ti"]),
(A("6.1.16"), vec!["jiA", "yAs", "st"]),
(A("6.1.108"), vec!["ji", "yAs", "st"]),
(A("6.4.2"), vec!["jI", "yAs", "st"]),
(A("8.4.56"), vec!["jI", "yA", "t"]),
],
);
}

/// Fixes https://github.com/ambuda-org/vidyut/issues/119
///
/// This test verifies the following:
/// - We correctly apply 6.4.49.
#[test]
fn paspardhyate() {
let spardh = d("sparDa~\\", Bhvadi);
let args = Tinanta::builder()
.dhatu(spardh.with_sanadi(&[Sanadi::yaN]))
.prayoga(Prayoga::Karmani)
.purusha(Purusha::Prathama)
.vacana(Vacana::Eka)
.lakara(Lakara::Lat)
.build()
.unwrap();
let t = Tester::default();
let ps = t.derive_tinantas(&args);
for p in &ps {
println!("{}", p.text());
}
let p = ps.iter().find(|p| p.text() == "pAsparDyate").unwrap();

use Rule::Ashtadhyayi as A;

assert_matches_prakriya(
p,
&[
(A("1.3.1"), vec!["sparDa~\\"]),
(A("1.2.4"), vec!["pA", "sparD", "ya", "ya", "te"]),
(A("6.4.49"), vec!["pA", "sparD", "a", "ya", "te"]),
(A("6.4.48"), vec!["pA", "sparD", "", "ya", "te"]),
(A("8.4.68"), vec!["pA", "sparD", "", "ya", "te"]),
],
);
}

// Test to make sure 8.4.1 applies in akzRoti (i.e when R immediately follows r/z)
#[test]
fn akshnoti() {
Expand Down Expand Up @@ -77,3 +149,33 @@ fn krinaati() {

assert_matches_prakriya(p, &[(A("8.4.2"), vec!["krI", "RA", "ti"])]);
}

// Fixes https://github.com/ambuda-org/vidyut/issues/125
//
// This test verifies the following:
// - We correctly apply 8.4.54 with 7.4.73
#[test]
fn babhuva() {
let bhu = d("BU", Bhvadi);
let args = Tinanta::builder()
.dhatu(bhu)
.prayoga(Prayoga::Kartari)
.purusha(Purusha::Prathama)
.vacana(Vacana::Eka)
.lakara(Lakara::Lit)
.build()
.unwrap();
let t = Tester::default();
let ps = t.derive_tinantas(&args);
let p = ps.iter().find(|p| p.text() == "baBUva").unwrap();

use Rule::Ashtadhyayi as A;

assert_matches_prakriya(
p,
&[
(A("7.4.73"), vec!["Ba", "BU", "v", "a"]),
(A("8.4.54"), vec!["ba", "BU", "v", "a"]),
],
);
}
40 changes: 4 additions & 36 deletions vidyut-prakriya/tests/regressions.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
//! Derivations that aren't captured in our other tests.
//! Bug fixes that aren't captured in other tests.
//!
//! The tests here are responses to bug fixes that users have filed. Each should show that the bug
//! has been fixed and help ensure that the bug does not reappear.
extern crate test_utils;
use test_utils::*;
use vidyut_prakriya::args::Gana::*;
Expand Down Expand Up @@ -43,38 +46,3 @@ fn irshy_san_lan() {
&["Erzyiyizat", "Erzyizizat"],
);
}

/// Fixes https://github.com/ambuda-org/vidyut/issues/118
///
/// This test verifies the following:
/// - We correctly apply 6.4.108.
/// - We lengthen the dhatu's vowel with 6.4.2.
#[test]
fn jiyat_prakriya() {
let jya = d("jyA\\", Kryadi);
let args = Tinanta::builder()
.dhatu(jya)
.prayoga(Prayoga::Kartari)
.purusha(Purusha::Prathama)
.vacana(Vacana::Eka)
.lakara(Lakara::AshirLin)
.build()
.unwrap();
let t = Tester::default();
let ps = t.derive_tinantas(&args);
let p = ps.iter().find(|p| p.text() == "jIyAt").unwrap();

use Rule::Ashtadhyayi as A;

assert_matches_prakriya(
p,
&[
(A("1.3.1"), vec!["jyA\\"]),
(A("3.4.116"), vec!["jyA", "ti"]),
(A("6.1.16"), vec!["jiA", "yAs", "st"]),
(A("6.1.108"), vec!["ji", "yAs", "st"]),
(A("6.4.2"), vec!["jI", "yAs", "st"]),
(A("8.4.56"), vec!["jI", "yA", "t"]),
],
);
}
2 changes: 2 additions & 0 deletions vidyut-prakriya/www/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ <h1 class="font-bold text-3xl" x-text="devaNoSvara(activeDhatu.upadesha)"></h1>
</select>
</div>

<p class="text-slate-500">(Click on a <i>pada</i> to see its <i>prakriyā</i>.)</p>

<template x-for="la in createAllTinantas()">
<section>
<h1 class="text-2xl font-bold my-4" x-text="la.title"></h1>
Expand Down
Loading

0 comments on commit 8e01e35

Please sign in to comment.