aboutsummaryrefslogtreecommitdiffstats
path: root/scalding/src/main/scala/sandcrawler/BibjsonScorable.scala
blob: cdd598f9e0031da8fd33c3e285ef7a6eb62aafc5 (plain)
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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package sandcrawler

import scala.math
import scala.util.parsing.json.JSON
import scala.util.parsing.json.JSONObject

import cascading.flow.FlowDef
import cascading.tuple.Fields
import com.twitter.scalding._
import com.twitter.scalding.typed.TDsl._
// XXX: import parallelai.spyglass.hbase.HBasePipeConversions

// XXX: class BibjsonScorable extends Scorable with HBasePipeConversions {

class BibjsonScorable extends Scorable {

  def getSource(args : Args) : Source = {
    TextLine(args("bibjson-input"))
  }

  def getFeaturesPipe(args : Args)(implicit mode : Mode, flowDef : FlowDef) : TypedPipe[MapFeatures] = {
    getSource(args).read
      .toTypedPipe[String](new Fields("line"))
      .map { BibjsonScorable.bibjsonToMapFeatures(_) }
  }
}

object BibjsonScorable {
  def bibjsonToMapFeatures(json : String) : MapFeatures = {
    Scorable.jsonToMap(json) match {
      case None => MapFeatures(Scorable.NoSlug, json)
      case Some(map) => {
        if (map contains "title") {
          val title = Scorable.getString(map, "title")
          val doi = Scorable.getString(map, "doi")
          val sha1 = Scorable.getString(map, "sha")
          // TODO: year, authors (if available)
          if (title == null || title.isEmpty) {
            new MapFeatures(Scorable.NoSlug, json)
          } else {
            val sf : ScorableFeatures = ScorableFeatures.create(title=title, doi=doi, sha1=sha1)
            new MapFeatures(sf.toSlug, sf.toString)
          }
        } else {
          new MapFeatures(Scorable.NoSlug, json)
        }
      }
    }
  }
}