summaryrefslogtreecommitdiff
path: root/cli/source
diff options
context:
space:
mode:
authorRocky Madden <git@rockymadden.com>2012-10-07 02:40:07 -0600
committerRocky Madden <git@rockymadden.com>2012-10-07 02:40:07 -0600
commit2a5145f7280fac663a07c0585be51b7b03d485b9 (patch)
tree79f88ea4cee9887db9145783759dc229f6ab9086 /cli/source
parent3fca0f2f4c3ca58a8e043ae56752fd90f550ac9e (diff)
downloadstringmetric-2a5145f7280fac663a07c0585be51b7b03d485b9.tar.gz
stringmetric-2a5145f7280fac663a07c0585be51b7b03d485b9.tar.bz2
stringmetric-2a5145f7280fac663a07c0585be51b7b03d485b9.zip
Added JaroMetric and spec. Also created CLI and spec for metric.
Diffstat (limited to 'cli/source')
-rwxr-xr-xcli/source/core/scala/org/hashtree/stringmetric/cli/command/jaroMetric.scala52
-rwxr-xr-xcli/source/test/scala/org/hashtree/stringmetric/cli/command/jaroMetricSpec.scala39
2 files changed, 91 insertions, 0 deletions
diff --git a/cli/source/core/scala/org/hashtree/stringmetric/cli/command/jaroMetric.scala b/cli/source/core/scala/org/hashtree/stringmetric/cli/command/jaroMetric.scala
new file mode 100755
index 0000000..ba061c7
--- /dev/null
+++ b/cli/source/core/scala/org/hashtree/stringmetric/cli/command/jaroMetric.scala
@@ -0,0 +1,52 @@
+package org.hashtree.stringmetric.cli.command
+
+import org.hashtree.stringmetric.JaroMetric
+import org.hashtree.stringmetric.cli._
+import org.hashtree.stringmetric.cli.command._
+
+/**
+ * The jaroMetric [[org.hashtree.stringmetric.cli.command.Command]]. Compares two strings to calculate the
+ * Jaro distance.
+ */
+object jaroMetric 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 two strings to calculate the Jaro distance." + ls + ls +
+ "Syntax:" + ls +
+ tab + "jaroMetric [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(JaroMetric.compare(strings(0), strings(1)).toString)
+ }
+} \ No newline at end of file
diff --git a/cli/source/test/scala/org/hashtree/stringmetric/cli/command/jaroMetricSpec.scala b/cli/source/test/scala/org/hashtree/stringmetric/cli/command/jaroMetricSpec.scala
new file mode 100755
index 0000000..d3eabe0
--- /dev/null
+++ b/cli/source/test/scala/org/hashtree/stringmetric/cli/command/jaroMetricSpec.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 jaroMetricSpec extends ScalaTest {
+ "jaroMetric" should provide {
+ "main method" when passed {
+ "valid dashless arguments" should executes {
+ "print the distance" in {
+ val out = new java.io.ByteArrayOutputStream()
+
+ Console.withOut(out)(
+ jaroMetric.main(Array("--unitTest", "--debug", "abc", "abc"))
+ )
+
+ out.toString should equal ("1.0\n")
+ out.reset()
+
+ Console.withOut(out)(
+ jaroMetric.main(Array("--unitTest", "--debug", "abc", "xyz"))
+ )
+
+ out.toString should equal ("0.0\n")
+ out.reset()
+ }
+ }
+ "no dashless arguments" should throws {
+ "IllegalArgumentException" in {
+ evaluating {
+ jaroMetric.main(Array("--unitTest", "--debug"))
+ } should produce [IllegalArgumentException]
+ }
+ }
+ }
+ }
+} \ No newline at end of file