Commits

vvcephei  committed dc7a42d

Excising the abomination that was Comparer.scala

  • Participants
  • Parent commits 2a67015

Comments (0)

Files changed (2)

File src/main/scala/opennlp/scalabha/tree/Comparer.scala

-package opennlp.scalabha.tree
-
-import org.clapper.argot._
-import opennlp.scalabha.model._
-import opennlp.scalabha.log.SimpleLogger
-import java.io.{OutputStreamWriter, BufferedWriter}
-
-object Comparer {
-
-  import ArgotConverters._
-
-  val parser = new ArgotParser(this.getClass.getName, preUsage = Some("Version 0.0"))
-  val help = parser.flag[Boolean](List("h", "help"), "print help")
-  val left = parser.option[String](List("l", "left"), "FILE", "left tree inputFile for comparison")
-  val right = parser.option[String](List("r", "right"), "FILE", "right tree inputFile for comparison")
-  val leftLog = new SimpleLogger(this.getClass.getName+"(LEFT FILE)", SimpleLogger.WARN, new BufferedWriter(new OutputStreamWriter(System.err)))
-  val rightLog = new SimpleLogger(this.getClass.getName+"(RIGHT FILE)", SimpleLogger.WARN, new BufferedWriter(new OutputStreamWriter(System.err)))
-  val log = new SimpleLogger(this.getClass.getName, SimpleLogger.WARN, new BufferedWriter(new OutputStreamWriter(System.err)))
-
-  abstract class ComparisonResult
-
-  case class Success() extends ComparisonResult{
-    override def toString(): String = {
-      "OK"
-    }
-    val bool = true
-  }
-
-  case class Fail[T](index: Int, left: T, right: T) extends ComparisonResult {
-    override def toString(): String = {
-      "FAIL: Line %d <<%s>>!=<<%s>>".format(index, left, right)
-    }
-    val bool = false
-  }
-
-  def apply(index: Int, left: TreeNode, right: TreeNode): ComparisonResult = {
-    if (left.compareStructure(right))
-      Success()
-    else
-      Fail[TreeNode](index, left, right)
-  }
-
-  def apply(index: Int, left: String, right: String): ComparisonResult = {
-    val (leftOption, rightOption) = (Parser(index, left, leftLog), Parser(index, right, rightLog))
-    if (
-      if (leftOption.isDefined && rightOption.isDefined) {
-        apply(index, leftOption.get, rightOption.get) match {
-          case Success() =>
-            true
-          case _ =>
-            false
-        }
-      } else if (leftOption.isDefined) {
-        log.err("Line %d: Could not parse RIGHT line:<<%s>>".format(index, right))
-        false
-      } else if (rightOption.isDefined) {
-        log.err("Line %d: Could not parse LEFT line:<<%s>>".format(index, right))
-        false
-      } else {
-        log.err("Line %d: Unable to parse lines: left:<<%s>> right:<<%s>>\n".format(index, left, right))
-        false
-      })
-      Success()
-    else
-      Fail[String](index, left, right)
-  }
-
-  def burnRemainder(toRead: Iterator[String], log: SimpleLogger) {
-    for (line <- toRead) {
-      log.err("found an extra line:<<%s>>\n".format(line))
-    }
-  }
-
-  def main(args: Array[String]) {
-    try {
-      parser.parse(args)
-
-      if (help.value.isDefined) {
-        parser.usage()
-      }
-
-      val left_file =
-        (if (left.value.isDefined) {
-          scala.io.Source.fromFile(left.value.get, "UTF-8").getLines()
-        } else {
-          parser.usage()
-        })
-
-      val right_file =
-        (if (right.value.isDefined) {
-          scala.io.Source.fromFile(right.value.get, "UTF-8").getLines()
-        } else {
-          parser.usage()
-        })
-
-      for (((leftLine, rightLine), index) <- (left_file zip right_file).zipWithIndex) {
-        println(Comparer(index, leftLine, rightLine))
-      }
-      burnRemainder(left_file, leftLog)
-      burnRemainder(right_file, rightLog)
-
-      log.summary("left parse (Warnings,Errors): %s\n".format(leftLog.getStats()))
-      log.summary("right parse (Warnings,Errors): %s\n".format(rightLog.getStats()))
-      log.summary("Total comparison (Warnings,Errors): %s\n".format(log.getStats()))
-    }
-    catch {
-      case e: ArgotUsageException =>
-        println(e.message)
-    }
-  }
-
-}

File src/test/scala/opennlp/scalabha/test/ComparerTest.scala

-package opennlp.scalabha.test
-
-import opennlp.scalabha.model._
-import opennlp.scalabha.tree.Comparer
-import opennlp.scalabha.tree.Comparer.{Fail, ComparisonResult, Success}
-import org.scalatest.FlatSpec
-import org.scalatest.matchers.ShouldMatchers
-
-class ComparerTest extends FlatSpec with ShouldMatchers {
-  val tests = List[(String, String, ComparisonResult)](
-    ("(a b)","(a b)", Success()),
-    ("(a b)","(b b)", Fail(0,"(a b)","(b b)")),
-    ("(a b)","(a c)", Success()),
-    ("(a b c)","(a b c)", Success()),
-    ("(a b c d)","(a b c d)", Success()),
-    ("(a (b c))","(a (b c))", Success()),
-    ("(a (b c d) (e f g))","(a (b c d) (e f g))", Success()),
-    ("(a b))","(a b))", Fail(0, "(a b))","(a b))"))
-  )
-
-  for ((left, right, result) <- tests) {
-    "\"%s\"".format(left) should "compare %s to \"%s\"".format(result, right) in {
-      assert(Comparer(0, left,right) === result)
-    }
-  }
-}