diff options
author | Rocky Madden <git@rockymadden.com> | 2012-10-20 15:44:53 -0600 |
---|---|---|
committer | Rocky Madden <git@rockymadden.com> | 2012-10-20 15:44:53 -0600 |
commit | c91fe035e4427f4ca071c55455e19b011a33a1d5 (patch) | |
tree | 9b220424f76b42dd9cdc61c50c28f97077673665 /cli | |
parent | 9fbe4868503142a7bd2e502b393d3bd60fdb441c (diff) | |
download | stringmetric-c91fe035e4427f4ca071c55455e19b011a33a1d5.tar.gz stringmetric-c91fe035e4427f4ca071c55455e19b011a33a1d5.tar.bz2 stringmetric-c91fe035e4427f4ca071c55455e19b011a33a1d5.zip |
Created DiceSorensen metric, spec, and command.
Diffstat (limited to 'cli')
-rwxr-xr-x | cli/source/core/scala/org/hashtree/stringmetric/cli/command/diceSorensenMetric.scala | 58 | ||||
-rwxr-xr-x | cli/source/test/scala/org/hashtree/stringmetric/cli/command/diceSorensenMetricSpec.scala | 39 |
2 files changed, 97 insertions, 0 deletions
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 |