Skip to content

Commit

Permalink
♻️ Decoupled PhonotacticRules logic from Phonotactic config
Browse files Browse the repository at this point in the history
  • Loading branch information
Thaza-Kun committed Apr 15, 2024
1 parent 0cf0760 commit 17db0a1
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 27 deletions.
18 changes: 18 additions & 0 deletions wasm-rs/onc/src/phonotactics/mod.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,28 @@
pub mod tags;

use itertools::Itertools;
use nom::IResult;
use std::fmt::Display;

use crate::phonotactics::tags::SyllableTags;

#[derive(Clone, Default)]
pub struct PhonotacticRule {
definition: SyllableTags<String>,
}

impl PhonotacticRule {
pub fn with_definitions(definition: SyllableTags<String>) -> Self {
PhonotacticRule { definition }
}
pub fn parse_syllables<'a>(&'a self, input: &'a String) -> IResult<&'a str, Phrase<&'a str>> {
self.definition
.as_str()
.parse_tags(&input)
.map(|(r, p)| (r, p.with_postprocessing(&self.definition)))
}
}

pub struct Phrase<O: Copy> {
pub syllables: Vec<SyllableUnit<O>>,
}
Expand Down
10 changes: 8 additions & 2 deletions wasm-rs/src/imbuhan.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use onc::phonotactics::PhonotacticRule;

use std::collections::HashMap;

use serde::{Deserialize, Serialize};
Expand Down Expand Up @@ -58,7 +60,9 @@ impl Imbuhan {
awal: Some(a),
akhir: _,
}) => {
if let Ok((_rest, phrase)) = phonotactic.parse_syllables(&text) {
if let Ok((_rest, phrase)) =
PhonotacticRule::from(phonotactic.clone()).parse_syllables(&text)
{
if let Some(first) = phrase.syllables.first() {
let mut offset = 0;
let default = &"".to_string();
Expand Down Expand Up @@ -90,7 +94,9 @@ impl Imbuhan {
akhir: Some(a),
awal: _,
}) => {
if let Ok((_rest, phrase)) = phonotactic.parse_syllables(&text) {
if let Ok((_rest, phrase)) =
PhonotacticRule::from(phonotactic.clone()).parse_syllables(&text)
{
if let Some(first) = phrase.syllables.first() {
let mut offset = 0;
let default = &"".to_string();
Expand Down
45 changes: 20 additions & 25 deletions wasm-rs/src/phonotactics.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use onc::phonotactics::tags::SyllableTags;
use onc::phonotactics::Phrase;
use onc::phonotactics::{PhonotacticRule, Phrase};
use onc::IResult;

use serde::Deserialize;
Expand All @@ -14,15 +14,27 @@ pub struct Phonotactic {
definition: SyllableTags<String>,
}

impl From<Phonotactic> for PhonotacticRule {
fn from(value: Phonotactic) -> Self {
PhonotacticRule::with_definitions(value.definition)
}
}

#[wasm_bindgen]
impl Phonotactic {
pub fn new(name: String, definition: SyllableTags<String>) -> Self {
Self { name, definition }
pub fn parse_string(&mut self, input: String, options: ParseResultOptions) -> ParseResults {
let text = input.to_lowercase();
let rule = PhonotacticRule::from(self.clone());
let s = rule.parse_syllables(&text);
InnerParseResult::from(s).render(options)
}
pub fn parse_syllables<'a>(&'a self, input: &'a String) -> IResult<&'a str, Phrase<&'a str>> {
self.definition
.as_str()
.parse_tags(&input)
.map(|(r, p)| (r, p.with_postprocessing(&self.definition)))
#[wasm_bindgen(getter)]
pub fn name(&self) -> String {
self.name.clone()
}
#[wasm_bindgen(getter)]
pub fn tags(&self) -> SyllableTagsJson {
self.definition.clone().into()
}
}

Expand Down Expand Up @@ -202,23 +214,6 @@ impl<'a> From<IResult<&'a str, Phrase<&'a str>>> for InnerParseResult<'a> {
}
}

#[wasm_bindgen]
impl Phonotactic {
pub fn parse_string(&mut self, input: String, options: ParseResultOptions) -> ParseResults {
let text = input.to_lowercase();
let s = self.parse_syllables(&text);
InnerParseResult::from(s).render(options)
}
#[wasm_bindgen(getter)]
pub fn name(&self) -> String {
self.name.clone()
}
#[wasm_bindgen(getter)]
pub fn tags(&self) -> SyllableTagsJson {
self.definition.clone().into()
}
}

#[cfg(test)]
mod test {
use crate::phonotactics::Phonotactic;
Expand Down

0 comments on commit 17db0a1

Please sign in to comment.