diff options
Diffstat (limited to 'cli')
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] + } + } + } + } +} |