summaryrefslogtreecommitdiff
path: root/cli/source/main/scala/com/rockymadden/stringmetric/cli/tokenize/ngramtokenizer.scala
blob: 52fea44ab0d57d7a6017937d7fcca43347f242f2 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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 None => println("not computable")
		}
}