Skip to content

Commit

Permalink
the same symbol now expands each occurrence
Browse files Browse the repository at this point in the history
  • Loading branch information
AlmasB committed Jan 22, 2018
1 parent c91cd2e commit 99d64e8
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 14 deletions.
50 changes: 39 additions & 11 deletions src/main/kotlin/com/almasb/tracery/EngModifiers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,56 @@ val ENG_MODIFIERS = load()

private fun load(): List<Modifier> {

// TODO: tighter defn for modifiers, e.g. text (s) cannot be empty

add("capitalize", { s, args ->
if (s.isEmpty())
return@add s

val char0 = s[0]
char0.toUpperCase() + s.drop(1)
s.first().toUpperCase() + s.drop(1)
})


add("s", { s, args ->
if (s.isEmpty())
return@add s

when (s.last()) {
in "shx" -> return@add s + "es"
'y' -> {
if (!s.dropLast(1).last().isVowel()) {
return@add s.dropLast(1) + "ies"
}
}
}

s + "s"
})

add("ed", { s, args ->
if (s.isEmpty())
return@add s

return@add when (s.last()) {
//in "shx" -> s + "ed"
'e' -> s + "d"
'y' -> {
if (!s.dropLast(1).last().isVowel()) {
s.dropLast(1) + "ied"
} else {
s + "d"
}
}
else -> s + "ed"
}
})

add("a", { s, args ->
if (s.isNotEmpty()) {
if (s[0] in "uU") {
if (s.length > 2) {
if (s[2] in "iI") {
return@add "a " + s
}
}
if (s[0] in "uU" && s.length > 2 && s[2] in "iI") {
return@add "a " + s
}

if (s[0] in "aeiouAEIOU") {
if (s[0].isVowel()) {
return@add "an " + s
}
}
Expand All @@ -52,4 +78,6 @@ private fun add(name: String, func: (String, Array<String>) -> String) {
return func.invoke(s, args.toList().toTypedArray())
}
})
}
}

private fun Char.isVowel() = this in "aeiouAEIOU"
7 changes: 6 additions & 1 deletion src/main/kotlin/com/almasb/tracery/TraceryConcepts.kt
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ class Grammar() {

private val symbols: HashMap<String, Symbol> = hashMapOf()

fun addSymbol(symbol: Symbol) {
symbols[symbol.key] = symbol
}

fun flatten() = flatten("#origin#")

fun flatten(startSymbolKey: String): String {
Expand Down Expand Up @@ -102,6 +106,7 @@ class Grammar() {

var replacedValue = expand(newValue)

// TODO: extract apply modifiers
if (it.contains(".")) {
val modifiers = it.split(".").drop(1)

Expand All @@ -115,7 +120,7 @@ class Grammar() {
}
}

newS = newS.replace("#$it#", replacedValue)
newS = newS.replaceFirst("#$it#", replacedValue)
}

return newS
Expand Down
18 changes: 16 additions & 2 deletions src/test/kotlin/com/almasb/tracery/TraceryTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ package com.almasb.tracery
import org.hamcrest.MatcherAssert
import org.hamcrest.MatcherAssert.assertThat
import org.hamcrest.Matchers
import org.hamcrest.Matchers.`is`
import org.hamcrest.Matchers.either
import org.hamcrest.Matchers.*
import org.junit.jupiter.api.Test
import java.nio.file.Files
import java.nio.file.Paths
Expand Down Expand Up @@ -59,6 +58,21 @@ class TraceryTest {
assertThat(expandedText, `is`("The purple owl of the mountain is called Mia"))
}

@Test
fun `The same symbol expands to random text`() {

Tracery.setRandom(Random(0))

val json = readJSON("simple4.json")

val grammar = Tracery.createGrammar(json)
val expandedText = grammar.flatten("#sentence#")

val tokens = expandedText.split(",")

assertThat(tokens[0], `is`(not(tokens[1])))
}

@Test
fun `Modifiers`() {

Expand Down
4 changes: 4 additions & 0 deletions src/test/resources/com/almasb/tracery/simple4.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"sentence": ["#color#,#color#"],
"color": ["orange","blue","white","black","grey","purple","indigo","turquoise"]
}

0 comments on commit 99d64e8

Please sign in to comment.