1. Lars Yencken
  2. gpalign-scala

Commits

Lars Yencken  committed 2ee7c51

Bundles the kanji readings file into the jar file during building.

  • Participants
  • Parent commits 3da965d
  • Branches default

Comments (0)

Files changed (2)

File nbproject/build-impl.xml

View file
         <taskdef classname="org.netbeans.modules.java.j2seproject.copylibstask.CopyLibs" classpath="${libs.CopyLibs.classpath}" name="copylibs"/>
         <copylibs compress="${jar.compress}" jarfile="${dist.jar}" manifest="${manifest.file}" runtimeclasspath="${run.classpath.without.build.classes.dir}">
             <fileset dir="${build.classes.dir}"/>
+            <fileset file="resources/kanji.readings"/>
             <manifest>
                 <attribute name="Main-Class" value="${main.class}"/>
                 <attribute name="Class-Path" value="${jar.classpath}"/>

File src/gpaligner/AlignmentGenerator.scala

View file
 /**
  * An extension to the Japanese generator which prunes alignments in batches.
  */
-class KanjidicGenerator(filename: String = "resources/kanji.readings") extends JapaneseGenerator {
-  val readingMap: Map[String, Set[String]] = parseReadings(filename)
+class KanjidicGenerator(val filename: String = "kanji.readings") extends JapaneseGenerator {
+  val readingMap: Map[String, Set[String]] = parseReadings
 
   override def pruneBatch(asRaw: Stream[Alignment]): Stream[Alignment] = {
     val as = super.pruneBatch(asRaw)
     }
   }
 
-  private def parseReadings(fn: String): Map[String, Set[String]] = {
+  private def parseReadings: Map[String, Set[String]] = {
+    val istream = KanjidicGenerator.getClass.getResourceAsStream("/" + filename)
+    val source = {
+      if (istream == null) {
+        // Within the right directory
+        scala.io.Source.fromFile(new java.io.File("resources/" + filename))
+      } else {
+        // Within a jar, where the resource is bundled
+        scala.io.Source.fromInputStream(istream)
+      }
+    }
+
     var result = Map[String, Set[String]]()
-    for (line <- scala.io.Source.fromFile(new java.io.File(filename)).getLines("\n")) {
+    for (line <- source.getLines("\n")) {
       val parts = line.split(" ")
       require(parts.size > 0)
       result += (parts(0) -> Set(parts.slice(1, parts.size) : _*))
     }
+
+    if (istream != null) {
+      istream.close
+    }
+
     result
   }
 }