Helper.scala 2.27 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/*
 * rico2edm
 * Copyright (C) 2021  UB Basel
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 *
 */



package ch.memobase.rico2edm.utils

import java.io.ByteArrayOutputStream
import java.util.zip.Deflater
26
27
import java.util.{Properties, HashMap => JHashMap}
import scala.language.reflectiveCalls
28
29
30

object Helper {

31
32
  private var isoLanguageCodes: Option[JHashMap[String,String]] = None

33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
  def compress(data: Array[Byte]): Array[Byte] = {
    val deflater = new Deflater()
    deflater.setInput(data)

    val outputStream = new ByteArrayOutputStream(data.length)

    deflater.finish()
    val buffer = new Array[Byte](1024)
    while ( {
      !deflater.finished
    }) {
      val count = deflater.deflate(buffer) // returns the generated code... index
      outputStream.write(buffer, 0, count)
    }
    outputStream.close()
    outputStream.toByteArray


  }

53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
  def initEnrichementMapping(props: Properties):Unit = {


    val isoCodes = new JHashMap[String,String]()
    using(io.Source.fromFile(props.get(Keys.LANGUAGE_ISO_CODE).toString)) { source =>
      for (line <- source.getLines) {
        val temp = line.split(",").map(_.trim)
        isoCodes.put(temp(0),temp(1))
      }
    }
    isoLanguageCodes = Some(isoCodes)
  }

  //noinspection ScalaStyle
  private def using[A <: { def close(): Unit }, B](resource: A)(f: A => B): B =
    try {
      f(resource)
    } finally {
      resource.close()
    }

  def getLanguageCode(wikiId:String):Option[String] = {
    isoLanguageCodes match {
      case Some(langkeys) if langkeys.containsKey(wikiId)  => Some(langkeys.get(wikiId))
      case Some(langkeys) if !langkeys.containsKey(wikiId)  => None
      case None => None
    }

  }

83
84
85


}