Skip to content

Commit

Permalink
added regex support
Browse files Browse the repository at this point in the history
  • Loading branch information
AlmasB committed Jan 26, 2018
1 parent 4e7f079 commit dde3aa6
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 19 deletions.
29 changes: 23 additions & 6 deletions src/main/kotlin/com/almasb/tracery/TraceryConcepts.kt
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,16 @@ class Symbol(val key: String, val ruleset: Set<Rule>) {
/**
* Selects a random single rule from the ruleset.
*/
fun selectRule(): Rule {
fun selectRule(regex: String): Rule {
// TODO: when using regex also take into account distributions?

if (regex.isNotEmpty()) {
val valid = distributions.values.plus(withoutDistr).filter { it.text.matches(regex.toRegex()) }

return valid[Tracery.random.nextInt(valid.size)]
}


if (distributions.isNotEmpty()) {
val randomValue = Tracery.random.nextInt(100)

Expand Down Expand Up @@ -198,23 +207,31 @@ class Grammar() {
} else if (result[i] == '}') {
val key = result.substring(symbolTagIndex + 1, i)

// "it" is of form key.mod.mod where mods are optional
// in case we have modifiers
val symbolName = key.substringBefore(".")
// "it" is of form key#regex#.mod.mod where mods are optional

val symbolName = if (key.contains("#")) {
key.substringBefore("#")
} else {
// in case we have modifiers
key.substringBefore(".")
}

// TODO: generalize


val newValue = if (symbolName == "num") {
Tracery.random.nextInt(Int.MAX_VALUE).toString()
} else {
getSymbol(symbolName).selectRule().text
val regex = if (key.contains("#")) key.substringAfter("#").substringBefore("#") else ""

getSymbol(symbolName).selectRule(regex).text
}

var replacedValue = expand(newValue)

if (key.hasModifiers()) {
replacedValue = applyModifiers(replacedValue, key.split(".").drop(1))
// clean from regex then apply mods
replacedValue = applyModifiers(replacedValue, key.substringAfterLast("#").split(".").drop(1))
}

result = result.replaceRange(symbolTagIndex, i+1, replacedValue)
Expand Down
24 changes: 12 additions & 12 deletions src/test/kotlin/com/almasb/tracery/TraceryTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -146,18 +146,18 @@ class TraceryTest {
assertThat(expansion, `is`("Izzi traveled with her pet raven. Izzi was never astute, for the raven was always too impassioned."))
}

// @Test
// fun `Regex selection`() {
// for (i in 1..15) {
// Tracery.setRandom(Random(i.toLong()))
//
// val json = readJSON("regex.json")
// val grammar = Tracery.createGrammar(json)
// val expansion = grammar.flatten("randomAnimal")
//
// assertThat(expansion, either(`is`("cow")).or(`is`("sparrow")))
// }
// }
@Test
fun `Regex selection`() {
for (i in 1..15) {
Tracery.setRandom(Random(i.toLong()))

val json = readJSON("regex.json")
val grammar = Tracery.createGrammar(json)
val expansion = grammar.flatten("randomAnimal")

assertThat(expansion, either(`is`("cow")).or(`is`("sparrow")))
}
}

@Test
fun `Num keyword`() {
Expand Down
2 changes: 1 addition & 1 deletion src/test/resources/com/almasb/tracery/regex.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"randomAnimal": ["{animal,.*w$}"],
"randomAnimal": ["{animal#.*w$#}"],
"animal": ["unicorn","raven","sparrow","scorpion","coyote","eagle","owl","lizard","zebra","duck","kitten", "cow"]
}

0 comments on commit dde3aa6

Please sign in to comment.