jweinst1
5/21/2017 - 4:47 AM

generate noun system

generate noun system

//CFG generator example
//tries to use a cfg to generate language

import scala.util.Random

object main {
  def main():Unit = {
    for(_ <- 1 to 10) println(NounPhrase())
  }
}

object sample {
  //gets random element from array
  def arr[T](items:Array[T]):T = {
    items(Random.nextInt(items.length))
  }
}

//abstract class of phrase production construct
abstract class Phrase {
  def apply():String
}
//top level object for NP
object NounPhrase extends Phrase {
  val children:Array[Phrase] = Array(Noun, Determiner)
  
  def apply():String = sample.arr(children)()
}

//second level object
object Noun extends Phrase {
  val children:Array[String] = Array("apple", "ball", "store")
  
  def apply():String = sample.arr(children)
}

//second level object
object Determiner extends Phrase {
  val children:Array[String] = Array("the only", "a", "the")
  
  def apply():String = sample.arr(children)
}

main.main()