From 146751ba709110c2941ad49e03c204044ee0a9b9 Mon Sep 17 00:00:00 2001 From: Benjamin Van Treese Date: Mon, 27 Apr 2020 11:56:42 -0500 Subject: [PATCH] Lots of cleanup, fix some rewrite rules, better example --- docs/index.html | 9 +- docs/index.js | 37 +++++++- docs/langgen.js | 146 +++++++++++++++++++------------ src/dropecho/langgen/Consts.hx | 24 +++-- src/dropecho/langgen/Language.hx | 64 ++++++++------ src/dropecho/langgen/Rewrite.hx | 10 ++- src/dropecho/langgen/Spell.hx | 6 +- test.hxml | 18 ++-- test/langgen/RewriteTest.hx | 73 ++++++++++++++-- test/langgen/SpellTest.hx | 5 -- 10 files changed, 269 insertions(+), 123 deletions(-) diff --git a/docs/index.html b/docs/index.html index 5ae9323..6814b09 100644 --- a/docs/index.html +++ b/docs/index.html @@ -51,9 +51,12 @@
- -
-
+
+
+
+
+
+
diff --git a/docs/index.js b/docs/index.js index 356a6d2..6607b7e 100644 --- a/docs/index.js +++ b/docs/index.js @@ -30,10 +30,12 @@ function generate(){ let languagename = gen.createWord('language'); + document.getElementById('langname').innerHTML = + `The language of ${ languagename }`; + var configtable = document.getElementById('config'); configtable.innerHTML = 'Config'; - function addConfigRow(name, data) { var row = document.createElement('tr'); configtable.append(row); @@ -57,8 +59,37 @@ function generate(){ addConfigRow('max syllables in word', gen.config.word_length_max); - document.getElementById('langname').innerHTML = - `The language of ${ languagename }`; + var orthoTable = document.getElementById('ortho'); + orthoTable.innerHTML = 'PhonemeWritten'; + + function addOrthoRow(letter, ortho) { + var row = document.createElement('tr'); + orthoTable.append(row); + + var configName = document.createElement('td'); + configName.innerText = letter; + var configValue = document.createElement('td'); + configValue.innerText = ortho; + row.append(configName); + row.append(configValue); + } + + var letters = gen.config.vowels + .concat(gen.config.consonants) + .concat(gen.config.sset) + .concat(gen.config.lset) + .concat(gen.config.fset) + .reduce((a, b) => { + if(a.indexOf(b) === -1) { + a.push(b); + } + + return a; + }, []); + + for(let i = 0; i < letters.length; i++) { + addOrthoRow(letters[i], gen.spell.spell(letters[i])); + } let wordTable = document.getElementById('words'); diff --git a/docs/langgen.js b/docs/langgen.js index 91e0d51..2edbde7 100644 --- a/docs/langgen.js +++ b/docs/langgen.js @@ -1184,6 +1184,38 @@ class haxe_ds__$Map_Map_$Impl_$ { $hxClasses["haxe.ds._Map.Map_Impl_"] = haxe_ds__$Map_Map_$Impl_$; haxe_ds__$Map_Map_$Impl_$.__name__ = "haxe.ds._Map.Map_Impl_"; class dropecho_langgen_Consts { + static getRandomCorthSet(random) { + var this1 = dropecho_langgen_Consts.corthsets; + var _g = []; + var k = dropecho_langgen_Consts.corthsets.keys(); + while(k.hasNext()) { + var k1 = k.next(); + _g.push(k1); + } + var key = random.choice(_g); + var _this = this1; + if(__map_reserved[key] != null) { + return _this.getReserved(key); + } else { + return _this.h[key]; + } + } + static getRandomVorthSet(random) { + var this1 = dropecho_langgen_Consts.vorthsets; + var _g = []; + var k = dropecho_langgen_Consts.vorthsets.keys(); + while(k.hasNext()) { + var k1 = k.next(); + _g.push(k1); + } + var key = random.choice(_g); + var _this = this1; + if(__map_reserved[key] != null) { + return _this.getReserved(key); + } else { + return _this.h[key]; + } + } static getRandomConsonantSet(random) { var _g = []; var x = dropecho_langgen_Consts.consonant_sets.keys(); @@ -1295,8 +1327,8 @@ class dropecho_langgen_Language { var tmp5 = dropecho_langgen_Consts.getRandomSSet(this.random); var tmp6 = dropecho_langgen_Consts.getRandomLSet(this.random); var tmp7 = dropecho_langgen_Consts.getRandomFSet(this.random); - randommin = this.random.randomInt(1,3); - tmp = { consonants : tmp1, vowels : tmp2, syllable_structure : tmp3, phrase_structure : tmp4, sset : tmp5, lset : tmp6, fset : tmp7, word_length_min : randommin, word_length_max : this.random.randomInt(randommin + 1,randommin + this.random.randomInt(0,5))}; + randommin = this.random.randomInt(1,2); + tmp = { consonants : tmp1, vowels : tmp2, syllable_structure : tmp3, phrase_structure : tmp4, sset : tmp5, lset : tmp6, fset : tmp7, word_length_min : randommin, word_length_max : this.random.randomInt(randommin + 1,randommin + this.random.randomInt(1,4))}; } this.config = tmp; this.spell = new dropecho_langgen_Spell(); @@ -1304,32 +1336,33 @@ class dropecho_langgen_Language { this.definite = this.createWord("the",1,1); } createSyllable() { + var split = this.config.syllable_structure.split(""); var _g = []; var _g1 = 0; - var _g2 = this.config.syllable_structure.split(""); - while(_g1 < _g2.length) { - var x = _g2[_g1]; - ++_g1; - if(x.split("")[0] == "?") { - if(this.random.random() > 0.5) { - continue; - } + var _g2 = split.length; + while(_g1 < _g2) { + var x = _g1++; + if(split[x] == "?") { + continue; + } + if(x < split.length - 1 && split[x + 1] == "?" && this.random.random() > 0.5) { + continue; } var tmp; - switch(x) { - case "?F":case "F": - tmp = this.random.choice(this.config.fset); - break; - case "?C":case "C": + switch(split[x]) { + case "C": tmp = this.random.choice(this.config.consonants); break; - case "?L":case "L": + case "F": + tmp = this.random.choice(this.config.fset); + break; + case "L": tmp = this.random.choice(this.config.lset); break; - case "?S":case "S": + case "S": tmp = this.random.choice(this.config.sset); break; - case "?V":case "V": + case "V": tmp = this.random.choice(this.config.vowels); break; default: @@ -1405,52 +1438,57 @@ class dropecho_langgen_Language { if(tmp) { var _this1 = this.words; subject = __map_reserved[key] != null ? _this1.getReserved(key) : _this1.h[key]; + } else { + subject = this.createWord(); } + var split = this.config.phrase_structure.split(""); var _g = []; var _g1 = 0; - var _g2 = this.config.phrase_structure.split(""); - while(_g1 < _g2.length) { - var x = _g2[_g1]; - ++_g1; - if(x.split("")[0] == "?" && this.random.random() > 0.5) { + var _g2 = split.length; + while(_g1 < _g2) { + var x = _g1++; + if(split[x] == "?") { + continue; + } + if(x < split.length - 1 && split[x + 1] == "?" && this.random.random() > 0.5) { continue; } var phrase; - switch(x) { - case "?D":case "D": + switch(split[x]) { + case "D": phrase = this.definite; break; - case "?G":case "G": + case "G": phrase = this.genitive; break; - case "?N":case "N": - if(this.random.random() > 0.2) { - var _this2 = this.words; - var key1 = this.random; - var _g11 = []; - var k = this.words.keys(); - while(k.hasNext()) { - var k1 = k.next(); - _g11.push(k1); - } - var _g3 = []; - var _g12 = 0; - var _g21 = _g11; - while(_g12 < _g21.length) { - var v = _g21[_g12]; - ++_g12; - if(v != "the" && v != "of") { - _g3.push(v); - } + case "N": + var _g11 = []; + var k = this.words.keys(); + while(k.hasNext()) { + var k1 = k.next(); + _g11.push(k1); + } + var _g3 = []; + var _g12 = 0; + var _g21 = _g11; + while(_g12 < _g21.length) { + var v = _g21[_g12]; + ++_g12; + if(v != "the" && v != "of") { + _g3.push(v); } - var key2 = key1.choice(_g3); - phrase = __map_reserved[key2] != null ? _this2.getReserved(key2) : _this2.h[key2]; + } + var choices = _g3; + if(this.random.random() > 0.2 && choices.length > 0) { + var _this2 = this.words; + var key1 = this.random.choice(choices); + phrase = __map_reserved[key1] != null ? _this2.getReserved(key1) : _this2.h[key1]; } else { phrase = this.createWord(); } break; case "S": - phrase = subject != null ? subject : this.createWord(key); + phrase = subject; break; default: phrase = ""; @@ -1458,7 +1496,7 @@ class dropecho_langgen_Language { _g.push(phrase); } var phrase1 = _g.join(" "); - return this.spell.spell(phrase1); + return phrase1; } translate(text) { var tokens = StringTools.trim(text).split(" "); @@ -1520,11 +1558,11 @@ class dropecho_langgen_Rewrite { var _this_r1 = new RegExp("V","g".split("u").join("")); rule = rule.replace(_this_r1,"[" + vowels + "]{1}"); var _this_r2 = new RegExp("_","g".split("u").join("")); - rule = rule.replace(_this_r2,"([" + char + "]{1})"); + rule = rule.replace(_this_r2,"(" + char + ")"); return rule; } addRule(char,rule,replaceWith) { - var reg = new EReg(this.parseRule(char,rule),""); + var reg = new EReg(this.parseRule(char,rule),"g"); this.rules.set(reg,replaceWith); } rewrite(s) { @@ -1548,11 +1586,9 @@ Object.assign(dropecho_langgen_Rewrite.prototype, { }); class dropecho_langgen_Spell { constructor(ortho) { + var random = new seedyrng_Random(); if(ortho == null) { - var _this = dropecho_langgen_Consts.corthsets; - var tmp = __map_reserved["Default"] != null ? _this.getReserved("Default") : _this.h["Default"]; - var _this1 = dropecho_langgen_Consts.vorthsets; - this.ortho = { consonants : tmp, vowels : __map_reserved["Default"] != null ? _this1.getReserved("Default") : _this1.h["Default"]}; + this.ortho = { consonants : dropecho_langgen_Consts.getRandomCorthSet(random), vowels : dropecho_langgen_Consts.getRandomVorthSet(random)}; } else { this.ortho = ortho; } diff --git a/src/dropecho/langgen/Consts.hx b/src/dropecho/langgen/Consts.hx index ef1fb42..25a1a85 100644 --- a/src/dropecho/langgen/Consts.hx +++ b/src/dropecho/langgen/Consts.hx @@ -1,5 +1,7 @@ package dropecho.langgen; +import seedyrng.Random; + @:expose("Consts") class Consts { public static var consonant_sets = [ @@ -87,37 +89,45 @@ class Consts { 'Doubles' => ['A' => 'aa', 'E' => 'ee', 'I' => 'ii', 'O' => 'oo', 'U' => 'uu'] ]; - public static function getRandomConsonantSet(random) { + public static function getRandomCorthSet(random:Random) { + return corthsets.get(random.choice([for (k in corthsets.keys()) k])); + } + + public static function getRandomVorthSet(random:Random) { + return vorthsets.get(random.choice([for (k in vorthsets.keys()) k])); + } + + public static function getRandomConsonantSet(random:Random) { var keys = [for (x in consonant_sets.keys()) x]; return consonant_sets[random.choice(keys)]; } - public static function getRandomVowelSet(random) { + public static function getRandomVowelSet(random:Random) { var keys = [for (x in vowel_sets.keys()) x]; return vowel_sets[random.choice(keys)]; } - public static function getRandomSSet(random) { + public static function getRandomSSet(random:Random) { var keys = [for (x in ssets.keys()) x]; return ssets[random.choice(keys)]; } - public static function getRandomFSet(random) { + public static function getRandomFSet(random:Random) { var keys = [for (x in fsets.keys()) x]; return fsets[random.choice(keys)]; } - public static function getRandomLSet(random) { + public static function getRandomLSet(random:Random) { var keys = [for (x in lsets.keys()) x]; return lsets[random.choice(keys)]; } - public static function getRandomSyllableStructure(random) { + public static function getRandomSyllableStructure(random:Random) { return random.choice(syllable_structures); } - public static function getRandomPhraseStructure(random) { + public static function getRandomPhraseStructure(random:Random) { return random.choice(phrase_structures); } } diff --git a/src/dropecho/langgen/Language.hx b/src/dropecho/langgen/Language.hx index ac40d9a..1469a4c 100644 --- a/src/dropecho/langgen/Language.hx +++ b/src/dropecho/langgen/Language.hx @@ -54,8 +54,8 @@ class Language { sset: Consts.getRandomSSet(random), lset: Consts.getRandomLSet(random), fset: Consts.getRandomFSet(random), - word_length_min: randommin = random.randomInt(1, 3), - word_length_max: random.randomInt(randommin + 1, randommin + random.randomInt(0, 5)) + word_length_min: randommin = random.randomInt(1, 2), + word_length_max: random.randomInt(randommin + 1, randommin + random.randomInt(1, 4)) }; spell = new Spell(); @@ -66,22 +66,25 @@ class Language { } public function createSyllable():String { + var split = config.syllable_structure.split(""); return [ - for (x in config.syllable_structure.split("")) { - if (x.split("")[0] == "?") { - if (this.random.random() > 0.5) - continue; - } - switch (x) { - case "C" | "?C": + for (x in 0...split.length) { + if (split[x] == "?") + continue; + + if (x < split.length - 1 && split[x + 1] == "?" && random.random() > 0.5) + continue; + + switch (split[x]) { + case "C": random.choice(config.consonants); - case "V" | "?V": + case "V": random.choice(config.vowels); - case "S" | "?S": + case "S": random.choice(config.sset); - case "L" | "?L": + case "L": random.choice(config.lset); - case "F" | "?F": + case "F": random.choice(config.fset); case _: ""; @@ -102,10 +105,7 @@ class Language { return words.get(key); } - var word = [ - for (_ in 0...random.randomInt(min, max)) - this.createSyllable() - ].join(""); + var word = [for (_ in 0...random.randomInt(min, max)) createSyllable()].join(""); word = spell.spell(word); @@ -114,38 +114,46 @@ class Language { trans_words.set(word, key); } - return word; // spell.spell(word); + return word; } public function createPhrase(?key:String):String { var subject = null; if (key != null && words.exists(key)) { subject = words.get(key); + } else { + subject = createWord(); } + var split = config.phrase_structure.split(""); var phrase = [ - for (x in config.phrase_structure.split("")) { - if (x.split("")[0] == "?" && this.random.random() > 0.5) + for (x in 0...split.length) { + if (split[x] == "?") continue; - switch (x) { - case "D" | "?D": + + if (x < split.length - 1 && split[x + 1] == "?" && random.random() > 0.5) + continue; + + switch (split[x]) { + case "D": definite; - case "G" | "?G": + case "G": genitive; - case "N" | "?N": - if (this.random.random() > 0.2) { - words.get(random.choice([for (k in words.keys()) k].filter(x -> x != "the" && x != "of"))); + case "N": + var choices = [for (k in words.keys()) k].filter(x -> x != "the" && x != "of"); + if (this.random.random() > 0.2 && choices.length > 0) { + words.get(random.choice(choices)); } else { createWord(); } case "S": - subject != null ? subject : createWord(key); + subject; case _: ""; } } ].join(" "); - return spell.spell(phrase); + return phrase; } public function translate(text:String) { diff --git a/src/dropecho/langgen/Rewrite.hx b/src/dropecho/langgen/Rewrite.hx index 944706b..b25ed2e 100644 --- a/src/dropecho/langgen/Rewrite.hx +++ b/src/dropecho/langgen/Rewrite.hx @@ -2,6 +2,8 @@ package dropecho.langgen; import dropecho.langgen.Language.LanguageConfig; +using StringTools; + class Rewrite { public var rules:Map = new Map(); public var config:LanguageConfig; @@ -16,20 +18,20 @@ class Rewrite { rule = ~/C/g.replace(rule, '[$consts]{1}'); rule = ~/V/g.replace(rule, '[$vowels]{1}'); - rule = ~/_/g.replace(rule, '([$char]{1})'); + rule = ~/_/g.replace(rule, '($char)'); return rule; } public function addRule(char:String, rule:String, replaceWith:String) { - var reg = new EReg(parseRule(char, rule), ''); - rules.set(reg, replaceWith); + var reg = new EReg(parseRule(char, rule), 'g'); + rules.set(reg, replaceWith); } public function rewrite(s:String) { var after = s; for (rule => replaceWith in rules) { - after = rule.replace(after, replaceWith); + after = rule.replace(after, replaceWith); } return after; diff --git a/src/dropecho/langgen/Spell.hx b/src/dropecho/langgen/Spell.hx index 874cb4e..3c52d04 100644 --- a/src/dropecho/langgen/Spell.hx +++ b/src/dropecho/langgen/Spell.hx @@ -1,5 +1,6 @@ package dropecho.langgen; +import seedyrng.Random; import dropecho.langgen.Language.LanguageOrthography; @:expose("Spell") @@ -7,10 +8,11 @@ class Spell { public var ortho:LanguageOrthography; public function new(?ortho:LanguageOrthography) { + var random = new Random(); if (ortho == null) { this.ortho = { - consonants: Consts.corthsets["Default"], - vowels: Consts.vorthsets["Default"] + consonants: Consts.getRandomCorthSet(random), + vowels: Consts.getRandomVorthSet(random) } } else { this.ortho = ortho; diff --git a/test.hxml b/test.hxml index 429eba2..2df9fff 100755 --- a/test.hxml +++ b/test.hxml @@ -9,15 +9,15 @@ # -swf build/as3_test.swf ## JavaScript ---next --main TestMain --lib munit --lib hamcrest --lib seedyrng --cp src - --cp test --js build/js_test.js +# --next +# -main TestMain +# -lib munit +# -lib hamcrest +# -lib seedyrng +# -cp src +# +# -cp test +# -js build/js_test.js ## Neko # --next diff --git a/test/langgen/RewriteTest.hx b/test/langgen/RewriteTest.hx index 69241c5..e686108 100644 --- a/test/langgen/RewriteTest.hx +++ b/test/langgen/RewriteTest.hx @@ -6,8 +6,8 @@ import dropecho.langgen.*; class RewriteTest { public var rewriter:Rewrite; - @BeforeClass - public function suiteSetup() { + @Before + public function setup() { var config = { consonants: Consts.consonant_sets['Minimal'], vowels: Consts.vowel_sets['Default'], @@ -15,7 +15,9 @@ class RewriteTest { phrase_structure: Consts.phrase_structures[0], fset: [], sset: [], - lset: [] + lset: [], + word_length_min: 1, + word_length_max: 5 }; rewriter = new Rewrite(config); @@ -29,7 +31,7 @@ class RewriteTest { @Test public function parseRuleConsonant() { var test = '_C'; - var expected = '([d]{1})[ptkmnls]{1}'; + var expected = '(d)[ptkmnls]{1}'; var out = rewriter.parseRule('d', test); @@ -39,7 +41,7 @@ class RewriteTest { @Test public function parseRulePostFix() { var test = '_V'; - var expected = '([d]{1})[aeiou]{1}'; + var expected = '(d)[aeiou]{1}'; var out = rewriter.parseRule('d', test); @@ -49,7 +51,7 @@ class RewriteTest { @Test public function parseRulePostFixMultiple() { var test = '_VV'; - var expected = '([d]{1})[aeiou]{1}[aeiou]{1}'; + var expected = '(d)[aeiou]{1}[aeiou]{1}'; var out = rewriter.parseRule('d', test); @@ -59,7 +61,7 @@ class RewriteTest { @Test public function parseRulePreFix() { var test = 'V_'; - var expected = '[aeiou]{1}([d]{1})'; + var expected = '[aeiou]{1}(d)'; var out = rewriter.parseRule('d', test); @@ -100,4 +102,61 @@ class RewriteTest { Assert.areEqual(expected, after); } + + @Test public function moreSpecificRule() { + var rule = rewriter.parseRule("d", "_im"); + rewriter.addRule("d", "_im", "de"); + + var before = 'regdim'; + var expected = 'regde'; + + var after = rewriter.rewrite(before); + + Assert.areEqual("(d)im", rule); + Assert.areEqual(expected, after); + } + + @Test public function doubleLetter() { + rewriter.addRule("oo", "_", "u"); + + var before = 'roog'; + var expected = 'rug'; + + var after = rewriter.rewrite(before); + + Assert.areEqual(expected, after); + } + + @Test public function beginning_of_word() { + rewriter.addRule("oo", "^_", "u"); + + var before = 'ooroog'; + var expected = 'uroog'; + + var after = rewriter.rewrite(before); + + Assert.areEqual(expected, after); + } + + @Test public function end_of_word() { + rewriter.addRule("oo", "_$", "u"); + + var before = 'roogoo'; + var expected = 'roogu'; + + var after = rewriter.rewrite(before); + + Assert.areEqual(expected, after); + } + + @Test public function end_of_word_2() { + rewriter.addRule("oo", "_V$", "ui"); + + var before = 'roogooi'; + var expected = 'roogui'; + + var after = rewriter.rewrite(before); + + Assert.areEqual(expected, after); + } } diff --git a/test/langgen/SpellTest.hx b/test/langgen/SpellTest.hx index 8e8ad5d..52a7f93 100644 --- a/test/langgen/SpellTest.hx +++ b/test/langgen/SpellTest.hx @@ -15,11 +15,6 @@ class SpellTest { speller = new Spell(ortho); } - @Test - public function whaaat() { - trace('ʃ'); - } - @Test public function canInstantiate() { Assert.isNotNull(speller);