From 9fbe4868503142a7bd2e502b393d3bd60fdb441c Mon Sep 17 00:00:00 2001 From: Rocky Madden Date: Sat, 20 Oct 2012 15:34:25 -0600 Subject: Created Metaphone algorithm, metric, specs, and command. --- .../stringmetric/cli/command/metaphoneMetric.scala | 58 ++++++++++++++++++++++ .../stringmetric/cli/command/soundexMetric.scala | 4 +- .../cli/command/metaphoneMetricSpec.scala | 46 +++++++++++++++++ 3 files changed, 106 insertions(+), 2 deletions(-) create mode 100755 cli/source/core/scala/org/hashtree/stringmetric/cli/command/metaphoneMetric.scala create mode 100755 cli/source/test/scala/org/hashtree/stringmetric/cli/command/metaphoneMetricSpec.scala (limited to 'cli') diff --git a/cli/source/core/scala/org/hashtree/stringmetric/cli/command/metaphoneMetric.scala b/cli/source/core/scala/org/hashtree/stringmetric/cli/command/metaphoneMetric.scala new file mode 100755 index 0000000..9687273 --- /dev/null +++ b/cli/source/core/scala/org/hashtree/stringmetric/cli/command/metaphoneMetric.scala @@ -0,0 +1,58 @@ +package org.hashtree.stringmetric.cli.command + +import org.hashtree.stringmetric.StringCleanerDelegate +import org.hashtree.stringmetric.cli._ +import org.hashtree.stringmetric.cli.command._ +import org.hashtree.stringmetric.phonetic.MetaphoneMetric + +/** + * The metaphoneMetric [[org.hashtree.stringmetric.cli.command.Command]]. Compares two strings to determine if they are + * phonetically similarly, per the Metaphone algorithm. + */ +object metaphoneMetric 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 Metaphone algorithm." + ls + ls + + "Syntax:" + ls + + tab + "metaphoneMetric [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( + MetaphoneMetric.compare( + strings(0), + strings(1) + )(new StringCleanerDelegate).getOrElse("not comparable").toString + ) + } +} \ No newline at end of file diff --git a/cli/source/core/scala/org/hashtree/stringmetric/cli/command/soundexMetric.scala b/cli/source/core/scala/org/hashtree/stringmetric/cli/command/soundexMetric.scala index adefd14..0d52972 100755 --- a/cli/source/core/scala/org/hashtree/stringmetric/cli/command/soundexMetric.scala +++ b/cli/source/core/scala/org/hashtree/stringmetric/cli/command/soundexMetric.scala @@ -7,7 +7,7 @@ import org.hashtree.stringmetric.phonetic.SoundexMetric /** * The soundexMetric [[org.hashtree.stringmetric.cli.command.Command]]. Compares two strings to determine if they are - * pronounced similarly, per the Soundex phonetic algorithm. + * phonetically similarly, per the Soundex algorithm. */ object soundexMetric extends Command { override def main(args: Array[String]): Unit = { @@ -36,7 +36,7 @@ object soundexMetric extends Command { val tab = " " println( - "Compares two strings to determine if they are pronounced similarly, per the Soundex phonetic algorithm." + ls + ls + + "Compares two strings to determine if they are phonetically similarly, per the Soundex algorithm." + ls + ls + "Syntax:" + ls + tab + "soundexMetric [Options] string1 string2..." + ls + ls + "Options:" + ls + diff --git a/cli/source/test/scala/org/hashtree/stringmetric/cli/command/metaphoneMetricSpec.scala b/cli/source/test/scala/org/hashtree/stringmetric/cli/command/metaphoneMetricSpec.scala new file mode 100755 index 0000000..dc04434 --- /dev/null +++ b/cli/source/test/scala/org/hashtree/stringmetric/cli/command/metaphoneMetricSpec.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 metaphoneMetricSpec extends ScalaTest { + "metaphoneMetric" 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)( + metaphoneMetric.main(Array("--unitTest", "--debug", "aBc", "abc")) + ) + + out.toString should equal ("true\n") + out.reset() + + Console.withOut(out)( + metaphoneMetric.main(Array("--unitTest", "--debug", "aBc", "xyz")) + ) + + out.toString should equal ("false\n") + out.reset() + + Console.withOut(out)( + metaphoneMetric.main(Array("--unitTest", "--debug", "1", "1")) + ) + + out.toString should equal ("not comparable\n") + out.reset() + } + } + "no dashless arguments" should throws { + "IllegalArgumentException" in { + evaluating { + metaphoneMetric.main(Array("--unitTest", "--debug")) + } should produce [IllegalArgumentException] + } + } + } + } +} \ No newline at end of file -- cgit v1.2.3