Skip to content

Commit

Permalink
Decoupling of the logging tools through the creation of the Spotlight…
Browse files Browse the repository at this point in the history
…Log class. Issue #127
  • Loading branch information
golicar committed Sep 11, 2013
1 parent 26d20a9 commit 9b20cbc
Show file tree
Hide file tree
Showing 100 changed files with 607 additions and 734 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package org.dbpedia.spotlight.annotate

import org.apache.commons.logging.LogFactory
import org.dbpedia.spotlight.log.SpotlightLog
import org.dbpedia.spotlight.model._
import org.dbpedia.spotlight.spot.Spotter
import org.dbpedia.spotlight.exceptions.InputException
Expand All @@ -32,39 +32,35 @@ import java.util
*/
class DefaultAnnotator(val spotter : Spotter, val disambiguator: Disambiguator) extends Annotator {

private val LOG = LogFactory.getLog(this.getClass)

@throws(classOf[InputException])
def annotate(text : String) : java.util.List[DBpediaResourceOccurrence] = {

LOG.info("Spotting... ("+spotter.getName()+")")
SpotlightLog.info(this.getClass, "Spotting... (%s)", spotter.getName)
val spottedSurfaceForms : java.util.List[SurfaceFormOccurrence] = spotter.extract(new Text(text))

LOG.info("Disambiguating... ("+disambiguator.name+")")
SpotlightLog.info(this.getClass, "Disambiguating... (%s)", disambiguator.name)
val disambiguatedOccurrences : java.util.List[DBpediaResourceOccurrence] = {
if(spottedSurfaceForms.length>0)
disambiguator.disambiguate(spottedSurfaceForms)
else
new util.ArrayList[DBpediaResourceOccurrence]()
}

LOG.info("Done.")
SpotlightLog.info(this.getClass, "Done.")
disambiguatedOccurrences
}

}

class DefaultParagraphAnnotator(val spotter : Spotter, val disambiguator: ParagraphDisambiguatorJ) extends ParagraphAnnotator {

private val LOG = LogFactory.getLog(this.getClass)

@throws(classOf[InputException])
def annotate(text : String) : java.util.List[DBpediaResourceOccurrence] = {

LOG.info("Spotting... ("+spotter.getName()+")")
SpotlightLog.info(this.getClass, "Spotting... (%s)", spotter.getName)
val spottedSurfaceForms : List[SurfaceFormOccurrence] = asBuffer(spotter.extract(new Text(text))).toList

LOG.info("Disambiguating... ("+disambiguator.name+")")
SpotlightLog.info(this.getClass, "Disambiguating... (%s)", disambiguator.name)
val disambiguatedOccurrences : java.util.List[DBpediaResourceOccurrence] = {
if(spottedSurfaceForms.length>0)
disambiguator.disambiguate(Factory.Paragraph.from(spottedSurfaceForms))
Expand All @@ -73,7 +69,7 @@ class DefaultParagraphAnnotator(val spotter : Spotter, val disambiguator: Paragr
}


LOG.info("Done.")
SpotlightLog.info(this.getClass, "Done.")
disambiguatedOccurrences
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.dbpedia.spotlight.db

import model.{ResourceStore, SurfaceFormStore, CandidateMapStore}
import org.apache.commons.logging.LogFactory
import org.dbpedia.spotlight.model._
import org.dbpedia.spotlight.exceptions.SurfaceFormNotFoundException

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package org.dbpedia.spotlight.db
import model._
import org.dbpedia.spotlight.model._
import org.dbpedia.spotlight.disambiguate.mixtures.Mixture
import org.apache.commons.logging.LogFactory
import org.dbpedia.spotlight.log.SpotlightLog
import scala.collection.JavaConverters._
import similarity.{ContextSimilarity, TFICFSimilarity}
import org.dbpedia.spotlight.disambiguate.{ParagraphDisambiguator, Disambiguator}
Expand Down Expand Up @@ -38,8 +38,6 @@ class DBTwoStepDisambiguator(
contextSimilarity: ContextSimilarity
) extends ParagraphDisambiguator {

private val LOG = LogFactory.getLog(this.getClass)

/* Tokenizer that may be used for tokenization if the text is not already tokenized. */
var tokenizer: TextTokenizer = null

Expand Down Expand Up @@ -77,14 +75,14 @@ class DBTwoStepDisambiguator(

def bestK(paragraph: Paragraph, k: Int): Map[SurfaceFormOccurrence, List[DBpediaResourceOccurrence]] = {

LOG.debug("Running bestK for paragraph %s.".format(paragraph.id))
SpotlightLog.debug(this.getClass, "Running bestK for paragraph %s.",paragraph.id)

if (paragraph.occurrences.size == 0)
return Map[SurfaceFormOccurrence, List[DBpediaResourceOccurrence]]()

//Tokenize the text if it wasn't tokenized before:
if (tokenizer != null) {
LOG.info("Tokenizing input text...")
SpotlightLog.info(this.getClass, "Tokenizing input text...")
val tokens = tokenizer.tokenize(paragraph.text)
paragraph.text.setFeature(new Feature("tokens", tokens))
}
Expand Down Expand Up @@ -133,7 +131,7 @@ class DBTwoStepDisambiguator(
Map[SurfaceFormOccurrence, List[Candidate]]())(
(acc, sfOcc) => {

LOG.debug("Searching...")
SpotlightLog.debug(this.getClass, "Searching...")

val candidateRes = {
val sf = try {
Expand All @@ -143,10 +141,10 @@ class DBTwoStepDisambiguator(
}

val cands = candidateSearcher.getCandidates(sf)
LOG.debug("# candidates for: %s = %s.".format(sf, cands.size))
SpotlightLog.debug(this.getClass, "# candidates for: %s = %s.", sf, cands.size)

if (cands.size > MAX_CANDIDATES) {
LOG.debug("Reducing number of candidates to %d.".format(MAX_CANDIDATES))
SpotlightLog.debug(this.getClass, "Reducing number of candidates to %d.", MAX_CANDIDATES)
cands.toList.sortBy( _.prior ).reverse.take(MAX_CANDIDATES).toSet
} else {
cands
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.dbpedia.spotlight.db.disk

import java.io.File
import org.apache.commons.logging.LogFactory
import org.dbpedia.spotlight.log.SpotlightLog
import org.dbpedia.spotlight.db.model.TokenTypeStore

/**
Expand All @@ -12,10 +12,8 @@ import org.dbpedia.spotlight.db.model.TokenTypeStore

object DiskStore {

protected val LOG = LogFactory.getLog(this.getClass)

def loadContextStore(file: File, tokenTypeStore: TokenTypeStore): DiskContextStore = {
LOG.info("Opening disk-based context store...")
SpotlightLog.info(this.getClass, "Opening disk-based context store...")
val ds = new DiskContextStore(file.getAbsolutePath)
ds.tokenTypeStore = tokenTypeStore

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import org.dbpedia.spotlight.model.{OntologyType, DBpediaResource}
import org.dbpedia.spotlight.io.NTripleSource
import org.dbpedia.spotlight.model.Factory.OntologyType
import org.dbpedia.spotlight.model.OntologyType
import org.apache.commons.logging.LogFactory
import org.dbpedia.spotlight.log.SpotlightLog
import org.dbpedia.spotlight.db.disk.JDBMStore

/**
Expand All @@ -15,8 +15,6 @@ import org.dbpedia.spotlight.db.disk.JDBMStore
*/
class NTFileTypeMapper(dbFile:File) extends OntologyTypeMapper {

private val LOG = LogFactory.getLog(this.getClass)

private val typeMap: JDBMStore[String,String] = new JDBMStore[String,String](dbFile.getAbsolutePath)

private val TYPE_PREDICATE99 = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type"
Expand All @@ -28,17 +26,17 @@ class NTFileTypeMapper(dbFile:File) extends OntologyTypeMapper {
dirOrFile.listFiles(new FilenameFilter {
def accept(p1: File, p2: String) = p2.endsWith(".nt") || p2.endsWith(".nq")
}).foreach(file => {
LOG.info("Loading file: " + file.getName)
SpotlightLog.info(this.getClass, "Loading file: %s", file.getName)
loadFromFile(file)
})
}
else {
LOG.info("Loading file: " + dirOrFile.getName)
SpotlightLog.info(this.getClass, "Loading file: %s", dirOrFile.getName)
loadFromFile(dirOrFile)
}

typeMap.commit()
LOG.info("Files completely stored into the db file.")
SpotlightLog.info(this.getClass, "Files completely stored into the db file.")
}

private def loadFromFile(file: File) = {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.dbpedia.spotlight.db.memory

import org.dbpedia.spotlight.log.SpotlightLog
import org.dbpedia.spotlight.model.DBpediaResource
import java.lang.{Short, String}
import scala.collection.JavaConversions._
Expand Down Expand Up @@ -32,16 +33,16 @@ class MemoryResourceStore

override def loaded() {
createReverseLookup()
LOG.info("Counting total support...")
SpotlightLog.info(this.getClass, "Counting total support...")
totalSupport = supportForID.sum.toDouble
LOG.info("Done.")
SpotlightLog.info(this.getClass, "Done.")
}

def size = uriForID.size

def createReverseLookup() {
if (uriForID != null) {
LOG.info("Creating reverse-lookup for DBpedia resources.")
SpotlightLog.info(this.getClass, "Creating reverse-lookup for DBpedia resources.")
idFromURI = StringToIDMapFactory.createDefault(uriForID.size)

var i = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import scala.Predef._
import com.esotericsoftware.kryo.serializers.{DefaultArraySerializers, JavaSerializer}
import java.lang.{System, Short, String}
import collection.mutable.HashMap
import org.apache.commons.logging.LogFactory
import org.dbpedia.spotlight.log.SpotlightLog
import com.esotericsoftware.kryo.serializers.DefaultSerializers.KryoSerializableSerializer
import com.esotericsoftware.kryo.Kryo
import org.dbpedia.spotlight.db.model.{TokenTypeStore, ResourceStore}
Expand All @@ -24,9 +24,6 @@ import org.dbpedia.spotlight.db.FSADictionary
@SerialVersionUID(1001001)
abstract class MemoryStore extends Serializable {

@transient
protected val LOG = LogFactory.getLog(this.getClass)

/**
* Method called after the store has been deserialized.
* Implementations may override this method in order to finish setting
Expand All @@ -43,8 +40,6 @@ abstract class MemoryStore extends Serializable {
*/
object MemoryStore {

private val LOG = LogFactory.getLog(this.getClass)

val kryos = HashMap[String, Kryo]()

kryos.put(classOf[MemoryResourceStore].getSimpleName,
Expand Down Expand Up @@ -132,15 +127,15 @@ object MemoryStore {

val kryo: Kryo = kryos.get(simpleName).get

LOG.info("Loading %s...".format(simpleName))
SpotlightLog.info(this.getClass, "Loading %s...", simpleName)
val sStart = System.currentTimeMillis()
val input = new Input(in)

val s = kryo.readClassAndObject(input).asInstanceOf[T]
s.asInstanceOf[MemoryStore].loaded()

input.close()
LOG.info("Done (%d ms)".format(System.currentTimeMillis() - sStart))
SpotlightLog.info(this.getClass, "Done (%d ms)", System.currentTimeMillis() - sStart)
s
}

Expand Down Expand Up @@ -175,12 +170,12 @@ object MemoryStore {
def dump(store: MemoryStore, out: File) {
val kryo = kryos.get(store.getClass.getSimpleName).get

LOG.info("Writing %s...".format(store.getClass.getSimpleName))
SpotlightLog.info(this.getClass, "Writing %s...", store.getClass.getSimpleName)
val output = new Output(new FileOutputStream(out))
kryo.writeClassAndObject(output, store)

output.close()
LOG.info("Done.")
SpotlightLog.info(this.getClass, "Done.")
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.dbpedia.spotlight.db.memory

import org.dbpedia.spotlight.log.SpotlightLog
import org.dbpedia.spotlight.model.SurfaceForm
import org.dbpedia.spotlight.db.model.SurfaceFormStore
import org.dbpedia.spotlight.exceptions.SurfaceFormNotFoundException
Expand Down Expand Up @@ -59,13 +60,13 @@ class MemorySurfaceFormStore

def createReverseLookup() {

LOG.info("Summing total SF counts.")
SpotlightLog.info(this.getClass, "Summing total SF counts.")
totalAnnotatedCount = annotatedCountForID.sum
totalOccurrenceCount = totalCountForID.sum


if (stringForID != null) {
LOG.info("Creating reverse-lookup for surface forms, adding normalized surface forms.")
SpotlightLog.info(this.getClass, "Creating reverse-lookup for surface forms, adding normalized surface forms.")
idForString = StringToIDMapFactory.createDefault(stringForID.size * 2)

var i = 0
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.dbpedia.spotlight.db.memory

import org.dbpedia.spotlight.log.SpotlightLog
import org.dbpedia.spotlight.db.model.TokenTypeStore
import java.lang.String
import org.dbpedia.spotlight.model.TokenType
Expand Down Expand Up @@ -41,7 +42,7 @@ class MemoryTokenTypeStore

def createReverseLookup() {
if (tokenForId != null) {
LOG.info("Creating reverse-lookup for Tokens.")
SpotlightLog.info(this.getClass, "Creating reverse-lookup for Tokens.")
idFromToken = StringToIDMapFactory.createDefault(tokenForId.size)

var id = 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import collection.mutable
import org.dbpedia.spotlight.db.model.TokenTypeStore
import scala.collection.JavaConversions._
import org.dbpedia.spotlight.util.MathUtil
import org.apache.commons.logging.LogFactory

/**
* Generative context similarity based on Han et. al
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package org.dbpedia.spotlight.db.similarity

import scala.collection.JavaConversions._

import org.apache.commons.logging.LogFactory
import scala.Int
import collection.immutable
import collection.mutable
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ package org.dbpedia.spotlight.disambiguate
*/

import mixtures.LinearRegressionMixture
import org.apache.commons.logging.LogFactory
import org.dbpedia.spotlight.log.SpotlightLog
import org.dbpedia.spotlight.exceptions.{SearchException, InputException}
import org.apache.lucene.search.Explanation
import org.dbpedia.spotlight.model._
Expand All @@ -31,15 +31,13 @@ import scala.collection.JavaConverters._
*/
class CuttingEdgeDisambiguator(val contextSearcher: ContextSearcher) extends Disambiguator with ParagraphDisambiguator {

private val LOG = LogFactory.getLog(this.getClass)

LOG.info("Initializing disambiguator object ...")
SpotlightLog.info(this.getClass, "Initializing disambiguator object ...")
val disambiguator : Disambiguator = new MixedWeightsDisambiguator(contextSearcher, new LinearRegressionMixture())

//TODO fix MultiThreading
//val disambiguator : Disambiguator = new MultiThreadedDisambiguatorWrapper(new MixedWeightsDisambiguator(contextSearcher, new LinearRegressionMixture()))

LOG.info("Done.")
SpotlightLog.info(this.getClass, "Done.")

def disambiguate(sfOccurrence: SurfaceFormOccurrence): DBpediaResourceOccurrence = {
disambiguator.disambiguate(sfOccurrence)
Expand Down Expand Up @@ -68,7 +66,7 @@ class CuttingEdgeDisambiguator(val contextSearcher: ContextSearcher) extends Dis
acc + (o -> bestK(o,k).asScala.toList)
} catch {
case e: Exception => {
LOG.debug("Exception: "+e.getMessage)
SpotlightLog.debug(this.getClass, "Exception: %s", e.getMessage)
//e.printStackTrace()
acc
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import org.dbpedia.spotlight.lucene.LuceneManager
import org.dbpedia.spotlight.lucene.search.MergedOccurrencesContextSearcher
import java.io.File
import org.dbpedia.spotlight.lucene.similarity._
import org.apache.commons.logging.LogFactory
import org.dbpedia.spotlight.log.SpotlightLog
import org.apache.lucene.search.Explanation
import org.dbpedia.spotlight.model._
import org.dbpedia.spotlight.lucene.disambiguate.{MixedWeightsDisambiguator, MergedOccurrencesDisambiguator}
Expand All @@ -40,13 +40,11 @@ import scala.collection.JavaConversions._
*/
class DefaultDisambiguator(val contextSearcher: ContextSearcher) extends Disambiguator with ParagraphDisambiguator {

private val LOG = LogFactory.getLog(this.getClass)

LOG.info("Initializing disambiguator object ...")
SpotlightLog.info(this.getClass, "Initializing disambiguator object ...")

val disambiguator : Disambiguator = new MergedOccurrencesDisambiguator(contextSearcher)

LOG.info("Done.")
SpotlightLog.info(this.getClass, "Done.")

def disambiguate(sfOccurrence: SurfaceFormOccurrence): DBpediaResourceOccurrence = {
disambiguator.disambiguate(sfOccurrence)
Expand Down
Loading

0 comments on commit 9b20cbc

Please sign in to comment.