Skip to content

Commit

Permalink
0.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
pejrich committed Feb 25, 2024
1 parent f0ca34d commit 600d96d
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# CHANGELOG.md

## 0.3.0 (2024-02-24)

Features:

- Return `[]` for invalid values

## 0.2.0 (2024-02-06)

Features:
Expand Down
19 changes: 15 additions & 4 deletions lib/arpabex.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,20 @@ defmodule Arpabex do
## Examples
iex> Arpabex.phonemes_strs("notaword")
["N", "AA1", "T", "AH0", "W", "ER0", "D"]
```elixir
iex> Arpabex.phoneme_strs("notaword")
["N", "AA1", "T", "AH0", "W", "ER0", "D"]
# Returns [] for invalid chars
iex> Arpabex.phoneme_strs(",")
[]
```
"""
def phoneme_strs(string), do: Arpabex.Native.phoneme_strs(string)
@spec phoneme_strs(String.t()) :: list(String.t())
def phoneme_strs(string) do
case Arpabex.Native.phoneme_strs(string) do
nil -> []
["EH1", "N", "AY1", "T", "UH0", "R", "AE1", "N"] -> []
val -> val
end
end
end
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Arpabex.MixProject do
use Mix.Project

@version "0.2.0"
@version "0.3.0"
@source_url "https://github.com/pejrich/Arpabex"

def project do
Expand Down
21 changes: 20 additions & 1 deletion native/arpabex_native/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
extern crate grapheme_to_phoneme;
use grapheme_to_phoneme::Model;
use once_cell::sync::Lazy;
use std::panic;
use std::sync::Mutex;

static MODEL: Lazy<Mutex<Model>> = Lazy::new(|| {
Expand All @@ -10,7 +11,25 @@ static MODEL: Lazy<Mutex<Model>> = Lazy::new(|| {

#[rustler::nif]
fn phoneme_strs(string: &str) -> Vec<&str> {
return MODEL.lock().unwrap().predict_phonemes_strs(string).unwrap();
match MODEL.lock() {
Err(e) => {
println!("Arpabex model error {:#?}\n", e);
return vec![];
}
Ok(model) => {
let result = panic::catch_unwind(|| {
return model.predict_phonemes_strs(string);
});
match result {
Ok(Ok(res)) => {
return res;
}
_ => {
return vec![];
}
}
}
}
}

rustler::init!("Elixir.Arpabex.Native", [phoneme_strs]);

0 comments on commit 600d96d

Please sign in to comment.