Created
June 6, 2020 13:53
-
-
Save Dviejopomata/f8b103dd4ccf9b9e55cee6fb34eaaa25 to your computer and use it in GitHub Desktop.
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
package uoc.exercise; | |
import java.io.FileNotFoundException; | |
import java.io.FileOutputStream; | |
import java.io.IOException; | |
import java.lang.reflect.Array; | |
import java.util.List; | |
import org.apache.log4j.BasicConfigurator; | |
import org.apache.log4j.Logger; | |
import uoc.utils.FileUtilities; | |
import com.hp.hpl.jena.ontology.Individual; | |
import com.hp.hpl.jena.ontology.OntClass; | |
import com.hp.hpl.jena.ontology.OntModel; | |
import com.hp.hpl.jena.ontology.OntModelSpec; | |
import com.hp.hpl.jena.ontology.OntResource; | |
import com.hp.hpl.jena.rdf.model.InfModel; | |
import com.hp.hpl.jena.rdf.model.Model; | |
import com.hp.hpl.jena.rdf.model.ModelFactory; | |
import com.hp.hpl.jena.rdf.model.ModelMaker; | |
import com.hp.hpl.jena.rdf.model.Property; | |
import com.hp.hpl.jena.rdf.model.RDFNode; | |
import com.hp.hpl.jena.rdf.model.Resource; | |
import com.hp.hpl.jena.rdf.model.Statement; | |
import com.hp.hpl.jena.rdf.model.StmtIterator; | |
import com.hp.hpl.jena.reasoner.Reasoner; | |
import com.hp.hpl.jena.reasoner.rulesys.GenericRuleReasoner; | |
import com.hp.hpl.jena.reasoner.rulesys.Rule; | |
import com.hp.hpl.jena.util.FileManager; | |
import com.hp.hpl.jena.util.PrintUtil; | |
import com.hp.hpl.jena.util.iterator.ExtendedIterator; | |
import com.hp.hpl.jena.vocabulary.RDF; | |
import com.hp.hpl.jena.ontology.DatatypeProperty; | |
public class Exercise { | |
private FileUtilities fileUtilities; | |
static Logger logger = Logger.getLogger(Exercise.class); | |
public static final String NSTWITTER = "http://www.semanticweb.org/laia/ontologies/2020/1/Twitter#"; | |
public static final String GEO = "http://www.w3.org/2003/01/geo/wgs84_pos#"; | |
public Exercise() { | |
super(); | |
fileUtilities = new FileUtilities(); | |
} | |
/** | |
* @param args | |
*/ | |
public static void main(String[] args) { | |
Exercise app = new Exercise(); | |
app.run(); | |
} | |
private void run() { | |
List<List<String>> fileContents = null; | |
try { | |
fileContents = fileUtilities.readFileFromClasspath("data/Tweets.csv"); | |
} catch (FileNotFoundException e) { | |
System.err.println("ERROR: File not found"); | |
e.printStackTrace(); | |
} catch (IOException e) { | |
System.err.println("ERROR: I/O error"); | |
e.printStackTrace(); | |
} | |
if (fileContents == null) { | |
return; | |
} | |
BasicConfigurator.configure(); | |
Model model = FileManager.get().loadModel("file:src/data/PAC3_2020_03_28.owl"); | |
OntModel om = sectionA(fileContents, model); | |
sectionB(om); | |
} | |
/** | |
* Exercici 2 A | |
* | |
* @param fileContents | |
* @param model | |
*/ | |
private OntModel sectionA(List<List<String>> fileContents, Model model) { | |
OntModel m = createOntModel(model); | |
Individual aTweet, userName, airline, qualifier; | |
for (List<String> row : fileContents) { | |
// TODO 1 Create Tweet | |
aTweet = createIndividual(m, "Tweet", row.get(0)); | |
System.out.println("Atweet " + aTweet.toString()); | |
if (aTweet != null) { | |
// TODO 2 | |
// create instance Name | |
userName = createIndividual(m, "Name", "name_" + row.get(7)); | |
// add has_user_name | |
if (row.get(7) != null) { | |
userName.addLiteral(m.getProperty(NSTWITTER + "hasValue"), row.get(7)); | |
} | |
addObjectPropertyToIndividual(m, aTweet, "hasUserName", userName); | |
// add Latitude and Longitude | |
final String latLon = row.get(11); | |
if (latLon != null && !latLon.equals("")) { | |
String[] parts = latLon.split(","); | |
final double lat = Double.parseDouble(parts[0].replace("[", "")); | |
final double lon = Double.parseDouble(parts[1].replace("]", "")); | |
final Individual latitude = createIndividual(m, "Latitude", "latitude_" + row.get(7)); | |
latitude.addLiteral(m.getProperty(NSTWITTER + "hasValue"), lat); | |
final Individual longitude = createIndividual(m, "Longitude", "longitude_" + row.get(7)); | |
longitude.addLiteral(m.getProperty(NSTWITTER + "hasValue"), lon); | |
addObjectPropertyToIndividual(m, aTweet, "lat", latitude); | |
addObjectPropertyToIndividual(m, aTweet, "lon", longitude); | |
} | |
// add Airline_sentiment_confidence | |
qualifier = createIndividual(m, "airline_sentiment_confidence", | |
"airline_sentiment_confidence_" + row.get(7)); | |
System.out.println(row.get(2)); | |
qualifier.addLiteral(m.getProperty(NSTWITTER + "hasValue"),Double.parseDouble( row.get(2))); | |
addObjectPropertyToIndividual(m, aTweet, "airlineSentiment", qualifier); | |
// add Airline | |
airline = createIndividual(m, "Airline", "Airline_" + row.get(7)); | |
airline.addLiteral(m.getProperty(NSTWITTER + "hasValue"), row.get(2)); | |
addObjectPropertyToIndividual(m, aTweet, "Airline_", airline); | |
} | |
} | |
// Print Tweets | |
printTweets(m); | |
// Print Time > 0.5 | |
printMoreThan(m, 0.5); | |
return m; | |
} | |
/** | |
* Create an individual from class | |
* | |
* @param m | |
* @param name | |
* @return | |
*/ | |
private Individual createIndividual(OntModel m, String className, String name) { | |
final OntClass tweet = m.getOntClass(NSTWITTER + className); | |
return m.createIndividual(name, tweet); | |
} | |
/** | |
* Function add a property with a value, if value is different "" | |
* | |
* @param m | |
* @param i | |
* @param property | |
* @param value | |
*/ | |
private void addObjectPropertyToIndividual(OntModel m, Individual i, String property, Individual value) { | |
i.addProperty(m.getProperty(NSTWITTER + property), value); | |
} | |
/** | |
* @param m | |
*/ | |
private void printMoreThan(OntModel m, double limit) { | |
System.out.println("Instances more than " + limit); | |
// TODO 4 | |
final ExtendedIterator<? extends OntResource> it = m.getOntClass(NSTWITTER + "Tweet").listInstances(); | |
RDFNode airlineConfidence, valueAirline; | |
while (it.hasNext()) { | |
final OntResource ontResource = it.next(); | |
airlineConfidence = ontResource.getPropertyValue(m.getProperty(NSTWITTER + "airline_sentiment_confidence")); | |
if(airlineConfidence!=null) { | |
// System.out.println(airlineConfidence); | |
valueAirline = ((OntResource)airlineConfidence).getPropertyValue(m.getProperty(NSTWITTER + "hasValue")); | |
if(valueAirline!=null) { | |
// System.out.println("HAS VALUE"); | |
if(valueAirline.isLiteral()) { | |
// System.out.println("IS LITERAL "+ valueAirline.asLiteral().getDouble()); | |
if(valueAirline.asLiteral().getDouble() > limit) { | |
System.out.println(" - " + PrintUtil.print(ontResource)); | |
} | |
} | |
} | |
} | |
} | |
} | |
/** | |
* @param m | |
*/ | |
private void printTweets(OntModel m) { | |
// TODO 3 | |
System.out.println("Tweets: "); | |
final ExtendedIterator<? extends OntResource> it = m.getOntClass(NSTWITTER + "Tweet").listInstances(); | |
while (it.hasNext()) { | |
final OntResource ontResource = it.next(); | |
System.out.println(" - " + PrintUtil.print(ontResource)); | |
} | |
} | |
/** | |
* Exercise 2 B | |
* | |
* @param om | |
*/ | |
private void sectionB(OntModel om) { | |
List<Rule> rules = Rule.rulesFromURL("file:src/data/Rules.rules"); | |
Reasoner reasoner2 = new GenericRuleReasoner(rules); | |
reasoner2 = reasoner2.bindSchema(om); | |
InfModel infmodel = ModelFactory.createInfModel(reasoner2, om); | |
printStrongConfidence(infmodel); | |
} | |
private void printStrongConfidence(InfModel m) { | |
System.out.println("StrongConfidence: "); | |
if (m != null) | |
printStatements(m, null, RDF.type, m.getResource(NSTWITTER + "StrongConfidence")); | |
} | |
/** | |
* Generate an OntModel | |
* | |
* @param model | |
* @return | |
*/ | |
private OntModel createOntModel(Model model) { | |
ModelMaker maker = ModelFactory.createMemModelMaker(); | |
OntModelSpec spec = new OntModelSpec(OntModelSpec.OWL_MEM_MICRO_RULE_INF); | |
spec.setImportModelMaker(maker); | |
OntModel m = ModelFactory.createOntologyModel(spec, model); | |
return m; | |
} | |
public static void printStatements(Model m, Resource s, Property p, Resource o) { | |
if (m != null && (s != null || p != null || o != null)) { | |
for (StmtIterator i = m.listStatements(s, p, o); i.hasNext();) { | |
Statement stmt = i.nextStatement(); | |
if (stmt.getObject().isURIResource()) { | |
System.out.println(" - " + PrintUtil.print(stmt)); | |
} else if (stmt.getObject().isLiteral()) { | |
System.out.println(" - " + PrintUtil.print(stmt)); | |
} | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment