diff options
author | Rocky Madden <git@rockymadden.com> | 2012-10-26 19:46:48 -0600 |
---|---|---|
committer | Rocky Madden <git@rockymadden.com> | 2012-10-26 19:46:48 -0600 |
commit | ce234db3048af3d7cb5c825c8e217a9169ab5c3b (patch) | |
tree | 61716b04494d4fce785f2c4407401db492bd7559 /cli | |
parent | 93d1ecd3df6df537f951f63de060a5db92e01235 (diff) | |
download | stringmetric-ce234db3048af3d7cb5c825c8e217a9169ab5c3b.tar.gz stringmetric-ce234db3048af3d7cb5c825c8e217a9169ab5c3b.tar.bz2 stringmetric-ce234db3048af3d7cb5c825c8e217a9169ab5c3b.zip |
Created NYSIIS algorithm, metric, command, and specs.
Diffstat (limited to 'cli')
-rwxr-xr-x | cli/source/core/scala/org/hashtree/stringmetric/cli/command/nysiisMetric.scala | 58 | ||||
-rwxr-xr-x | cli/source/test/scala/org/hashtree/stringmetric/cli/command/nysiisMetricSpec.scala | 46 |
2 files changed, 104 insertions, 0 deletions
diff --git a/cli/source/core/scala/org/hashtree/stringmetric/cli/command/nysiisMetric.scala b/cli/source/core/scala/org/hashtree/stringmetric/cli/command/nysiisMetric.scala new file mode 100755 index 0000000..d49b99e --- /dev/null +++ b/cli/source/core/scala/org/hashtree/stringmetric/cli/command/nysiisMetric.scala @@ -0,0 +1,58 @@ +package org.hashtree.stringmetric.cli.command + +import org.hashtree.stringmetric.StringFilterDelegate +import org.hashtree.stringmetric.cli._ +import org.hashtree.stringmetric.cli.command._ +import org.hashtree.stringmetric.phonetic.NysiisMetric + +/** + * The nysiisMetric [[org.hashtree.stringmetric.cli.command.Command]]. Compares two strings to determine if they are + * phonetically similarly, per the NYSIIS algorithm. + */ +object nysiisMetric 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 determine if they are phonetically similarly, per the NYSIIS algorithm." + ls + ls + + "Syntax:" + ls + + tab + "nysiisMetric [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( + NysiisMetric.compare( + strings(0), + strings(1) + )(new StringFilterDelegate).getOrElse("not comparable").toString + ) + } +}
\ No newline at end of file diff --git a/cli/source/test/scala/org/hashtree/stringmetric/cli/command/nysiisMetricSpec.scala b/cli/source/test/scala/org/hashtree/stringmetric/cli/command/nysiisMetricSpec.scala new file mode 100755 index 0000000..4c0beea --- /dev/null +++ b/cli/source/test/scala/org/hashtree/stringmetric/cli/command/nysiisMetricSpec.scala @@ -0,0 +1,46 @@ +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 nysiisMetricSpec extends ScalaTest { + "nysiisMetric" 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)( + nysiisMetric.main(Array("--unitTest", "--debug", "aBc", "abc")) + ) + + out.toString should equal ("true\n") + out.reset() + + Console.withOut(out)( + nysiisMetric.main(Array("--unitTest", "--debug", "aBc", "xyz")) + ) + + out.toString should equal ("false\n") + out.reset() + + Console.withOut(out)( + nysiisMetric.main(Array("--unitTest", "--debug", "1", "1")) + ) + + out.toString should equal ("not comparable\n") + out.reset() + } + } + "no dashless arguments" should throws { + "IllegalArgumentException" in { + evaluating { + nysiisMetric.main(Array("--unitTest", "--debug")) + } should produce [IllegalArgumentException] + } + } + } + } +}
\ No newline at end of file |