-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adapter for Scala Grounders (rvacareanu/grounder) #887
Changes from all commits
8a010e2
ccab059
cdf5a44
6f0a649
d9afa85
faddb7a
cc5017a
6bb84e2
3b27cc0
bb28676
0f33f19
a8be06c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package org.ml4ai.skema.grounding.scala_grounders | ||
|
||
import org.clulab.scala_grounders.grounding.GroundingConfig | ||
import org.ml4ai.skema.text_reading.grounding.Grounder | ||
import org.ml4ai.skema.text_reading.grounding.GroundingCandidate | ||
import org.ml4ai.skema.text_reading.grounding.GroundingConcept | ||
import com.typesafe.config.Config | ||
import org.clulab.scala_grounders.grounding.SequentialGrounder | ||
import org.clulab.scala_grounders.model.DKG | ||
import org.clulab.scala_grounders.model.DKGSynonym | ||
import com.typesafe.config.ConfigFactory | ||
import org.clulab.scala_grounders.using | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Recently we've been using the the
often looking like |
||
import org.clulab.scala_grounders.model.DKG | ||
import scala.io.Source | ||
|
||
|
||
/** | ||
* This class adapts the data definitions from this project to work with scala-grounder's definition | ||
* Concretely, the changes needed are: | ||
* - SKEMA's GroundingConcept to scala-grounder's DKG (avalaible in `fromConceptToDKG`) | ||
* - scala-grounder's DKG to SKEMA's GroundingConcept (avalaible in `fromDKGToConcept`) | ||
* - Create the scala-grounder Grounder (`grounder = SequentialGrounder()`) | ||
* - Changing `groundingCandidates` to call the right method from the scala-grounder side | ||
* | ||
* @param groundingConcepts -> The concepts which we will use to do the grounding | ||
* Every candidate text for grounding (i.e. any text that we | ||
* want to ground) will be grounded on these concepts | ||
* (Note: depending on the implementation, it is possible that | ||
* none of these groundingConcepts candidates are suitable, so | ||
* we might not return anything; however, we will never return | ||
* a concept that is outside this) | ||
*/ | ||
class ScalaGroundersAdapter(groundingConcepts: Seq[GroundingConcept]) extends Grounder { | ||
lazy val concepts = groundingConcepts.map(fromConceptToDKG) | ||
lazy val grounder = SequentialGrounder().mkFast(concepts) | ||
def groundingCandidates(texts: Seq[String], k: Int): Seq[Seq[GroundingCandidate]] = { | ||
texts.map { text => | ||
// TODO Maybe provide additional context (useful for NeuralGrounder) | ||
grounder.ground(text, None, concepts, k) | ||
.map { result => | ||
GroundingCandidate(fromDKGToConcept(result.dkg), result.score, details = Some(result.groundingDetails.grounderName)) | ||
} | ||
.force.toSeq | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's a clever use of Streams. IntelliJ says that the .toSeq is superfluous. |
||
} | ||
} | ||
|
||
/** | ||
* Transform a SKEMA's `GroundingConcept` to a scala-grounders' `DKG` | ||
* They have similar meanings, so the map is 1:1 | ||
* | ||
* @param concept | ||
* @return | ||
*/ | ||
def fromConceptToDKG(concept: GroundingConcept): DKG = { | ||
DKG(concept.id, concept.name, concept.description, concept.synonyms.map { synonyms => synonyms.map { s => DKGSynonym(s, None) } }.getOrElse(Seq.empty)) | ||
} | ||
|
||
/** | ||
* Transform a scala-grounder' `DKG` to SKEMA's `GroundingConcept` | ||
* They have similar meanings, so the map is 1:1 | ||
* | ||
* @param dkg | ||
* @return | ||
*/ | ||
def fromDKGToConcept(dkg: DKG): GroundingConcept = { | ||
GroundingConcept(dkg.id, dkg.name, dkg.description, Option(dkg.synonyms.map(_.value)), None) | ||
} | ||
|
||
} | ||
/** | ||
* Provide altenatives way of creating a `ScalaGroundersAdapter` | ||
*/ | ||
object ScalaGroundersAdapter { | ||
def apply(groundingConcepts: Seq[GroundingConcept]): ScalaGroundersAdapter = new ScalaGroundersAdapter(groundingConcepts) | ||
def fromDkgs(dkgs: Seq[DKG]): ScalaGroundersAdapter = new ScalaGroundersAdapter(dkgs.map(dkg => GroundingConcept(dkg.id, dkg.name, dkg.description, Option(dkg.synonyms.map(_.value)), None))) | ||
def fromFile(groundingConceptsPath: String): ScalaGroundersAdapter = { | ||
val concepts = using(Source.fromFile(groundingConceptsPath)) { it => | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Source.fromFile without an encoding specified may be a problem on machines where they have Java using an unexpected default encoding. It might be easier to play it safe here and save tracking it down later. |
||
ujson.read(it.mkString).arr.map(it => DKG.fromJson(it)) | ||
} | ||
ScalaGroundersAdapter.fromDkgs(concepts) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package org.ml4ai.skema.grounding.scala_grounders | ||
|
||
import org.ml4ai.skema.test.Test | ||
|
||
import java.nio.{Buffer, ByteBuffer, ByteOrder} | ||
|
||
import org.ml4ai.skema.text_reading.grounding.Grounder | ||
import org.ml4ai.skema.text_reading.grounding.GroundingCandidate | ||
import org.ml4ai.skema.text_reading.grounding.GroundingConcept | ||
|
||
/** | ||
* | ||
* Running command: | ||
* sbt "testOnly org.ml4ai.skema.grounding.scala_grounders.TestAdapter" | ||
*/ | ||
class TestAdapter extends Test { | ||
|
||
behavior of "ScalaGroundersAdapter" | ||
|
||
val gcs = Seq( | ||
GroundingConcept( | ||
id = "id1", | ||
name = "dog", | ||
description = Some("this is a cute dog"), | ||
synonyms = None, | ||
embedding = None | ||
), | ||
GroundingConcept( | ||
id = "id2", | ||
name = "cat", | ||
description = Some("this is a cute cat"), | ||
synonyms = None, | ||
embedding = None | ||
), | ||
GroundingConcept( | ||
id = "id3", | ||
name = "dog cat", | ||
description = Some("here we have a dog and a cat"), | ||
synonyms = None, | ||
embedding = None | ||
), | ||
GroundingConcept( | ||
id = "id4", | ||
name = "cat", | ||
description = Some("this is a cute cat"), | ||
synonyms = None, | ||
embedding = None | ||
), | ||
) | ||
|
||
val sga = new ScalaGroundersAdapter(gcs) | ||
|
||
val result = sga.groundingCandidates(Seq("dog"), 10).head | ||
|
||
// Check that the first one is a GroundingCandidate with id1 | ||
it should "ground" in { | ||
result.foreach(println) | ||
result.head.concept.id should be ("id1") | ||
} | ||
|
||
|
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey, that's Python! ;-)
groundingConceptsPath = ontologyFilePath