From 99d64e84ab6e498bcbbb6cd87ac4280f2d8ab84a Mon Sep 17 00:00:00 2001 From: AlmasB Date: Mon, 22 Jan 2018 16:54:26 +0000 Subject: [PATCH] the same symbol now expands each occurrence --- .../kotlin/com/almasb/tracery/EngModifiers.kt | 50 +++++++++++++++---- .../com/almasb/tracery/TraceryConcepts.kt | 7 ++- .../kotlin/com/almasb/tracery/TraceryTest.kt | 18 ++++++- .../resources/com/almasb/tracery/simple4.json | 4 ++ 4 files changed, 65 insertions(+), 14 deletions(-) create mode 100644 src/test/resources/com/almasb/tracery/simple4.json diff --git a/src/main/kotlin/com/almasb/tracery/EngModifiers.kt b/src/main/kotlin/com/almasb/tracery/EngModifiers.kt index dddb264..bdfc103 100644 --- a/src/main/kotlin/com/almasb/tracery/EngModifiers.kt +++ b/src/main/kotlin/com/almasb/tracery/EngModifiers.kt @@ -11,30 +11,56 @@ val ENG_MODIFIERS = load() private fun load(): List { + // 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 } } @@ -52,4 +78,6 @@ private fun add(name: String, func: (String, Array) -> String) { return func.invoke(s, args.toList().toTypedArray()) } }) -} \ No newline at end of file +} + +private fun Char.isVowel() = this in "aeiouAEIOU" \ No newline at end of file diff --git a/src/main/kotlin/com/almasb/tracery/TraceryConcepts.kt b/src/main/kotlin/com/almasb/tracery/TraceryConcepts.kt index b404cc0..c255e60 100644 --- a/src/main/kotlin/com/almasb/tracery/TraceryConcepts.kt +++ b/src/main/kotlin/com/almasb/tracery/TraceryConcepts.kt @@ -73,6 +73,10 @@ class Grammar() { private val symbols: HashMap = hashMapOf() + fun addSymbol(symbol: Symbol) { + symbols[symbol.key] = symbol + } + fun flatten() = flatten("#origin#") fun flatten(startSymbolKey: String): String { @@ -102,6 +106,7 @@ class Grammar() { var replacedValue = expand(newValue) + // TODO: extract apply modifiers if (it.contains(".")) { val modifiers = it.split(".").drop(1) @@ -115,7 +120,7 @@ class Grammar() { } } - newS = newS.replace("#$it#", replacedValue) + newS = newS.replaceFirst("#$it#", replacedValue) } return newS diff --git a/src/test/kotlin/com/almasb/tracery/TraceryTest.kt b/src/test/kotlin/com/almasb/tracery/TraceryTest.kt index ab535d9..1c64911 100644 --- a/src/test/kotlin/com/almasb/tracery/TraceryTest.kt +++ b/src/test/kotlin/com/almasb/tracery/TraceryTest.kt @@ -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 @@ -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`() { diff --git a/src/test/resources/com/almasb/tracery/simple4.json b/src/test/resources/com/almasb/tracery/simple4.json new file mode 100644 index 0000000..0fe4565 --- /dev/null +++ b/src/test/resources/com/almasb/tracery/simple4.json @@ -0,0 +1,4 @@ +{ + "sentence": ["#color#,#color#"], + "color": ["orange","blue","white","black","grey","purple","indigo","turquoise"] +} \ No newline at end of file