-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #37 from fearful-symmetry/rhai_support
Add rhai support
- Loading branch information
Showing
21 changed files
with
421 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"transforms": { | ||
"from-root" : { | ||
"transforms": [ | ||
{"rhai_script": {"file": "examples/rhai/rhai/string_transform.rhai"}} | ||
] | ||
}, | ||
"from-middle": { | ||
"transforms": [ | ||
{"rhai_script": {"file": "examples/rhai/rhai/array_transform.rhai"}} | ||
] | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Using the Rhai scripting language to transform words | ||
|
||
Kirum supports [Rhai](https://github.com/rhaiscript/rhai) for transforming words in a language tree using a simple scripting language. This allows for more complex transforms based on string manipulation and conditional logic: | ||
|
||
```rhai | ||
// if the etymon's language is "mylang", remove all instances of the letter "t" | ||
// and add the post fix "ah" if the word also starts with "el". | ||
if language == "mylang" { | ||
updated.remove("t"); | ||
if updated.starts_with("el"){ | ||
updated = updated + "ah" | ||
} | ||
} | ||
``` | ||
|
||
Rhai documentation can be found [here](https://rhai.rs/book/ref/index.html). | ||
|
||
To use a rhai script, specify it as a transform: | ||
```json | ||
"from-root" : { | ||
"transforms": [ | ||
{"rhai_script": {"file": "rhai/string_transform.rhai"}} | ||
] | ||
}, | ||
``` | ||
|
||
As demonstrated in [string_transform.rhai](rhai/string_transform.rhai), the the Rhai script exports a number of variables | ||
that can be used in a script to transform a word selectively based on the word's associated metadata. | ||
|
||
To render the test, run: | ||
|
||
```bash | ||
kirum render -d ./examples/rhai line | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// in addition to lemma_string | ||
// the word is also exported as an array | ||
// This is useful in cases where the characters of a language do not align with uncode's idea of a word. | ||
let updated = lemma_array; | ||
|
||
updated.push("u"); | ||
|
||
updated |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
// kirum exports the word you want to transform as `lemma_string` | ||
|
||
let updated = lemma_string; | ||
updated.replace("k", "c"); | ||
|
||
// Note that the metadata exported into the Rhai script is the metadata of | ||
// the word youre *transforming from*, not the word you're *transforming into*. | ||
|
||
// Kirum also exports the part of speech as `pos` | ||
// see libkirum/src/word.rs for possible values. | ||
|
||
if pos == "none" { | ||
updated = "ō" + updated; | ||
} | ||
|
||
// `language` is also available | ||
|
||
if language == "Greek" { | ||
updated.remove("t"); | ||
} | ||
|
||
// The tags associated with the word are also exported | ||
if tags.contains("example") { | ||
updated = updated + "a"; | ||
} | ||
|
||
//the historical_metadata field is also exported | ||
if metadata.contains("word") { | ||
updated = updated + "l"; | ||
} | ||
|
||
// return the value | ||
updated |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"words": { | ||
"late": { | ||
"language": "Exemplum", "definition": "the final word", | ||
"etymology": {"etymons": [{"etymon": "middle", "transforms": ["from-middle"]}] | ||
} | ||
}, | ||
"middle": { | ||
"language": "Exemplum", "definition": "The middle word", | ||
"etymology": {"etymons": [{"etymon": "root-word", "transforms":["from-root"]}]} | ||
}, | ||
"root-word": { | ||
"word": "kratia", "language": "Greek", "definition": "power, rule", | ||
"tags": ["example"], | ||
"historical_metadata": {"word": "example"} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.