diff options
author | Rocky Madden <git@rockymadden.com> | 2013-01-10 12:59:59 -0700 |
---|---|---|
committer | Rocky Madden <git@rockymadden.com> | 2013-01-10 12:59:59 -0700 |
commit | 2b1ee6b4f3d548414e0513149c727a640f4548fe (patch) | |
tree | 97963f86cb3563b801fd5c6de5f6d9b259e35334 /cli/source | |
parent | 5ba0d76e0266b6ad7ac9b1d3db4e04f04550ef50 (diff) | |
download | stringmetric-2b1ee6b4f3d548414e0513149c727a640f4548fe.tar.gz stringmetric-2b1ee6b4f3d548414e0513149c727a640f4548fe.tar.bz2 stringmetric-2b1ee6b4f3d548414e0513149c727a640f4548fe.zip |
Created Ratcliff/Obershelp metric, command, and specs.
Diffstat (limited to 'cli/source')
2 files changed, 93 insertions, 0 deletions
diff --git a/cli/source/core/scala/com/rockymadden/stringmetric/cli/similarity/ratcliffObershelpMetric.scala b/cli/source/core/scala/com/rockymadden/stringmetric/cli/similarity/ratcliffObershelpMetric.scala new file mode 100755 index 0000000..fa31794 --- /dev/null +++ b/cli/source/core/scala/com/rockymadden/stringmetric/cli/similarity/ratcliffObershelpMetric.scala @@ -0,0 +1,54 @@ +package com.rockymadden.stringmetric.cli.similarity + +import com.rockymadden.stringmetric.cli._ +import com.rockymadden.stringmetric.similarity.RatcliffObershelpMetric + +/** + * The ratcliffObershelpMetric [[com.rockymadden.stringmetric.cli.Command]]. Compares the similarity of two strings + * using the Ratcliff / Obershelp similarity index. + */ +object ratcliffObershelpMetric extends Command { + override def main(args: Array[String]): Unit = { + val options = OptionMap(args) + + try { + // Help. + if (options.contains('h) || options.contains('help)) { + help() + exit(options) + // Execute. + } else if (options.contains('dashless) && (options('dashless): OptionMapArray).length == 2) { + execute(options) + exit(options) + // Invalid syntax. + } else throw new IllegalArgumentException("Expected valid syntax. See --help.") + } catch { + case e: Throwable => error(e, options) + } + } + + override def help(): Unit = { + val ls = sys.props("line.separator") + val tab = " " + + println( + "Compares the similarity of two strings using the Ratcliff / Obershelp similarity index." + ls + ls + + "Syntax:" + ls + + tab + "ratcliffObershelpMetric [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: OptionMapArray = options('dashless) + + println( + RatcliffObershelpMetric.compare( + strings(0), + strings(1) + ).getOrElse("not comparable") + ) + } +} diff --git a/cli/source/test/scala/com/rockymadden/stringmetric/cli/similarity/ratcliffObershelpMetricSpec.scala b/cli/source/test/scala/com/rockymadden/stringmetric/cli/similarity/ratcliffObershelpMetricSpec.scala new file mode 100755 index 0000000..cf7a0bf --- /dev/null +++ b/cli/source/test/scala/com/rockymadden/stringmetric/cli/similarity/ratcliffObershelpMetricSpec.scala @@ -0,0 +1,39 @@ +package com.rockymadden.stringmetric.cli.similarity + +import com.rockymadden.stringmetric.ScalaTest +import org.junit.runner.RunWith +import org.scalatest.junit.JUnitRunner + +@RunWith(classOf[JUnitRunner]) +final class ratcliffObershelpMetricSpec extends ScalaTest { + "ratcliffObershelpMetric" 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)( + ratcliffObershelpMetric.main(Array("--unitTest", "--debug", "abc", "abc")) + ) + + out.toString should equal ("1.0\n") + out.reset() + + Console.withOut(out)( + ratcliffObershelpMetric.main(Array("--unitTest", "--debug", "abc", "xyz")) + ) + + out.toString should equal ("0.0\n") + out.reset() + } + } + "no dashless arguments" should throws { + "IllegalArgumentException" in { + evaluating { + ratcliffObershelpMetric.main(Array("--unitTest", "--debug")) + } should produce [IllegalArgumentException] + } + } + } + } +} |