-
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
Merged
+154
−2
Merged
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
8a010e2
Initial Adapter for scala-grounders
robertvacareanu ccab059
Add details
robertvacareanu cdf5a44
Dependency update
robertvacareanu 6f0a649
Comments
robertvacareanu d9afa85
Test
robertvacareanu faddb7a
Adapter
robertvacareanu cc5017a
Add Adapter in Grounder Factory
robertvacareanu 6bb84e2
Test command
robertvacareanu 3b27cc0
Use `mkFast` on Grounder
robertvacareanu bb28676
Comments
robertvacareanu 0f33f19
Version
robertvacareanu a8be06c
Merge branch 'main' into rvacareanu/grounder
myedibleenso File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next
Next commit
Initial Adapter for scala-grounders
- Loading branch information
commit 8a010e231cf08bf45d581f67328e1b19fe7f7a3b
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
...scala/src/main/scala/org/ml4ai/skema/text_reading/grounding/scala_grounders/Adapter.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
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 | ||
|
||
/** | ||
* 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 grounder = SequentialGrounder() | ||
def groundingCandidates(texts: Seq[String], k: Int): Seq[Seq[GroundingCandidate]] = { | ||
val concepts = groundingConcepts.map(fromConceptToDKG) | ||
texts.map { text => | ||
grounder.ground(text, concepts, k) | ||
.map { result => | ||
println(result) | ||
GroundingCandidate(fromDKGToConcept(result.dkg), result.score) | ||
} | ||
.force.toSeq | ||
} | ||
} | ||
|
||
/** | ||
* Transform a SKEMA's `GroundingConcept` to a scala-grounders' `DKG` | ||
* They have similar meanings, so the map is 1:1 | ||
* | ||
* @param concept | ||
* @return | ||
*/ | ||
private 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 | ||
*/ | ||
private def fromDKGToConcept(dkg: DKG): GroundingConcept = { | ||
GroundingConcept(dkg.id, dkg.name, dkg.description, Option(dkg.synonyms.map(_.value)), None) | ||
} | ||
|
||
} | ||
|
||
object AdHocExample extends App { | ||
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) | ||
// println(sga.grounder.components.toList.toSeq.map(_.getName)) | ||
// val result = sga.groundingCandidates(Seq("dog dog dog"), 10) | ||
val result = sga.groundingCandidates(Seq("dog"), 10) | ||
// result.head.foreach(println) | ||
// println(ConfigFactory.load()) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
That's a clever use of Streams. IntelliJ says that the .toSeq is superfluous.