2017-02-02 20 views
1

类似树json解析引用here,我想实现一个简单的可视化决策树在斯卡拉。它与databricks笔记本电脑中的显示方法完全相同。转换火花决策树模型调试字符串嵌套在斯卡拉JSON

我是scala的新手,很难获得正确的逻辑。我知道我们必须进行递归调用来构建孩子,并在显示最终预测值时断开。我试图代码使用这里下面提到的输入模型调试字符串

def getStatmentType(x: String): (String, String) = { 
    val ifPattern = "If+".r 
    val ifelsePattern = "Else+".r 
    var t = ifPattern.findFirstIn(x.toString) 
    if(t != None){ 
     ("If", (x.toString).replace("If","")) 
    }else { 
     var ts = ifelsePattern.findFirstIn(x.toString) 
     if(ts != None) ("Else", (x.toString).replace("Else", "")) 
     else ("None", (x.toString).replace("(", "").replace(")","")) 
    } 
    } 
    def delete[A](test:List[A])(i: Int) = test.take(i) ++ test.drop((i+1)) 
    def BuildJson(tree:List[String]):List[Map[String, Any]] = { 
    var block:List[Map[String, Any]] = List() 
    var lines:List[String] = tree 
    loop.breakable { 
     while (lines.length > 0) { 
     println("here") 
     var (cond, name) = getStatmentType(lines(0)) 
     println("initial" + cond) 
     if (cond == "If") { 
      println("if" + cond) 
     // lines = lines.tail 
      lines = delete(lines)(0) 
      block = block :+ Map("if-name" -> name, "children" -> BuildJson(lines)) 
      println("After pop Else State"+lines(0)) 
      val (p_cond, p_name) = getStatmentType(lines(0)) 
     // println(p_cond + " = "+ p_name+ "\n") 
      cond = p_cond 
      name = p_name 
      println(cond + " after="+ name+ "\n") 
      if (cond == "Else") { 
      println("else" + cond) 
      lines = lines.tail 
      block = block :+ Map("else-name" -> name, "children" -> BuildJson(lines)) 
      } 
     }else if(cond == "None") { 
      println(cond + "NONE") 
      lines = delete(lines)(0) 
      block = block :+ Map("predict" -> name) 
     }else { 
      println("Finaly Break") 
      println("While loop--" +lines) 
      loop.break() 

     } 
     } 
    } 
    block 
    } 

    def treeJson1(str: String):JsValue = { 
    val str = "If (feature 0 in {1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,10.0,11.0,12.0,13.0})\n If (feature 0 in {6.0})\n  Predict: 17.0\n Else (feature 0 not in {6.0})\n  Predict: 6.0\n Else (feature 0 not in {1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,10.0,11.0,12.0,13.0})\n Predict: 20.0" 
    val x = str.replace(" ","") 
    val xs = x.split("\n").toList 
    var js = BuildJson(xs) 
    println(MapReader.mapToJson(js)) 
    Json.toJson("") 
    } 

预期输出:

[ 
    { 
    'name': 'Root', 
    'children': [ 
     { 
    'name': 'feature 0 in {1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,10.0,11.0,12.0,13.0}', 
    'children': [ 
     { 
     'name': 'feature 0 in {6.0}', 
     'children': [ 
      { 
      'name': 'Predict: 17.0' 
      } 
     ] 
     }, 
     { 
     'name': 'feature 0 not in {6.0}', 
     'children': [ 
      { 
      'name': 'Predict: 6.0' 
      } 
     ] 
     } 
    ] 
    }, 
    { 
    'name': 'feature 0 not in {1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,10.0,11.0,12.0,13.0}', 
    'children': [ 
     { 
     'name': 'Predict: 20.0' 
     } 
    ] 
    } 
] 

回答