diff options
Diffstat (limited to 'cli')
23 files changed, 348 insertions, 828 deletions
diff --git a/cli/source/main/scala/com/rockymadden/stringmetric/cli/Command.scala b/cli/source/main/scala/com/rockymadden/stringmetric/cli/Command.scala index fedc0d9..7451c35 100755 --- a/cli/source/main/scala/com/rockymadden/stringmetric/cli/Command.scala +++ b/cli/source/main/scala/com/rockymadden/stringmetric/cli/Command.scala @@ -1,24 +1,32 @@ package com.rockymadden.stringmetric.cli -/** Defines the traits and provides basic implementations of a command. Commands are always implemented as objects. */ -trait Command { - def help(): Unit - - final def error(error: Throwable, options: OptionMap): Unit = - if (!isUnitTest(options)) { +abstract class Command( + protected val help: (OptionMap => String), + protected val predicate: (OptionMap => Boolean), + protected val execute: (OptionMap => String) +) { + private def error(error: Throwable, opts: OptionMap): Unit = + if (!isUnitTest(opts)) { println(error.getMessage) sys.exit(1) } else throw error - def execute(options: OptionMap): Unit - - final def exit(options: OptionMap): Unit = if (!isUnitTest(options)) sys.exit(0) + private def exit(opts: OptionMap): Unit = if (!isUnitTest(opts)) sys.exit(0) - final protected[this] def isDebug(options: OptionMap): Boolean = - (options.contains('d) || (options.contains('debug) && options.get('debug) != "false")) + private def isUnitTest(opts: OptionMap) = + opts.contains('ut) || (opts.contains('unitTest) && opts.get('unitTest) != "false") - final protected[this] def isUnitTest(options: OptionMap): Boolean = - (options.contains('ut) || (options.contains('unitTest) && options.get('unitTest) != "false")) + final def main(args: Array[String]): Unit = { + val opts: OptionMap = args - def main(args: Array[String]): Unit + try + if (opts.contains('h) || opts.contains('help)) { + println(help(opts)) + exit(opts) + } else if (predicate(opts)) { + println(execute(opts)) + exit(opts) + } else throw new IllegalArgumentException("Expected valid syntax. See --help.") + catch { case e: Throwable => error(e, opts) } + } } diff --git a/cli/source/main/scala/com/rockymadden/stringmetric/cli/package.scala b/cli/source/main/scala/com/rockymadden/stringmetric/cli/package.scala index 0b049bb..c4d8873 100755 --- a/cli/source/main/scala/com/rockymadden/stringmetric/cli/package.scala +++ b/cli/source/main/scala/com/rockymadden/stringmetric/cli/package.scala @@ -22,6 +22,10 @@ package object cli { implicit def arrayOfStringToOptionMap(stringArray: Array[String]): OptionMap = OptionMap(stringArray) + final val Ls = sys.props("line.separator") + final val Tab = " " + + class OptionString(val get: String) object OptionString { diff --git a/cli/source/main/scala/com/rockymadden/stringmetric/cli/phonetic/metaphonealgorithm.scala b/cli/source/main/scala/com/rockymadden/stringmetric/cli/phonetic/metaphonealgorithm.scala index d0b3812..95f14b6 100755 --- a/cli/source/main/scala/com/rockymadden/stringmetric/cli/phonetic/metaphonealgorithm.scala +++ b/cli/source/main/scala/com/rockymadden/stringmetric/cli/phonetic/metaphonealgorithm.scala @@ -3,39 +3,14 @@ package com.rockymadden.stringmetric.cli.phonetic import com.rockymadden.stringmetric.cli._ import com.rockymadden.stringmetric.phonetic.MetaphoneAlgorithm -/** - * The metaphonealgorithm [[com.rockymadden.stringmetric.cli.Command]]. Returns the phonetic representation of the - * passed string, per the Metaphone algorithm. - */ -object metaphonealgorithm extends Command { - override def main(args: Array[String]): Unit = { - val opts: OptionMap = args - - try - if (opts.contains('h) || opts.contains('help)) { - help() - exit(opts) - } else if (opts.contains('dashless) && (opts('dashless): Array[String]).length == 1) { - execute(opts) - exit(opts) - } else throw new IllegalArgumentException("Expected valid syntax. See --help.") - catch { case e: Throwable => error(e, opts) } - } - - override def help(): Unit = { - val ls = sys.props("line.separator") - val tab = " " - - println( - "Returns the phonetic representation of the passed string, per the Metaphone algorithm." + ls + ls + - "Syntax:" + ls + - tab + "metaphonealgorithm [Options] string..." + ls + ls + - "Options:" + ls + - tab + "-h, --help" + ls + - tab + tab + "Outputs description, syntax, and opts." - ) - } - - override def execute(opts: OptionMap): Unit = - println(MetaphoneAlgorithm.compute(opts('dashless)).getOrElse("not computable")) -} +case object metaphonealgorithm extends Command( + (opts) => + "Returns the phonetic representation of the passed string, per the Metaphone algorithm." + Ls + Ls + + "Syntax:" + Ls + + Tab + "metaphonealgorithm [Options] string..." + Ls + Ls + + "Options:" + Ls + + Tab + "-h, --help" + Ls + + Tab + Tab + "Outputs description, syntax, and opts.", + (opts) => opts.contains('dashless) && (opts('dashless): Array[String]).length == 1, + (opts) => MetaphoneAlgorithm.compute(opts('dashless)).getOrElse("not computable") +) diff --git a/cli/source/main/scala/com/rockymadden/stringmetric/cli/phonetic/metaphonemetric.scala b/cli/source/main/scala/com/rockymadden/stringmetric/cli/phonetic/metaphonemetric.scala index 98ac85c..1249723 100755 --- a/cli/source/main/scala/com/rockymadden/stringmetric/cli/phonetic/metaphonemetric.scala +++ b/cli/source/main/scala/com/rockymadden/stringmetric/cli/phonetic/metaphonemetric.scala @@ -3,42 +3,19 @@ package com.rockymadden.stringmetric.cli.phonetic import com.rockymadden.stringmetric.cli._ import com.rockymadden.stringmetric.phonetic.MetaphoneMetric -/** - * The metaphonemetric [[com.rockymadden.stringmetric.cli.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 opts: OptionMap = args - - try - if (opts.contains('h) || opts.contains('help)) { - help() - exit(opts) - } else if (opts.contains('dashless) && (opts('dashless): Array[String]).length == 2) { - execute(opts) - exit(opts) - } else throw new IllegalArgumentException("Expected valid syntax. See --help.") - catch { case e: Throwable => error(e, opts) } - } - - 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 opts." - ) - } - - override def execute(opts: OptionMap): Unit = { +case object metaphonemetric extends Command( + (opts) => + "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 opts.", + (opts) => opts.contains('dashless) && (opts('dashless): Array[String]).length == 2, + (opts) => { val strings: Array[String] = opts('dashless) - - println(MetaphoneMetric.compare(strings(0), strings(1)).getOrElse("not comparable")) + MetaphoneMetric.compare(strings(0), strings(1)) + .map(_.toString) + .getOrElse("not comparable") } -} +) diff --git a/cli/source/main/scala/com/rockymadden/stringmetric/cli/phonetic/nysiisalgorithm.scala b/cli/source/main/scala/com/rockymadden/stringmetric/cli/phonetic/nysiisalgorithm.scala index 0770ba5..bef2bc5 100755 --- a/cli/source/main/scala/com/rockymadden/stringmetric/cli/phonetic/nysiisalgorithm.scala +++ b/cli/source/main/scala/com/rockymadden/stringmetric/cli/phonetic/nysiisalgorithm.scala @@ -3,39 +3,14 @@ package com.rockymadden.stringmetric.cli.phonetic import com.rockymadden.stringmetric.cli._ import com.rockymadden.stringmetric.phonetic.NysiisAlgorithm -/** - * The nysiisalgorithm [[com.rockymadden.stringmetric.cli.Command]]. Returns the phonetic representation of the passed - * string, per the NYSIIS algorithm. - */ -object nysiisalgorithm extends Command { - override def main(args: Array[String]): Unit = { - val options: OptionMap = args - - try - if (options.contains('h) || options.contains('help)) { - help() - exit(options) - } else if (options.contains('dashless) && (options('dashless): Array[String]).length == 1) { - execute(options) - exit(options) - } 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( - "Returns the phonetic representation of the passed string, per the NYSIIS algorithm." + ls + ls + - "Syntax:" + ls + - tab + "nysiisalgorithm [Options] string..." + ls + ls + - "Options:" + ls + - tab + "-h, --help" + ls + - tab + tab + "Outputs description, syntax, and options." - ) - } - - override def execute(options: OptionMap): Unit = - println(NysiisAlgorithm.compute(options('dashless)).getOrElse("not computable")) -} +case object nysiisalgorithm extends Command( + (opts) => + "Returns the phonetic representation of the passed string, per the NYSIIS algorithm." + Ls + Ls + + "Syntax:" + Ls + + Tab + "nysiisalgorithm [Options] string..." + Ls + Ls + + "Options:" + Ls + + Tab + "-h, --help" + Ls + + Tab + Tab + "Outputs description, syntax, and options.", + (opts) => opts.contains('dashless) && (opts('dashless): Array[String]).length == 1, + (opts) => NysiisAlgorithm.compute(opts('dashless)).getOrElse("not computable") +) diff --git a/cli/source/main/scala/com/rockymadden/stringmetric/cli/phonetic/nysiismetric.scala b/cli/source/main/scala/com/rockymadden/stringmetric/cli/phonetic/nysiismetric.scala index 95ece48..7857c21 100755 --- a/cli/source/main/scala/com/rockymadden/stringmetric/cli/phonetic/nysiismetric.scala +++ b/cli/source/main/scala/com/rockymadden/stringmetric/cli/phonetic/nysiismetric.scala @@ -3,42 +3,19 @@ package com.rockymadden.stringmetric.cli.phonetic import com.rockymadden.stringmetric.cli._ import com.rockymadden.stringmetric.phonetic.NysiisMetric -/** - * The nysiismetric [[com.rockymadden.stringmetric.cli.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 opts: OptionMap = args - - try - if (opts.contains('h) || opts.contains('help)) { - help() - exit(opts) - } else if (opts.contains('dashless) && (opts('dashless): Array[String]).length == 2) { - execute(opts) - exit(opts) - } else throw new IllegalArgumentException("Expected valid syntax. See --help.") - catch { case e: Throwable => error(e, opts) } - } - - 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 opts." - ) - } - - override def execute(opts: OptionMap): Unit = { +case object nysiismetric extends Command( + (opts) => + "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 opts.", + (opts) => opts.contains('dashless) && (opts('dashless): Array[String]).length == 2, + (opts) => { val strings: Array[String] = opts('dashless) - - println(NysiisMetric.compare(strings(0), strings(1)).getOrElse("not comparable")) + NysiisMetric.compare(strings(0), strings(1)) + .map(_.toString) + .getOrElse("not comparable") } -} +) diff --git a/cli/source/main/scala/com/rockymadden/stringmetric/cli/phonetic/refinednysiisalgorithm.scala b/cli/source/main/scala/com/rockymadden/stringmetric/cli/phonetic/refinednysiisalgorithm.scala index e155cbf..3fc9f25 100755 --- a/cli/source/main/scala/com/rockymadden/stringmetric/cli/phonetic/refinednysiisalgorithm.scala +++ b/cli/source/main/scala/com/rockymadden/stringmetric/cli/phonetic/refinednysiisalgorithm.scala @@ -3,39 +3,14 @@ package com.rockymadden.stringmetric.cli.phonetic import com.rockymadden.stringmetric.cli._ import com.rockymadden.stringmetric.phonetic.RefinedNysiisAlgorithm -/** - * The refinednysiisalgorithm [[com.rockymadden.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 opts: OptionMap = args - - try - if (opts.contains('h) || opts.contains('help)) { - help() - exit(opts) - } else if (opts.contains('dashless) && (opts('dashless): Array[String]).length == 1) { - execute(opts) - exit(opts) - } else throw new IllegalArgumentException("Expected valid syntax. See --help.") - catch { case e: Throwable => error(e, opts) } - } - - 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 opts." - ) - } - - override def execute(opts: OptionMap): Unit = - println(RefinedNysiisAlgorithm.compute(opts('dashless)).getOrElse("not computable")) -} +case object refinednysiisalgorithm extends Command( + (opts) => + "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 opts.", + (opts) => opts.contains('dashless) && (opts('dashless): Array[String]).length == 1, + (opts) => RefinedNysiisAlgorithm.compute(opts('dashless)).getOrElse("not computable") +) diff --git a/cli/source/main/scala/com/rockymadden/stringmetric/cli/phonetic/refinednysiismetric.scala b/cli/source/main/scala/com/rockymadden/stringmetric/cli/phonetic/refinednysiismetric.scala index 3c32291..2566fa8 100755 --- a/cli/source/main/scala/com/rockymadden/stringmetric/cli/phonetic/refinednysiismetric.scala +++ b/cli/source/main/scala/com/rockymadden/stringmetric/cli/phonetic/refinednysiismetric.scala @@ -3,42 +3,19 @@ package com.rockymadden.stringmetric.cli.phonetic import com.rockymadden.stringmetric.cli._ import com.rockymadden.stringmetric.phonetic.RefinedNysiisMetric -/** - * The refinednysiismetric [[com.rockymadden.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: OptionMap = args - - try - if (options.contains('h) || options.contains('help)) { - help() - exit(options) - } else if (options.contains('dashless) && (options('dashless): Array[String]).length == 2) { - execute(options) - exit(options) - } 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 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: Array[String] = options('dashless) - - println(RefinedNysiisMetric.compare(strings(0), strings(1)).getOrElse("not comparable")) +case object refinednysiismetric extends Command( + (opts) => + "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.", + (opts) => opts.contains('dashless) && (opts('dashless): Array[String]).length == 2, + (opts) => { + val strings: Array[String] = opts('dashless) + RefinedNysiisMetric.compare(strings(0), strings(1)) + .map(_.toString) + .getOrElse("not comparable") } -} +) diff --git a/cli/source/main/scala/com/rockymadden/stringmetric/cli/phonetic/refinedsoundexalgorithm.scala b/cli/source/main/scala/com/rockymadden/stringmetric/cli/phonetic/refinedsoundexalgorithm.scala index f5a89a8..12ac8e0 100755 --- a/cli/source/main/scala/com/rockymadden/stringmetric/cli/phonetic/refinedsoundexalgorithm.scala +++ b/cli/source/main/scala/com/rockymadden/stringmetric/cli/phonetic/refinedsoundexalgorithm.scala @@ -3,39 +3,14 @@ package com.rockymadden.stringmetric.cli.phonetic import com.rockymadden.stringmetric.cli._ import com.rockymadden.stringmetric.phonetic.RefinedSoundexAlgorithm -/** - * The refinedsoundexalgorithm [[com.rockymadden.stringmetric.cli.Command]]. Returns the phonetic representation of the - * passed string, per the refined Soundex algorithm. - */ -object refinedsoundexalgorithm extends Command { - override def main(args: Array[String]): Unit = { - val opts: OptionMap = args - - try - if (opts.contains('h) || opts.contains('help)) { - help() - exit(opts) - } else if (opts.contains('dashless) && (opts('dashless): Array[String]).length == 1) { - execute(opts) - exit(opts) - } else throw new IllegalArgumentException("Expected valid syntax. See --help.") - catch { case e: Throwable => error(e, opts) } - } - - override def help(): Unit = { - val ls = sys.props("line.separator") - val tab = " " - - println( - "Returns the phonetic representation of the passed string, per the refined Soundex algorithm." + ls + ls + - "Syntax:" + ls + - tab + "refinedsoundexalgorithm [Options] string..." + ls + ls + - "Options:" + ls + - tab + "-h, --help" + ls + - tab + tab + "Outputs description, syntax, and opts." - ) - } - - override def execute(opts: OptionMap): Unit = - println(RefinedSoundexAlgorithm.compute(opts('dashless)).getOrElse("not computable")) -} +case object refinedsoundexalgorithm extends Command( + (opts) => + "Returns the phonetic representation of the passed string, per the refined Soundex algorithm." + Ls + Ls + + "Syntax:" + Ls + + Tab + "refinedsoundexalgorithm [Options] string..." + Ls + Ls + + "Options:" + Ls + + Tab + "-h, --help" + Ls + + Tab + Tab + "Outputs description, syntax, and opts.", + (opts) => opts.contains('dashless) && (opts('dashless): Array[String]).length == 1, + (opts) => RefinedSoundexAlgorithm.compute(opts('dashless)).getOrElse("not computable") +) diff --git a/cli/source/main/scala/com/rockymadden/stringmetric/cli/phonetic/refinedsoundexmetric.scala b/cli/source/main/scala/com/rockymadden/stringmetric/cli/phonetic/refinedsoundexmetric.scala index e5ea4b4..370bd71 100755 --- a/cli/source/main/scala/com/rockymadden/stringmetric/cli/phonetic/refinedsoundexmetric.scala +++ b/cli/source/main/scala/com/rockymadden/stringmetric/cli/phonetic/refinedsoundexmetric.scala @@ -3,42 +3,19 @@ package com.rockymadden.stringmetric.cli.phonetic import com.rockymadden.stringmetric.cli._ import com.rockymadden.stringmetric.phonetic.RefinedSoundexMetric -/** - * The refinedsoundexmetric [[com.rockymadden.stringmetric.cli.Command]]. Compares two strings to determine if they are - * phonetically similarly, per the refined Soundex algorithm. - */ -object refinedsoundexmetric extends Command { - override def main(args: Array[String]): Unit = { - val options: OptionMap = args - - try - if (options.contains('h) || options.contains('help)) { - help() - exit(options) - } else if (options.contains('dashless) && (options('dashless): Array[String]).length == 2) { - execute(options) - exit(options) - } 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 two strings to determine if they are phonetically similarly, per the refined Soundex algorithm." + ls + ls + - "Syntax:" + ls + - tab + "refinedsoundexmetric [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: Array[String] = options('dashless) - - println(RefinedSoundexMetric.compare(strings(0), strings(1)).getOrElse("not comparable")) +case object refinedsoundexmetric extends Command( + (opts) => + "Compares two strings to determine if they are phonetically similarly, per the refined Soundex algorithm." + Ls + Ls + + "Syntax:" + Ls + + Tab + "refinedsoundexmetric [Options] string1 string2..." + Ls + Ls + + "Options:" + Ls + + Tab + "-h, --help" + Ls + + Tab + Tab + "Outputs description, syntax, and options.", + (opts) => opts.contains('dashless) && (opts('dashless): Array[String]).length == 2, + (opts) => { + val strings: Array[String] = opts('dashless) + RefinedSoundexMetric.compare(strings(0), strings(1)) + .map(_.toString) + .getOrElse("not comparable") } -} +) diff --git a/cli/source/main/scala/com/rockymadden/stringmetric/cli/phonetic/soundexalgorithm.scala b/cli/source/main/scala/com/rockymadden/stringmetric/cli/phonetic/soundexalgorithm.scala index 9883eb3..56cf068 100755 --- a/cli/source/main/scala/com/rockymadden/stringmetric/cli/phonetic/soundexalgorithm.scala +++ b/cli/source/main/scala/com/rockymadden/stringmetric/cli/phonetic/soundexalgorithm.scala @@ -3,39 +3,14 @@ package com.rockymadden.stringmetric.cli.phonetic import com.rockymadden.stringmetric.cli._ import com.rockymadden.stringmetric.phonetic.SoundexAlgorithm -/** - * The soundexalgorithm [[com.rockymadden.stringmetric.cli.Command]]. Returns the phonetic representation of the passed - * string, per the Soundex algorithm. - */ -object soundexalgorithm extends Command { - override def main(args: Array[String]): Unit = { - val opts: OptionMap = args - - try - if (opts.contains('h) || opts.contains('help)) { - help() - exit(opts) - } else if (opts.contains('dashless) && (opts('dashless): Array[String]).length == 1) { - execute(opts) - exit(opts) - } else throw new IllegalArgumentException("Expected valid syntax. See --help.") - catch { case e: Throwable => error(e, opts) } - } - - override def help(): Unit = { - val ls = sys.props("line.separator") - val tab = " " - - println( - "Returns the phonetic representation of the passed string, per the Soundex algorithm." + ls + ls + - "Syntax:" + ls + - tab + "soundexalgorithm [Options] string..." + ls + ls + - "Options:" + ls + - tab + "-h, --help" + ls + - tab + tab + "Outputs description, syntax, and opts." - ) - } - - override def execute(opts: OptionMap): Unit = - println(SoundexAlgorithm.compute(opts('dashless)).getOrElse("not computable")) -} +case object soundexalgorithm extends Command( + (opts) => + "Returns the phonetic representation of the passed string, per the Soundex algorithm." + Ls + Ls + + "Syntax:" + Ls + + Tab + "soundexalgorithm [Options] string..." + Ls + Ls + + "Options:" + Ls + + Tab + "-h, --help" + Ls + + Tab + Tab + "Outputs description, syntax, and opts.", + (opts) => opts.contains('dashless) && (opts('dashless): Array[String]).length == 1, + (opts) => SoundexAlgorithm.compute(opts('dashless)).getOrElse("not computable") +) diff --git a/cli/source/main/scala/com/rockymadden/stringmetric/cli/phonetic/soundexmetric.scala b/cli/source/main/scala/com/rockymadden/stringmetric/cli/phonetic/soundexmetric.scala index 01d070e..c7ec0cd 100755 --- a/cli/source/main/scala/com/rockymadden/stringmetric/cli/phonetic/soundexmetric.scala +++ b/cli/source/main/scala/com/rockymadden/stringmetric/cli/phonetic/soundexmetric.scala @@ -3,42 +3,19 @@ package com.rockymadden.stringmetric.cli.phonetic import com.rockymadden.stringmetric.cli._ import com.rockymadden.stringmetric.phonetic.SoundexMetric -/** - * The soundexmetric [[com.rockymadden.stringmetric.cli.Command]]. Compares two strings to determine if they are - * phonetically similarly, per the Soundex algorithm. - */ -object soundexmetric extends Command { - override def main(args: Array[String]): Unit = { - val options: OptionMap = args - - try - if (options.contains('h) || options.contains('help)) { - help() - exit(options) - } else if (options.contains('dashless) && (options('dashless): Array[String]).length == 2) { - execute(options) - exit(options) - } 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 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 + - tab + "-h, --help" + ls + - tab + tab + "Outputs description, syntax, and options." - ) - } - - override def execute(options: OptionMap): Unit = { - val strings: Array[String] = options('dashless) - - println(SoundexMetric.compare(strings(0), strings(1)).getOrElse("not comparable")) +case object soundexmetric extends Command( + (opts) => + "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 + + Tab + "-h, --help" + Ls + + Tab + Tab + "Outputs description, syntax, and options.", + (opts) => opts.contains('dashless) && (opts('dashless): Array[String]).length == 2, + (opts) => { + val strings: Array[String] = opts('dashless) + SoundexMetric.compare(strings(0), strings(1)) + .map(_.toString) + .getOrElse("not comparable") } -} +) diff --git a/cli/source/main/scala/com/rockymadden/stringmetric/cli/similarity/dicesorensenmetric.scala b/cli/source/main/scala/com/rockymadden/stringmetric/cli/similarity/dicesorensenmetric.scala index a166f4f..bb7ac0b 100755 --- a/cli/source/main/scala/com/rockymadden/stringmetric/cli/similarity/dicesorensenmetric.scala +++ b/cli/source/main/scala/com/rockymadden/stringmetric/cli/similarity/dicesorensenmetric.scala @@ -3,47 +3,24 @@ package com.rockymadden.stringmetric.cli.similarity import com.rockymadden.stringmetric.cli._ import com.rockymadden.stringmetric.similarity.DiceSorensenMetric -/** - * The dicesorensenmetric [[com.rockymadden.stringmetric.cli.Command]]. Compares the similarity of two strings using the - * Dice / Sorensen coefficient. - */ -object dicesorensenmetric extends Command { - override def main(args: Array[String]): Unit = { - val opts: OptionMap = args - - try - if (opts.contains('h) || opts.contains('help)) { - help() - exit(opts) - } else if (opts.contains('dashless) && (opts('dashless): Array[String]).length == 2 - && opts.contains('n) && (opts('n): Int) >= 1) { - - execute(opts) - exit(opts) - } else throw new IllegalArgumentException("Expected valid syntax. See --help.") - catch { case e: Throwable => error(e, opts) } - } - - override def help(): Unit = { - val ls = sys.props("line.separator") - val tab = " " - - println( - "Compares the similarity of two strings using the Dice / Sorensen coefficient." + ls + ls + - "Syntax:" + ls + - tab + "dicesorensenmetric [Options] string1 string2..." + ls + ls + - "Options:" + ls + - tab + "-h, --help" + ls + - tab + tab + "Outputs description, syntax, and opts." + - tab + "--n" + ls + - tab + tab + "The n-gram size, traditionally 2." - ) - } - - override def execute(opts: OptionMap): Unit = { +case object dicesorensenmetric extends Command( + (opts) => + "Compares the similarity of two strings using the Dice / Sorensen coefficient." + Ls + Ls + + "Syntax:" + Ls + + Tab + "dicesorensenmetric [Options] string1 string2..." + Ls + Ls + + "Options:" + Ls + + Tab + "-h, --help" + Ls + + Tab + Tab + "Outputs description, syntax, and opts." + + Tab + "--n" + Ls + + Tab + Tab + "The n-gram size, traditionally 2.", + (opts) => opts.contains('dashless) && (opts('dashless): Array[String]).length == 2 && + opts.contains('n) && (opts('n): Int) >= 1, + (opts) => { val strings: Array[String] = opts('dashless) val n: Int = opts('n) - println(DiceSorensenMetric(n).compare(strings(0), strings(1)).getOrElse("not comparable")) + DiceSorensenMetric(n).compare(strings(0), strings(1)) + .map(_.toString) + .getOrElse("not comparable") } -} +) diff --git a/cli/source/main/scala/com/rockymadden/stringmetric/cli/similarity/hammingmetric.scala b/cli/source/main/scala/com/rockymadden/stringmetric/cli/similarity/hammingmetric.scala index c03293f..e3db059 100755 --- a/cli/source/main/scala/com/rockymadden/stringmetric/cli/similarity/hammingmetric.scala +++ b/cli/source/main/scala/com/rockymadden/stringmetric/cli/similarity/hammingmetric.scala @@ -3,42 +3,20 @@ package com.rockymadden.stringmetric.cli.similarity import com.rockymadden.stringmetric.cli._ import com.rockymadden.stringmetric.similarity.HammingMetric -/** - * The hammingmetric [[com.rockymadden.stringmetric.cli.Command]]. Compares the number of characters that two equal - * length strings are different from one another. - */ -object hammingmetric extends Command { - override def main(args: Array[String]): Unit = { - val options: OptionMap = args +case object hammingmetric extends Command( + (opts) => + "Compares the number of characters that two equal length strings are different from one another." + Ls + Ls + + "Syntax:" + Ls + + Tab + "hammingmetric [Options] string1 string2..." + Ls + Ls + + "Options:" + Ls + + Tab + "-h, --help" + Ls + + Tab + Tab + "Outputs description, syntax, and options.", + (opts) => opts.contains('dashless) && (opts('dashless): Array[String]).length == 2, + (opts) => { + val strings: Array[String] = opts('dashless) - try - if (options.contains('h) || options.contains('help)) { - help() - exit(options) - } else if (options.contains('dashless) && (options('dashless): Array[String]).length == 2) { - execute(options) - exit(options) - } else throw new IllegalArgumentException("Expected valid syntax. See --help.") - catch { case e: Throwable => error(e, options) } + HammingMetric.compare(strings(0), strings(1)) + .map(_.toString) + .getOrElse("not comparable") } - - override def help(): Unit = { - val ls = sys.props("line.separator") - val tab = " " - - println( - "Compares the number of characters that two equal length strings are different from one another." + ls + ls + - "Syntax:" + ls + - tab + "hammingmetric [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: Array[String] = options('dashless) - - println(HammingMetric.compare(strings(0), strings(1)).getOrElse("not comparable")) - } -} +) diff --git a/cli/source/main/scala/com/rockymadden/stringmetric/cli/similarity/jaccardmetric.scala b/cli/source/main/scala/com/rockymadden/stringmetric/cli/similarity/jaccardmetric.scala index 5dee606..8301158 100755 --- a/cli/source/main/scala/com/rockymadden/stringmetric/cli/similarity/jaccardmetric.scala +++ b/cli/source/main/scala/com/rockymadden/stringmetric/cli/similarity/jaccardmetric.scala @@ -3,47 +3,24 @@ package com.rockymadden.stringmetric.cli.similarity import com.rockymadden.stringmetric.cli._ import com.rockymadden.stringmetric.similarity.JaccardMetric -/** - * The jaccardmetric [[com.rockymadden.stringmetric.cli.Command]]. Compares the similarity of two strings using the - * Jaccard coefficient. - */ -object jaccardmetric extends Command { - override def main(args: Array[String]): Unit = { - val opts: OptionMap = args - - try - if (opts.contains('h) || opts.contains('help)) { - help() - exit(opts) - } else if (opts.contains('dashless) && (opts('dashless): Array[String]).length == 2 - && opts.contains('n) && (opts('n): Int) >= 1) { - - execute(opts) - exit(opts) - } else throw new IllegalArgumentException("Expected valid syntax. See --help.") - catch { case e: Throwable => error(e, opts) } - } - - override def help(): Unit = { - val ls = sys.props("line.separator") - val tab = " " - - println( - "Compares the similarity of two strings using the Jaccard coefficient." + ls + ls + - "Syntax:" + ls + - tab + "jaccardmetric [Options] string1 string2..." + ls + ls + - "Options:" + ls + - tab + "-h, --help" + ls + - tab + tab + "Outputs description, syntax, and opts." + - tab + "--n" + ls + - tab + tab + "The n-gram size." - ) - } - - override def execute(opts: OptionMap): Unit = { +case object jaccardmetric extends Command( + (opts) => + "Compares the similarity of two strings using the Jaccard coefficient." + Ls + Ls + + "Syntax:" + Ls + + Tab + "jaccardmetric [Options] string1 string2..." + Ls + Ls + + "Options:" + Ls + + Tab + "-h, --help" + Ls + + Tab + Tab + "Outputs description, syntax, and opts." + + Tab + "--n" + Ls + + Tab + Tab + "The n-gram size.", + (opts) => opts.contains('dashless) && (opts('dashless): Array[String]).length == 2 + && opts.contains('n) && (opts('n): Int) >= 1, + (opts) => { val strings: Array[String] = opts('dashless) val n: Int = opts('n) - println(JaccardMetric(n).compare(strings(0), strings(1)).getOrElse("not comparable")) + JaccardMetric(n).compare(strings(0), strings(1)) + .map(_.toString) + .getOrElse("not comparable") } -} +) diff --git a/cli/source/main/scala/com/rockymadden/stringmetric/cli/similarity/jarometric.scala b/cli/source/main/scala/com/rockymadden/stringmetric/cli/similarity/jarometric.scala index fcb6aa2..cb7e188 100755 --- a/cli/source/main/scala/com/rockymadden/stringmetric/cli/similarity/jarometric.scala +++ b/cli/source/main/scala/com/rockymadden/stringmetric/cli/similarity/jarometric.scala @@ -3,39 +3,20 @@ package com.rockymadden.stringmetric.cli.similarity import com.rockymadden.stringmetric.cli._ import com.rockymadden.stringmetric.similarity.JaroMetric -/** The jarometric [[com.rockymadden.stringmetric.cli.Command]]. Compares two strings to calculate the Jaro distance. */ -object jarometric extends Command { - override def main(args: Array[String]): Unit = { - val options: OptionMap = args +case object jarometric extends Command( + (opts) => + "Compares two strings to calculate the Jaro distance." + Ls + Ls + + "Syntax:" + Ls + + Tab + "jarometric [Options] string1 string2..." + Ls + Ls + + "Options:" + Ls + + Tab + "-h, --help" + Ls + + Tab + Tab + "Outputs description, syntax, and options.", + (opts) => opts.contains('dashless) && (opts('dashless): Array[String]).length == 2, + (opts) => { + val strings: Array[String] = opts('dashless) - try - if (options.contains('h) || options.contains('help)) { - help() - exit(options) - } else if (options.contains('dashless) && (options('dashless): Array[String]).length == 2) { - execute(options) - exit(options) - } else throw new IllegalArgumentException("Expected valid syntax. See --help.") - catch { case e: Throwable => error(e, options) } + JaroMetric.compare(strings(0), strings(1)) + .map(_.toString) + .getOrElse("not comparable") } - - override def help(): Unit = { - val ls = sys.props("line.separator") - val tab = " " - - println( - "Compares two strings to calculate the Jaro distance." + ls + ls + - "Syntax:" + ls + - tab + "jarometric [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: Array[String] = options('dashless) - - println(JaroMetric.compare(strings(0), strings(1)).getOrElse("not comparable")) - } -} +) diff --git a/cli/source/main/scala/com/rockymadden/stringmetric/cli/similarity/jarowinklermetric.scala b/cli/source/main/scala/com/rockymadden/stringmetric/cli/similarity/jarowinklermetric.scala index b925c2d..5cfcb1e 100755 --- a/cli/source/main/scala/com/rockymadden/stringmetric/cli/similarity/jarowinklermetric.scala +++ b/cli/source/main/scala/com/rockymadden/stringmetric/cli/similarity/jarowinklermetric.scala @@ -3,42 +3,20 @@ package com.rockymadden.stringmetric.cli.similarity import com.rockymadden.stringmetric.cli._ import com.rockymadden.stringmetric.similarity.JaroWinklerMetric -/** - * The jarowinklermetric [[com.rockymadden.stringmetric.cli.Command]]. Compares two strings to calculate the - * Jaro-Winkler distance. - */ -object jarowinklermetric extends Command { - override def main(args: Array[String]): Unit = { - val opts: OptionMap = args - - try - if (opts.contains('h) || opts.contains('help)) { - help() - exit(opts) - } else if (opts.contains('dashless) && (opts('dashless): Array[String]).length == 2) { - execute(opts) - exit(opts) - } else throw new IllegalArgumentException("Expected valid syntax. See --help.") - catch { case e: Throwable => error(e, opts) } - } - - override def help(): Unit = { - val ls = sys.props("line.separator") - val tab = " " - - println( - "Compares two strings to calculate the Jaro-Winkler distance." + ls + ls + - "Syntax:" + ls + - tab + "jarowinklermetric [Options] string1 string2..." + ls + ls + - "Options:" + ls + - tab + "-h, --help" + ls + - tab + tab + "Outputs description, syntax, and opts." - ) - } - - override def execute(opts: OptionMap): Unit = { +case object jarowinklermetric extends Command( + (opts) => + "Compares two strings to calculate the Jaro-Winkler distance." + Ls + Ls + + "Syntax:" + Ls + + Tab + "jarowinklermetric [Options] string1 string2..." + Ls + Ls + + "Options:" + Ls + + Tab + "-h, --help" + Ls + + Tab + Tab + "Outputs description, syntax, and opts.", + (opts) => opts.contains('dashless) && (opts('dashless): Array[String]).length == 2, + (opts) => { val strings: Array[String] = opts('dashless) - println(JaroWinklerMetric.compare(strings(0), strings(1)).getOrElse("not comparable")) + JaroWinklerMetric.compare(strings(0), strings(1)) + .map(_.toString) + .getOrElse("not comparable") } -} +) diff --git a/cli/source/main/scala/com/rockymadden/stringmetric/cli/similarity/levenshteinmetric.scala b/cli/source/main/scala/com/rockymadden/stringmetric/cli/similarity/levenshteinmetric.scala index 6146750..9cfb021 100755 --- a/cli/source/main/scala/com/rockymadden/stringmetric/cli/similarity/levenshteinmetric.scala +++ b/cli/source/main/scala/com/rockymadden/stringmetric/cli/similarity/levenshteinmetric.scala @@ -3,43 +3,21 @@ package com.rockymadden.stringmetric.cli.similarity import com.rockymadden.stringmetric.cli._ import com.rockymadden.stringmetric.similarity.LevenshteinMetric -/** - * The levenshteinmetric [[com.rockymadden.stringmetric.cli.Command]]. Compares the number of characters that two - * strings are different from one another via insertion, deletion, and substitution. - */ -object levenshteinmetric extends Command { - override def main(args: Array[String]): Unit = { - val options: OptionMap = args +case object levenshteinmetric extends Command( + (opts) => + "Compares the number of characters that two strings are different from one another via insertion, deletion, " + + "and substitution." + Ls + Ls + + "Syntax:" + Ls + + Tab + "levenshteinmetric [Options] string1 string2..." + Ls + Ls + + "Options:" + Ls + + Tab + "-h, --help" + Ls + + Tab + Tab + "Outputs description, syntax, and options.", + (opts) => opts.contains('dashless) && (opts('dashless): Array[String]).length == 2, + (opts) => { + val strings: Array[String] = opts('dashless) - try - if (options.contains('h) || options.contains('help)) { - help() - exit(options) - } else if (options.contains('dashless) && (options('dashless): Array[String]).length == 2) { - execute(options) - exit(options) - } else throw new IllegalArgumentException("Expected valid syntax. See --help.") - catch { case e: Throwable => error(e, options) } + LevenshteinMetric.compare(strings(0), strings(1)) + .map(_.toString) + .getOrElse("not comparable") } - - override def help(): Unit = { - val ls = sys.props("line.separator") - val tab = " " - - println( - "Compares the number of characters that two strings are different from one another via insertion, deletion, " + - "and substitution." + ls + ls + - "Syntax:" + ls + - tab + "levenshteinmetric [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: Array[String] = options('dashless) - - println(LevenshteinMetric.compare(strings(0), strings(1)).getOrElse("not comparable")) - } -} +) diff --git a/cli/source/main/scala/com/rockymadden/stringmetric/cli/similarity/ngrammetric.scala b/cli/source/main/scala/com/rockymadden/stringmetric/cli/similarity/ngrammetric.scala index 1e54ec6..26914c8 100755 --- a/cli/source/main/scala/com/rockymadden/stringmetric/cli/similarity/ngrammetric.scala +++ b/cli/source/main/scala/com/rockymadden/stringmetric/cli/similarity/ngrammetric.scala @@ -3,47 +3,24 @@ package com.rockymadden.stringmetric.cli.similarity import com.rockymadden.stringmetric.cli._ import com.rockymadden.stringmetric.similarity.NGramMetric -/** - * The ngrammetric [[com.rockymadden.stringmetric.cli.Command]]. Compares the similarity of two strings using an N-Gram - * similarity index. - */ -object ngrammetric extends Command { - override def main(args: Array[String]): Unit = { - val options: OptionMap = args - - try - if (options.contains('h) || options.contains('help)) { - help() - exit(options) - } else if (options.contains('dashless) && (options('dashless): Array[String]).length == 2 - && options.contains('n) && (options('n): Int) >= 1) { - - execute(options) - exit(options) - } 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 an N-Gram similarity index." + ls + ls + - "Syntax:" + ls + - tab + "ngrammetric [Options] string1 string2..." + ls + ls + - "Options:" + ls + - tab + "-h, --help" + ls + - tab + tab + "Outputs description, syntax, and options." + - tab + "--n" + ls + - tab + tab + "The n." - ) - } - - override def execute(options: OptionMap): Unit = { - val strings: Array[String] = options('dashless) - val n: Int = options('n) - - println(NGramMetric(n).compare(strings(0), strings(1)).getOrElse("not comparable")) +case object ngrammetric extends Command( + (opts) => + "Compares the similarity of two strings using an N-Gram similarity index." + Ls + Ls + + "Syntax:" + Ls + + Tab + "ngrammetric [Options] string1 string2..." + Ls + Ls + + "Options:" + Ls + + Tab + "-h, --help" + Ls + + Tab + Tab + "Outputs description, syntax, and options." + + Tab + "--n" + Ls + + Tab + Tab + "The n.", + (opts) => opts.contains('dashless) && (opts('dashless): Array[String]).length == 2 && + opts.contains('n) && (opts('n): Int) >= 1, + (opts) => { + val strings: Array[String] = opts('dashless) + val n: Int = opts('n) + + NGramMetric(n).compare(strings(0), strings(1)) + .map(_.toString) + .getOrElse("not comparable") } -} +) diff --git a/cli/source/main/scala/com/rockymadden/stringmetric/cli/similarity/overlapmetric.scala b/cli/source/main/scala/com/rockymadden/stringmetric/cli/similarity/overlapmetric.scala index 81d3e04..a045b49 100755 --- a/cli/source/main/scala/com/rockymadden/stringmetric/cli/similarity/overlapmetric.scala +++ b/cli/source/main/scala/com/rockymadden/stringmetric/cli/similarity/overlapmetric.scala @@ -3,47 +3,24 @@ package com.rockymadden.stringmetric.cli.similarity import com.rockymadden.stringmetric.cli._ import com.rockymadden.stringmetric.similarity.OverlapMetric -/** - * The overlapmetric [[com.rockymadden.stringmetric.cli.Command]]. Compares the similarity of two strings using the - * overlap coefficient. - */ -object overlapmetric extends Command { - override def main(args: Array[String]): Unit = { - val opts: OptionMap = args - - try - if (opts.contains('h) || opts.contains('help)) { - help() - exit(opts) - } else if (opts.contains('dashless) && (opts('dashless): Array[String]).length == 2 - && opts.contains('n) && (opts('n): Int) >= 1) { - - execute(opts) - exit(opts) - } else throw new IllegalArgumentException("Expected valid syntax. See --help.") - catch { case e: Throwable => error(e, opts) } - } - - override def help(): Unit = { - val ls = sys.props("line.separator") - val tab = " " - - println( - "Compares the similarity of two strings using the overlap coefficient." + ls + ls + - "Syntax:" + ls + - tab + "overlapmetric [Options] string1 string2..." + ls + ls + - "Options:" + ls + - tab + "-h, --help" + ls + - tab + tab + "Outputs description, syntax, and opts." + - tab + "--n" + ls + - tab + tab + "The n-gram size." - ) - } - - override def execute(opts: OptionMap): Unit = { +case object overlapmetric extends Command( + (opts) => + "Compares the similarity of two strings using the overlap coefficient." + Ls + Ls + + "Syntax:" + Ls + + Tab + "overlapmetric [Options] string1 string2..." + Ls + Ls + + "Options:" + Ls + + Tab + "-h, --help" + Ls + + Tab + Tab + "Outputs description, syntax, and opts." + + Tab + "--n" + Ls + + Tab + Tab + "The n-gram size.", + (opts) => opts.contains('dashless) && (opts('dashless): Array[String]).length == 2 && + opts.contains('n) && (opts('n): Int) >= 1, + (opts) => { val strings: Array[String] = opts('dashless) val n: Int = opts('n) - println(OverlapMetric(n).compare(strings(0), strings(1)).getOrElse("not comparable")) + OverlapMetric(n).compare(strings(0), strings(1)) + .map(_.toString) + .getOrElse("not comparable") } -} +) diff --git a/cli/source/main/scala/com/rockymadden/stringmetric/cli/similarity/ratcliffobershelpmetric.scala b/cli/source/main/scala/com/rockymadden/stringmetric/cli/similarity/ratcliffobershelpmetric.scala index f69c222..08804b6 100755 --- a/cli/source/main/scala/com/rockymadden/stringmetric/cli/similarity/ratcliffobershelpmetric.scala +++ b/cli/source/main/scala/com/rockymadden/stringmetric/cli/similarity/ratcliffobershelpmetric.scala @@ -3,42 +3,20 @@ 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 opts: OptionMap = args - - try - if (opts.contains('h) || opts.contains('help)) { - help() - exit(opts) - } else if (opts.contains('dashless) && (opts('dashless): Array[String]).length == 2) { - execute(opts) - exit(opts) - } else throw new IllegalArgumentException("Expected valid syntax. See --help.") - catch { case e: Throwable => error(e, opts) } - } - - 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 opts." - ) - } - - override def execute(opts: OptionMap): Unit = { +case object ratcliffobershelpmetric extends Command( + (opts) => + "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 opts.", + (opts) => opts.contains('dashless) && (opts('dashless): Array[String]).length == 2, + (opts) => { val strings: Array[String] = opts('dashless) - println(RatcliffObershelpMetric.compare(strings(0), strings(1)).getOrElse("not comparable")) + RatcliffObershelpMetric.compare(strings(0), strings(1)) + .map(_.toString) + .getOrElse("not comparable") } -} +) diff --git a/cli/source/main/scala/com/rockymadden/stringmetric/cli/similarity/weightedlevenshteinmetric.scala b/cli/source/main/scala/com/rockymadden/stringmetric/cli/similarity/weightedlevenshteinmetric.scala index a9ad9b7..c74f80a 100755 --- a/cli/source/main/scala/com/rockymadden/stringmetric/cli/similarity/weightedlevenshteinmetric.scala +++ b/cli/source/main/scala/com/rockymadden/stringmetric/cli/similarity/weightedlevenshteinmetric.scala @@ -2,58 +2,32 @@ package com.rockymadden.stringmetric.cli.similarity import com.rockymadden.stringmetric.cli._ import com.rockymadden.stringmetric.similarity.WeightedLevenshteinMetric -import scala.math.BigDecimal -/** - * The weightedlevenshteinmetric [[com.rockymadden.stringmetric.cli.Command]]. Compares the number of characters that - * two strings are different from one another via insertion, deletion, and substitution. Allows the invoker to indicate - * the weight each operation takes. - */ -object weightedlevenshteinmetric extends Command { - override def main(args: Array[String]): Unit = { - val opts: OptionMap = args - - try - if (opts.contains('h) || opts.contains('help)) { - help() - exit(opts) - } else if (opts.contains('dashless) && (opts('dashless): Array[String]).length == 2 - && opts.contains('deleteWeight) && (opts('deleteWeight): Double) >= 0 - && opts.contains('insertWeight) && (opts('insertWeight): Double) >= 0 - && opts.contains('substituteWeight) && (opts('substituteWeight): Double) >= 0) { - - execute(opts) - exit(opts) - } else throw new IllegalArgumentException("Expected valid syntax. See --help.") - catch { case e: Throwable => error(e, opts) } - } - - override def help(): Unit = { - val ls = sys.props("line.separator") - val tab = " " - - println( - "Compares the number of characters that two strings are different from one another via insertion, deletion, " + - "and substitution. Allows the invoker to indicate the weight each operation takes." + ls + ls + - "Syntax:" + ls + - tab + "weightedlevenshteinmetric [Options] --deleteWeight=[double] --insertWeight=[double] --substituteWeight=[double] string1 string2..." + ls + ls + - "Options:" + ls + - tab + "--deleteWeight" + ls + - tab + tab + "The weight given to delete operations." + - tab + "-h, --help" + ls + - tab + tab + "Outputs description, syntax, and opts." + - tab + "--insertWeight" + ls + - tab + tab + "The weight given to insert operations." + - tab + "--substituteWeight" + ls + - tab + tab + "The weight given to substitute operations." - ) - } - - override def execute(opts: OptionMap): Unit = { +case object weightedlevenshteinmetric extends Command( + (opts) => + "Compares the number of characters that two strings are different from one another via insertion, deletion, " + + "and substitution. Allows the invoker to indicate the weight each operation takes." + Ls + Ls + + "Syntax:" + Ls + + Tab + "weightedlevenshteinmetric [Options] --deleteWeight=[double] --insertWeight=[double] --substituteWeight=[double] string1 string2..." + Ls + Ls + + "Options:" + Ls + + Tab + "--deleteWeight" + Ls + + Tab + Tab + "The weight given to delete operations." + + Tab + "-h, --help" + Ls + + Tab + Tab + "Outputs description, syntax, and opts." + + Tab + "--insertWeight" + Ls + + Tab + Tab + "The weight given to insert operations." + + Tab + "--substituteWeight" + Ls + + Tab + Tab + "The weight given to substitute operations.", + (opts) => opts.contains('dashless) && (opts('dashless): Array[String]).length == 2 && + opts.contains('deleteWeight) && (opts('deleteWeight): Double) >= 0 && + opts.contains('insertWeight) && (opts('insertWeight): Double) >= 0 && + opts.contains('substituteWeight) && (opts('substituteWeight): Double) >= 0, + (opts) => { val strings: Array[String] = opts('dashless) - println(WeightedLevenshteinMetric( - opts('deleteWeight), opts('insertWeight), opts('substituteWeight) - ).compare(strings(0), strings(1)).getOrElse("not comparable")) + WeightedLevenshteinMetric(opts('deleteWeight), opts('insertWeight), opts('substituteWeight)) + .compare(strings(0), strings(1)) + .map(_.toString) + .getOrElse("not comparable") } -} +) diff --git a/cli/source/main/scala/com/rockymadden/stringmetric/cli/tokenize/ngramtokenizer.scala b/cli/source/main/scala/com/rockymadden/stringmetric/cli/tokenize/ngramtokenizer.scala index 52fea44..cbd33d7 100755 --- a/cli/source/main/scala/com/rockymadden/stringmetric/cli/tokenize/ngramtokenizer.scala +++ b/cli/source/main/scala/com/rockymadden/stringmetric/cli/tokenize/ngramtokenizer.scala @@ -3,56 +3,29 @@ package com.rockymadden.stringmetric.cli.tokenize import com.rockymadden.stringmetric.cli._ import com.rockymadden.stringmetric.tokenize.NGramTokenizer -/** - * The ngramtokenizer [[com.rockymadden.stringmetric.cli.Command]]. Returns the N-Gram representation of the passed - * string. - */ -object ngramtokenizer extends Command { - override def main(args: Array[String]): Unit = { - val opts: OptionMap = args - - try - if (opts.contains('h) || opts.contains('help)) { - help() - exit(opts) - } else if (opts.contains('dashless) && (opts('dashless): Array[String]).length == 1 - && opts.contains('n) && (opts('n): Int) >= 1) { - - execute(opts) - exit(opts) - } else throw new IllegalArgumentException("Expected valid syntax. See --help.") - catch { case e: Throwable => error(e, opts) } - } - - override def help(): Unit = { - val ls = sys.props("line.separator") - val tab = " " - - println( - "Returns the N-Gram representation of the passed string." + ls + ls + - "Syntax:" + ls + - tab + "ngramtokenizer [Options] string..." + ls + ls + - "Options:" + ls + - tab + "-h, --help" + ls + - tab + tab + "Outputs description, syntax, and opts." + - tab + "--n" + ls + - tab + tab + "The n." - ) - } - - override def execute(opts: OptionMap): Unit = - NGramTokenizer(opts('n)).tokenize(opts('dashless)) match { - // Implicits are a pain here. - case Some(c) => { - val sb = new StringBuilder - - Range(0, c.length).foreach { i => - sb.append(c(i)) - if (i < c.length - 1) sb.append("|") - } - - println(sb.result()) +case object ngramtokenizer extends Command( + (opts) => + "Returns the N-Gram representation of the passed string." + Ls + Ls + + "Syntax:" + Ls + + Tab + "ngramtokenizer [Options] string..." + Ls + Ls + + "Options:" + Ls + + Tab + "-h, --help" + Ls + + Tab + Tab + "Outputs description, syntax, and opts." + + Tab + "--n" + Ls + + Tab + Tab + "The n.", + (opts) => opts.contains('dashless) && (opts('dashless): Array[String]).length == 1 && + opts.contains('n) && (opts('n): Int) >= 1, + (opts) => NGramTokenizer(opts('n)).tokenize(opts('dashless)) match { + case Some(c) => { + val sb = new StringBuilder + + Range(0, c.length).foreach { i => + sb.append(c(i)) + if (i < c.length - 1) sb.append("|") } - case None => println("not computable") + + sb.result() } -} + case None => "not computable" + } +) |