All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.fsq.twofishes.indexer.importers.geonames.HierarchyParser.scala Maven / Gradle / Ivy

The newest version!
// Copyright 2012 Foursquare Labs Inc. All Rights Reserved.
package io.fsq.twofishes.indexer.importers.geonames

import io.fsq.twofishes.util.{GeonamesId, StoredFeatureId}
import java.io.File
import scala.collection.mutable.HashMap

object HierarchyParser {
  def parseHierarchy(filenames: List[String]): HashMap[StoredFeatureId, List[StoredFeatureId]] = {
    val map = new HashMap[StoredFeatureId, List[StoredFeatureId]]
    for {
      filename <- filenames
      if new File(filename).exists
    } {
      val lines = scala.io.Source.fromFile(filename).getLines
      lines.foreach(l => {
        val parts = l.split("\\t")
        val parentId = GeonamesId(parts(0).toLong)
        val childId = GeonamesId(parts(1).toLong)
        val hierarchyType = parts.lift(2).getOrElse("")

        if (hierarchyType == "ADM") {
          if (!map.contains(childId)) {
            map(childId) = Nil
          }
          map(childId) = parentId :: map(childId)
        }
      })
    }

    map
  }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy