From 0fb2d1019f5d39715799440a386e47550c44c702 Mon Sep 17 00:00:00 2001 From: Henry Jameson Date: Fri, 26 Apr 2013 11:59:32 +0400 Subject: [PATCH] added word generator. makes funny words. fixed bug with using only "u" for syllableV --- src/Randomizer.scala | 3 ++ src/SyllableGenerator.scala | 4 +-- src/Word.scala | 31 ++++++++++++++++++ src/WordGenerator.scala | 25 +++++++++++++++ src/testing.sc | 63 ++++++++++++++++++++++--------------- 5 files changed, 99 insertions(+), 27 deletions(-) create mode 100644 src/Word.scala create mode 100644 src/WordGenerator.scala diff --git a/src/Randomizer.scala b/src/Randomizer.scala index 842231b..3114dc3 100644 --- a/src/Randomizer.scala +++ b/src/Randomizer.scala @@ -16,4 +16,7 @@ object Randomizer { def pickFrom[T](prob:List[T]): T = { prob(secuRnd.nextInt(prob.size)) } + def pickInt(min:Int,max:Int):Int = { + min + secuRnd.nextInt(max - min) + } } diff --git a/src/SyllableGenerator.scala b/src/SyllableGenerator.scala index 2830500..52807b7 100644 --- a/src/SyllableGenerator.scala +++ b/src/SyllableGenerator.scala @@ -73,8 +73,8 @@ class SyllableGenerator (val useX: Boolean, val sInsteadOfsh: Boolean = false, v } def generateSyllableF:Syllable = generateSyllableF(youonForce = false, youonProhibit = false) - def generateSyllableV(includeN:Boolean):Syllable = { - val letters = if (includeN) forAll:+"n" else forAll + def generateSyllableV(useN:Boolean):Syllable = { + val letters = if (useN) "n"::Nil else forCommon new Syllable("",Randomizer.pickFrom(letters),false) } diff --git a/src/Word.scala b/src/Word.scala new file mode 100644 index 0000000..0ff7d72 --- /dev/null +++ b/src/Word.scala @@ -0,0 +1,31 @@ +/** + * Created with IntelliJ IDEA. + * User: jcd + * Date: 4/26/13 + * Time: 11:04 AM + */ +class Word (val lentgh:Int){ + var numOfN = 0 + var numOfYouon = 0 + var numOfFSyllables = 0 + var numOfVSyllables = 0 + private val value:StringBuilder = new StringBuilder() + def append(syllable:Syllable){ + value.append(syllable.toString) + } + override def toString:String = { + value.toString() + } + def n():Int = { + numOfN / lentgh * 100 + } + def youyon():Int = { + numOfYouon / lentgh * 100 + } + def fsyl():Int = { + numOfFSyllables / lentgh * 100 + } + def vsyl():Int = { + numOfVSyllables / lentgh * 100 + } +} diff --git a/src/WordGenerator.scala b/src/WordGenerator.scala new file mode 100644 index 0000000..2af59fa --- /dev/null +++ b/src/WordGenerator.scala @@ -0,0 +1,25 @@ +/** + * Created with IntelliJ IDEA. + * User: jcd + * Date: 4/26/13 + * Time: 11:03 AM + */ +class WordGenerator(val sg:SyllableGenerator) { + def generateWord:Word= { + val word = new Word(Randomizer.pickInt(3,6)) + var lastYouon = false + for (i <- 0 to word.lentgh){ + val syllable = + if ((0 to 5 contains(word.n())) && Randomizer.pickWithProb(0.25)){ + sg.generateSyllableV(useN = true) + } else if ((0 to 25 contains(word.vsyl())) && Randomizer.pickWithProb(0.49)){ + sg.generateSyllableV(useN = false) + } else { + sg.generateSyllableF(youonForce = false, youonProhibit = lastYouon) + } + lastYouon = syllable.isYouon + word.append(syllable) + } + word + } +} diff --git a/src/testing.sc b/src/testing.sc index 2cd5772..6c75017 100644 --- a/src/testing.sc +++ b/src/testing.sc @@ -7,29 +7,42 @@ * sample workspace file to view how generator behaves. */ val s:SyllableGenerator = new SyllableGenerator(false, false, true) -s.generateSyllableF.toString() -s.generateSyllableF.toString() -s.generateSyllableF.toString() -s.generateSyllableF.toString() -s.generateSyllableF.toString() -s.generateSyllableF.toString() -s.generateSyllableF.toString() -s.generateSyllableF.toString() -s.generateSyllableF.toString() -s.generateSyllableF.toString() -s.generateSyllableF.toString() -s.generateSyllableF.toString() -s.generateSyllableF.toString() -s.generateSyllableF.toString() -s.generateSyllableF.toString() -s.generateSyllableF.toString() -s.generateSyllableF.toString() -s.generateSyllableF.toString() -s.generateSyllableF.toString() -s.generateSyllableF.toString() -s.generateSyllableF.toString() -s.generateSyllableF.toString() -s.generateSyllableF.toString() -s.generateSyllableF.toString() -s.generateSyllableF.toString() +val w:WordGenerator = new WordGenerator(s) +for (i <- 1 to 50){ + System.out.println (w.generateWord.toString+" ") +} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +