@@ -10,9 +10,13 @@ private val NotFound = -2
1010private val Optional = - 3
1111private val tokenizer = Tokenizer ()
1212
13- private fun coverage (scanned : Int , total : Int ) = scanned .toDouble() / total.toDouble()
13+ private fun coverage (matched : Int , total : Int ) = matched .toDouble() / total.toDouble()
1414
15- data class Evaluation (val value : Number = Double .NaN , val expr : List <String > = ArrayList (), val variables : Map <String , Double > = HashMap (), val coverage : Double = 0.0 )
15+ data class Evaluation (val value : Number = Double .NaN ,
16+ val expr : List <String > = ArrayList (),
17+ val variables : Map <String , Double > = HashMap (),
18+ val coverage : Double = 0.0 ,
19+ val rule : String = " " )
1620
1721class RuleBuilder (source : String , val name : String = " " ) {
1822 private val parser = RuleParser ()
@@ -53,16 +57,14 @@ class RuleBuilder(source: String, val name: String = "") {
5357 val tokens = tokenizer.tokenize(text)
5458 var offset = BaseOffset
5559 var remained = rule().size
56- var coverage = 0.0
5760 rule().forEach {
5861 val (index, optional) = indexOf(it, tokens, start = offset + 1 , end = Math .min(tokens.size - remained, tokens.lastIndex))
59- if (index == NotFound ) return Pair (false , coverage(index, tokens.size) )
62+ if (index == NotFound ) return Pair (false , 0.0 )
6063 if (it.id.isNotEmpty()) recognizedId(Pair (it.id, if (optional) it.optionalValue().toString() else tokens[index].surface))
6164 if (! optional) offset = index
62- coverage = coverage(index, tokens.size)
6365 remained--
6466 }
65- return Pair (true , coverage)
67+ return Pair (true , coverage(rule().size, tokens.size) )
6668 }
6769
6870 fun eval (text : String ): Evaluation {
@@ -72,9 +74,9 @@ class RuleBuilder(source: String, val name: String = "") {
7274 return if (matched) {
7375 args.forEach { calc.eval(it) }
7476 expr().forEach { calc.eval(it) }
75- Evaluation (calc.value, expr().toList(), calc.variables(), coverage)
77+ Evaluation (calc.value, expr().toList(), calc.variables(), coverage, name )
7678 } else {
77- Evaluation ()
79+ Evaluation (rule = name )
7880 }
7981 }
8082
0 commit comments