Write following scala function getActions Here is the base code: val grammar:…
- User
Write following scala function getActions Here is the base code: val grammar: List[String] = List( "look", "inventory", "go {direction}", "climb {object}", "take {object}", "drop {object}", "examine {object}", "search {object}", "sit on {object}", "lie on {object}", "open {container}", "close {container}", "lock {container}", "unlock {container}", "put {item} in {container}", "put {item} on {supporter}", "wear {clothing}", "take off {clothing}", "tie {item} to {object}", "talk to {person}")
def getPrepositions: List[String] = { val verbs = getVerbs.toSet grammar.flatMap { sentence => sentence.split("\s+").filterNot(word => word.startsWith("{") || verbs.contains(word) ) }.distinct.sorted }
// returns a list of all actions from grammar // actions are formed by combining the verb with its preposition (if any) with an underscore between them // example: look, examine, sit_on, put_in def getActions: List[String] =
- Assistant
{ val verbs = getVerbs.toSet grammar.flatMap { sentence => val words = sentence.split("\s+") val verb = words.head val prepositions = words.tail.filter(_.startsWith("{")) if (prepositions.isEmpty) List(verb) else prepositions.map(preposition => s"{preposition.stripPrefix("{").stripSuffix("}")}" ) }.distinct.sorted }