[英]Parser combinator matching on Map
I'm writing romanization tool using Scala's pattern combinators. 我正在使用Scala的模式组合器编写罗马化工具。
In one of the parsers I want to be able to match on some set of string values and transform them into corresponding values. 在一个解析器中,我希望能够匹配一组字符串值并将它们转换为相应的值。
Namely, I use Map[String, String] to translate between these values, but I did not find a way to match on map's keys without using separate regular expression. 即,我使用Map [String,String]在这些值之间进行转换,但是我没有找到一种无需使用单独的正则表达式就可以匹配map的键的方法。
object Transliteration extends RegexParsers {
private[text] val diphthongsMap = Map(
"ай" -> "ay",
"ей" -> "ey",
"ий" -> "iy",
"ой" -> "oy",
"уй" -> "uy",
"ый" -> "yi",
"эй" -> "ey",
"юй" -> "yuy",
"яй" -> "yay"
)
def diphthong: Parser[String] =
"""ай|ей|ий|ой|уй|ый|эй|юй|яй""".r ^^ { diphthongsMap(_) }
def text: Parser[String] =
rep1(notSymbols, extendedWord) ^^ { _.mkString }
[... bunch of other parsers ...]
def translatePhrase(phrase: String): String =
parseAll(text, phrase).get
}
Can I ditch explicit regular expression entirely? 我可以完全放弃显式正则表达式吗? Maybe write custom Parser?
也许写自定义解析器?
EDIT: I wasn't entirely clear that I just want more efficient way of coding diphthong
method without hardcode, not rewriting the whole logic of the program. 编辑:我不是很清楚我只是想要一种更有效的方法来编码不带硬编码的
diphthong
方法,而不是重写程序的整个逻辑。
A custom parser is probably not needed. 可能不需要自定义解析器。 One solution is to fold on the map:
一种解决方案是在地图上折叠:
def romanize(cyrillicString: String) =
diphthongsMap.foldLeft(cyrillicString) {
case (s, (from, to)) => s.replace(from, to)
}
although this solution isn't very efficient, with an approximate runtime of O(N^2) although could be okay to use for shorter text, as the cost of setting up a parser or a regex can be amortized. 尽管此解决方案不是很有效,但运行时间大约为O(N ^ 2),但可以用于较短的文本,因为可以分摊设置解析器或正则表达式的成本。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.