Lars Yencken avatar Lars Yencken committed 2ee7c51

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

Comments (0)

Files changed (2)

nbproject/build-impl.xml

         <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}"/>

src/gpaligner/AlignmentGenerator.scala

 /**
  * 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
   }
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.