Skip to content

Instantly share code, notes, and snippets.

@jimregan
Created March 19, 2013 20:25
Show Gist options
  • Save jimregan/5199780 to your computer and use it in GitHub Desktop.
Save jimregan/5199780 to your computer and use it in GitHub Desktop.
Translation iterator
scalac transiter.scala
scala TranslationIterator input.txt > output.txt
^test/test<n><sg>/test<vblex><pri><p3><sg>$ ^file/file<n><sg>/file<vblex><pri><p3><sg>$
^test/test<n><sg>/test<vblex><pri><p3><sg>$ ^file/file<n><sg>/file<vblex><pri><p3><sg>$ ^test/test<n><sg>/test<vblex><pri><p3><sg>$
[1.0]^test<n><sg>$ ^file<n><sg>$
[1.1]^test<n><sg>$ ^file<vblex><pri><p3><sg>$
[1.2]^test<vblex><pri><p3><sg>$ ^file<n><sg>$
[1.3]^test<vblex><pri><p3><sg>$ ^file<vblex><pri><p3><sg>$
[2.0]^test<n><sg>$ ^file<n><sg>$ ^test<n><sg>$
[2.1]^test<n><sg>$ ^file<n><sg>$ ^test<vblex><pri><p3><sg>$
[2.2]^test<n><sg>$ ^file<vblex><pri><p3><sg>$ ^test<n><sg>$
[2.3]^test<n><sg>$ ^file<vblex><pri><p3><sg>$ ^test<vblex><pri><p3><sg>$
[2.4]^test<vblex><pri><p3><sg>$ ^file<n><sg>$ ^test<n><sg>$
[2.5]^test<vblex><pri><p3><sg>$ ^file<n><sg>$ ^test<vblex><pri><p3><sg>$
[2.6]^test<vblex><pri><p3><sg>$ ^file<vblex><pri><p3><sg>$ ^test<n><sg>$
[2.7]^test<vblex><pri><p3><sg>$ ^file<vblex><pri><p3><sg>$ ^test<vblex><pri><p3><sg>$
object TranslationIterator {
def wrap(s: String): String = "^" + s + "$"
def cart(l: List[List[String]]): List[List[String]] = l match {
case Nil => List(Nil)
case h :: t => for(e <- h; rest <- cart(t)) yield e :: rest
}
def splitter(s: String) = s.split("\\/").toList.filter{_.contains("<") }
def main(args: Array[String]) {
val lines = io.Source.fromFile(args(0)).getLines.toList
var counter = 1
for(line <- lines) {
val words = line.split("\\$|\\^").filter{ _ contains("/") }
val list = words.toList.map{splitter}
val joined = cart(list)
for ((e, n) <- joined.zipWithIndex) {
println("[" + counter + "." + n + "]" + e.map{wrap}.mkString(" "))
}
counter += 1
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment