Advertisement
Guest User

Untitled

a guest
Nov 30th, 2015
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Scala 1.05 KB | None | 0 0
  1. case class Expression(rotule: String, word: String)
  2.  
  3. object Texts {
  4.  
  5.   def processSentence(str: String) : Map[String, Int] = {
  6.    
  7.     def loop(strLst: List[String], counter: Map[String, Int]): Map[String, Int] = {
  8.  
  9.       if (strLst.isEmpty || strLst.tail.isEmpty) {
  10.        
  11.         counter
  12.        
  13.       } else {    
  14.      
  15.         val n = Expression(strLst.head, strLst.head) match {
  16.           case Expression("#art", _) => "#art"
  17.           case Expression("#sub", _) => "#sub"
  18.           case Expression("#verbo", _) => "#verbo"
  19.           case Expression("#pron", _) => "#pron"
  20.         }
  21.        
  22.         if (counter.contains(n))
  23.           loop(strLst.tail.tail, counter - n + (n -> (counter(n) + 1)))
  24.         else
  25.           loop(strLst.tail.tail, counter + (n -> 1))
  26.       }
  27.     }
  28.    
  29.     val splittedStr = str.split(" ").toList
  30.     loop(splittedStr, Map())
  31.      
  32.   }
  33.  
  34.   def main(args: Array[String]) {
  35.     println(processSentence("#art O #sub rato #verbo roeu #art a #sub roupa #pron do #sub rei #pron de #sub roma"))
  36.   }
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement