From 72dfa5ebe75b707f192cbe65c23393d08dbd8f8a Mon Sep 17 00:00:00 2001 From: Rocky Madden Date: Sun, 18 Nov 2012 19:31:32 -0700 Subject: Created refined NYSIIS algorithm, metric, commands, and specs. --- .../cli/phonetic/refinedNysiisAlgorithm.scala | 50 ++++++++++++++++++++ .../cli/phonetic/refinedNysiisMetric.scala | 54 ++++++++++++++++++++++ .../cli/phonetic/refinedNysiisAlgorithmSpec.scala | 39 ++++++++++++++++ .../cli/phonetic/refinedNysiisMetricSpec.scala | 46 ++++++++++++++++++ 4 files changed, 189 insertions(+) create mode 100755 cli/source/core/scala/org/hashtree/stringmetric/cli/phonetic/refinedNysiisAlgorithm.scala create mode 100755 cli/source/core/scala/org/hashtree/stringmetric/cli/phonetic/refinedNysiisMetric.scala create mode 100755 cli/source/test/scala/org/hashtree/stringmetric/cli/phonetic/refinedNysiisAlgorithmSpec.scala create mode 100755 cli/source/test/scala/org/hashtree/stringmetric/cli/phonetic/refinedNysiisMetricSpec.scala (limited to 'cli/source') diff --git a/cli/source/core/scala/org/hashtree/stringmetric/cli/phonetic/refinedNysiisAlgorithm.scala b/cli/source/core/scala/org/hashtree/stringmetric/cli/phonetic/refinedNysiisAlgorithm.scala new file mode 100755 index 0000000..9953116 --- /dev/null +++ b/cli/source/core/scala/org/hashtree/stringmetric/cli/phonetic/refinedNysiisAlgorithm.scala @@ -0,0 +1,50 @@ +package org.hashtree.stringmetric.cli.phonetic + +import org.hashtree.stringmetric.cli._ +import org.hashtree.stringmetric.phonetic.RefinedNysiisAlgorithm + +/** + * The refinedNysiisAlgorithm [[org.hashtree.stringmetric.cli.Command]]. Returns the phonetic representation of the + * passed string, per the refined NYSIIS algorithm. + */ +object refinedNysiisAlgorithm 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(_ == ' ') == 0) { + 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( + "Returns the phonetic representation of the passed string, per the refined NYSIIS algorithm." + ls + ls + + "Syntax:" + ls + + tab + "refinedNysiisAlgorithm [Options] string..." + ls + ls + + "Options:" + ls + + tab + "-h, --help" + ls + + tab + tab + "Outputs description, syntax, and options." + ) + } + + override def execute(options: OptionMap): Unit = + println( + RefinedNysiisAlgorithm.compute( + options('dashless) + ).getOrElse("not computable").toString + ) +} \ No newline at end of file diff --git a/cli/source/core/scala/org/hashtree/stringmetric/cli/phonetic/refinedNysiisMetric.scala b/cli/source/core/scala/org/hashtree/stringmetric/cli/phonetic/refinedNysiisMetric.scala new file mode 100755 index 0000000..b09075a --- /dev/null +++ b/cli/source/core/scala/org/hashtree/stringmetric/cli/phonetic/refinedNysiisMetric.scala @@ -0,0 +1,54 @@ +package org.hashtree.stringmetric.cli.phonetic + +import org.hashtree.stringmetric.cli._ +import org.hashtree.stringmetric.phonetic.RefinedNysiisMetric + +/** + * The refinedNysiisMetric [[org.hashtree.stringmetric.cli.Command]]. Compares two strings to determine if they are + * phonetically similarly, per the refined NYSIIS algorithm. + */ +object refinedNysiisMetric 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 refined NYSIIS algorithm." + ls + ls + + "Syntax:" + ls + + tab + "refinedNysiisMetric [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( + RefinedNysiisMetric.compare( + strings(0), + strings(1) + ).getOrElse("not comparable").toString + ) + } +} \ No newline at end of file diff --git a/cli/source/test/scala/org/hashtree/stringmetric/cli/phonetic/refinedNysiisAlgorithmSpec.scala b/cli/source/test/scala/org/hashtree/stringmetric/cli/phonetic/refinedNysiisAlgorithmSpec.scala new file mode 100755 index 0000000..2567221 --- /dev/null +++ b/cli/source/test/scala/org/hashtree/stringmetric/cli/phonetic/refinedNysiisAlgorithmSpec.scala @@ -0,0 +1,39 @@ +package org.hashtree.stringmetric.cli.phonetic + +import org.hashtree.stringmetric.ScalaTest +import org.junit.runner.RunWith +import org.scalatest.junit.JUnitRunner + +@RunWith(classOf[JUnitRunner]) +final class refinedNysiisAlgorithmSpec extends ScalaTest { + "refinedNysiisAlgorithm" should provide { + "main method" when passed { + "valid dashless argument" should executes { + "print phonetic representation" in { + val out = new java.io.ByteArrayOutputStream() + + Console.withOut(out)( + refinedNysiisAlgorithm.main(Array("--unitTest", "--debug", "abc")) + ) + + out.toString should equal ("abc\n") + out.reset() + + Console.withOut(out)( + refinedNysiisAlgorithm.main(Array("--unitTest", "--debug", "1")) + ) + + out.toString should equal ("not computable\n") + out.reset() + } + } + "no dashless argument" should throws { + "IllegalArgumentException" in { + evaluating { + refinedNysiisAlgorithm.main(Array("--unitTest", "--debug")) + } should produce [IllegalArgumentException] + } + } + } + } +} \ No newline at end of file diff --git a/cli/source/test/scala/org/hashtree/stringmetric/cli/phonetic/refinedNysiisMetricSpec.scala b/cli/source/test/scala/org/hashtree/stringmetric/cli/phonetic/refinedNysiisMetricSpec.scala new file mode 100755 index 0000000..eddee0a --- /dev/null +++ b/cli/source/test/scala/org/hashtree/stringmetric/cli/phonetic/refinedNysiisMetricSpec.scala @@ -0,0 +1,46 @@ +package org.hashtree.stringmetric.cli.phonetic + +import org.hashtree.stringmetric.ScalaTest +import org.junit.runner.RunWith +import org.scalatest.junit.JUnitRunner + +@RunWith(classOf[JUnitRunner]) +final class refinedNysiisMetricSpec extends ScalaTest { + "refinedNysiisMetric" 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)( + refinedNysiisMetric.main(Array("--unitTest", "--debug", "abc", "abc")) + ) + + out.toString should equal ("true\n") + out.reset() + + Console.withOut(out)( + refinedNysiisMetric.main(Array("--unitTest", "--debug", "abc", "xyz")) + ) + + out.toString should equal ("false\n") + out.reset() + + Console.withOut(out)( + refinedNysiisMetric.main(Array("--unitTest", "--debug", "1", "1")) + ) + + out.toString should equal ("not comparable\n") + out.reset() + } + } + "no dashless arguments" should throws { + "IllegalArgumentException" in { + evaluating { + refinedNysiisMetric.main(Array("--unitTest", "--debug")) + } should produce [IllegalArgumentException] + } + } + } + } +} \ No newline at end of file -- cgit v1.2.3