Prepare: json serialization of named entities

This commit is contained in:
Francesco 2022-04-16 16:36:47 +02:00
parent 67a3654451
commit ae158a1505
3 changed files with 39 additions and 4 deletions

View File

@ -15,6 +15,7 @@ dependencies {
implementation 'org.apache.logging.log4j:log4j-core:2.17.2' implementation 'org.apache.logging.log4j:log4j-core:2.17.2'
implementation 'org.slf4j:slf4j-simple:1.7.36' implementation 'org.slf4j:slf4j-simple:1.7.36'
implementation 'net.sourceforge.argparse4j:argparse4j:0.9.0' implementation 'net.sourceforge.argparse4j:argparse4j:0.9.0'
implementation 'org.json:json:20220320'
} }
sourceSets { sourceSets {

View File

@ -1,8 +1,18 @@
package guessNNprepare package guessNNprepare
import org.apache.jena.rdf.model.Resource import org.apache.jena.rdf.model.{Resource, ResourceFactory}
import org.json.{JSONException, JSONObject}
class NamedEntity(val rdfResource: Resource, val Name: String, val readableType: String) { import scala.util.{Failure, Success, Try}
class NamedEntity(val rdfResource: Resource, val name: String, val readableType: String) {
def json: JSONObject = {
new JSONObject()
.put("name", name)
.put("type", readableType)
.put("uri", rdfResource.getURI)
}
def canEqual(other: Any): Boolean = other.isInstanceOf[NamedEntity] def canEqual(other: Any): Boolean = other.isInstanceOf[NamedEntity]
@ -20,3 +30,17 @@ class NamedEntity(val rdfResource: Resource, val Name: String, val readableType:
} }
} }
object NamedEntity {
def apply(json: JSONObject): Try[NamedEntity] = {
try {
Success(new NamedEntity(
ResourceFactory.createResource(json.getString("uri")),
json.getString("name"),
json.getString("type")
))
} catch {
case e: JSONException => Failure(e)
}
}
}

View File

@ -6,8 +6,9 @@ import net.sourceforge.argparse4j.inf.{ArgumentParser, Namespace}
import org.apache.jena.query.QueryExecutionFactory import org.apache.jena.query.QueryExecutionFactory
import org.apache.jena.rdf.model.{InfModel, Model, ModelFactory} import org.apache.jena.rdf.model.{InfModel, Model, ModelFactory}
import org.apache.jena.reasoner.ReasonerRegistry import org.apache.jena.reasoner.ReasonerRegistry
import org.json.JSONArray
import java.io.InputStream import java.io.{FileWriter, InputStream}
object ExtractEntities extends MainCommand { object ExtractEntities extends MainCommand {
@ -39,7 +40,16 @@ object ExtractEntities extends MainCommand {
getMondialNamedEntities(kg, "Sea")) getMondialNamedEntities(kg, "Sea"))
.distinct .distinct
printf("Found %d guessable named entities\n", guessableEntities.size) printf("Found %d guessable named entities\n", guessableEntities.size)
println(s"They would be written to ${jsonFilePath} (not implemented yet)")
println(s"Writing entities to ${jsonFilePath}")
val jsonEntities:JSONArray = new JSONArray()
guessableEntities.foreach(e => jsonEntities.put(e.json))
val writer = new FileWriter(jsonFilePath)
jsonEntities.write(writer)
writer.flush()
writer.close()
println("Done.")
} }
def getMondialNamedEntities(kg: Model, prefixedType: String): List[NamedEntity] = { def getMondialNamedEntities(kg: Model, prefixedType: String): List[NamedEntity] = {