From c91fe035e4427f4ca071c55455e19b011a33a1d5 Mon Sep 17 00:00:00 2001 From: Rocky Madden Date: Sat, 20 Oct 2012 15:44:53 -0600 Subject: Created DiceSorensen metric, spec, and command. --- .../cli/command/diceSorensenMetric.scala | 58 ++++++++++++++++++++++ .../cli/command/diceSorensenMetricSpec.scala | 39 +++++++++++++++ 2 files changed, 97 insertions(+) create mode 100755 cli/source/core/scala/org/hashtree/stringmetric/cli/command/diceSorensenMetric.scala create mode 100755 cli/source/test/scala/org/hashtree/stringmetric/cli/command/diceSorensenMetricSpec.scala (limited to 'cli/source') diff --git a/cli/source/core/scala/org/hashtree/stringmetric/cli/command/diceSorensenMetric.scala b/cli/source/core/scala/org/hashtree/stringmetric/cli/command/diceSorensenMetric.scala new file mode 100755 index 0000000..1e21a2d --- /dev/null +++ b/cli/source/core/scala/org/hashtree/stringmetric/cli/command/diceSorensenMetric.scala @@ -0,0 +1,58 @@ +package org.hashtree.stringmetric.cli.command + +import org.hashtree.stringmetric.{ CaseStringCleaner, StringCleanerDelegate } +import org.hashtree.stringmetric.cli._ +import org.hashtree.stringmetric.cli.command._ +import org.hashtree.stringmetric.distance.DiceSorensenMetric + +/** + * The diceSorensenMetric [[org.hashtree.stringmetric.cli.command.Command]]. Compares the similarity of two strings + * using the Dice coefficient / Sorensen similarity index. + */ +object diceSorensenMetric extends Command { + override def main(args: Array[String]): Unit = { + val options = OptionMapUtility.toOptionMap(args) + + try { + // Help. + if (options.contains('h) || options.contains('help)) { + help() + exit(options) + // Execute. + } else if (options.contains('dashless) && options('dashless).count(_ == ' ') == 1) { + execute(options) + exit(options) + // Invalid syntax. + } else { + throw new IllegalArgumentException("Expected valid syntax. See --help.") + } + } catch { + case e => error(e)(options) + } + } + + override def help(): Unit = { + val ls = sys.props("line.separator") + val tab = " " + + println( + "Compares the similarity of two strings using the Dice coefficient / Sorensen similarity index." + ls + ls + + "Syntax:" + ls + + tab + "diceSorensenMetric [Options] string1 string2..." + ls + ls + + "Options:" + ls + + tab + "-h, --help" + ls + + tab + tab + "Outputs description, syntax, and options." + ) + } + + override def execute(options: OptionMap): Unit = { + val strings = options('dashless).split(" ") + + println( + DiceSorensenMetric.compare( + strings(0), + strings(1) + )(new StringCleanerDelegate with CaseStringCleaner).getOrElse("not comparable").toString + ) + } +} \ No newline at end of file diff --git a/cli/source/test/scala/org/hashtree/stringmetric/cli/command/diceSorensenMetricSpec.scala b/cli/source/test/scala/org/hashtree/stringmetric/cli/command/diceSorensenMetricSpec.scala new file mode 100755 index 0000000..33ea7dd --- /dev/null +++ b/cli/source/test/scala/org/hashtree/stringmetric/cli/command/diceSorensenMetricSpec.scala @@ -0,0 +1,39 @@ +package org.hashtree.stringmetric.cli.command + +import org.hashtree.stringmetric.ScalaTest +import org.junit.runner.RunWith +import org.scalatest.junit.JUnitRunner + +@RunWith(classOf[JUnitRunner]) +final class diceSorensenMetricSpec extends ScalaTest { + "diceSorensenMetric" should provide { + "main method" when passed { + "valid dashless arguments" should executes { + "print if they are a match" in { + val out = new java.io.ByteArrayOutputStream() + + Console.withOut(out)( + diceSorensenMetric.main(Array("--unitTest", "--debug", "aBc", "abc")) + ) + + out.toString should equal ("1.0\n") + out.reset() + + Console.withOut(out)( + diceSorensenMetric.main(Array("--unitTest", "--debug", "aBc", "xyz")) + ) + + out.toString should equal ("0.0\n") + out.reset() + } + } + "no dashless arguments" should throws { + "IllegalArgumentException" in { + evaluating { + diceSorensenMetric.main(Array("--unitTest", "--debug")) + } should produce [IllegalArgumentException] + } + } + } + } +} \ No newline at end of file -- cgit v1.2.3