Skip to content

Instantly share code, notes, and snippets.

@ishideo
Last active March 7, 2019 06:03
Show Gist options
  • Save ishideo/fc524b1a49162a032ab7275ec45e90d7 to your computer and use it in GitHub Desktop.
Save ishideo/fc524b1a49162a032ab7275ec45e90d7 to your computer and use it in GitHub Desktop.
sbt "runMain KatakanaRomajiConvert.Main キョウト"
lazy val root = (project in file(".")).
settings(
inThisBuild(List(
scalaVersion := "2.12.7",
version := "0.1.0-SNAPSHOT"
)),
name := "KatakanaRomajiConvert",
libraryDependencies += "org.scalatest" %% "scalatest" % "3.0.5" % Test,
libraryDependencies += "com.ibm.icu" % "icu4j" % "63.1",
)
package KatakanaRomajiConvert
import com.ibm.icu.text.Transliterator
class KatakanaRomajiConvert(val word: String = "") {
private val transliterator = Transliterator.getInstance("Katakana-Latin")
def toRomaji: String = {
val kana: Option[String] = Some(word)
transliterator.transliterate(kana.getOrElse(""))
}
}
object Main extends App {
if (args.length != 0) {
val katakana: String = args(0)
val katakanaObj = new KatakanaRomajiConvert(katakana)
println(katakanaObj.toRomaji)
} else {
println("")
}
}
package KatakanaRomajiConvert
import org.scalatest._
class KatakanaRomajiConvertSpec extends FlatSpec with Matchers {
"Input strings \"トウキョウ\"" should "output strings toukyou" in {
new KatakanaRomajiConvert("トウキョウ").toRomaji shouldEqual "toukyou"
}
"Input no string" should "" in {
new KatakanaRomajiConvert("").toRomaji shouldEqual ""
}
"Argument is null" should "" in {
new KatakanaRomajiConvert(null).toRomaji shouldEqual ""
}
"No argument" should "" in {
new KatakanaRomajiConvert().toRomaji shouldEqual ""
}
"Input strings \"あいうアイウabcABC\"" should "" in {
new KatakanaRomajiConvert("あいうアイウabcABC").toRomaji shouldEqual "あいうaiuabcABC"
}
"Input strings \"あいう アイウ abc ABC\"" should "" in {
new KatakanaRomajiConvert("あいう アイウ abc ABC").toRomaji shouldEqual "あいう aiu abc ABC"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment