001/** 002 * 003 */ 004package org.dllearner.algorithms.isle.wsd; 005 006import java.util.HashSet; 007import java.util.Set; 008 009import org.apache.log4j.Logger; 010import org.dllearner.algorithms.isle.index.Annotation; 011import org.dllearner.algorithms.isle.index.EntityScorePair; 012import org.dllearner.algorithms.isle.index.SemanticAnnotation; 013import org.semanticweb.owlapi.model.OWLAnnotationAssertionAxiom; 014import org.semanticweb.owlapi.model.OWLAnnotationProperty; 015import org.semanticweb.owlapi.model.OWLDataFactory; 016import org.semanticweb.owlapi.model.OWLEntity; 017import org.semanticweb.owlapi.model.OWLLiteral; 018import org.semanticweb.owlapi.model.OWLOntology; 019import org.semanticweb.owlapi.util.IRIShortFormProvider; 020import org.semanticweb.owlapi.util.SimpleIRIShortFormProvider; 021 022import uk.ac.manchester.cs.owl.owlapi.OWLDataFactoryImpl; 023 024/** 025 * @author Lorenz Buehmann 026 * 027 */ 028public class SimpleWordSenseDisambiguation extends WordSenseDisambiguation{ 029 030 031 private static final Logger logger = Logger.getLogger(SimpleWordSenseDisambiguation.class); 032 033 private IRIShortFormProvider sfp = new SimpleIRIShortFormProvider(); 034 private OWLDataFactory df = new OWLDataFactoryImpl(); 035 private OWLAnnotationProperty annotationProperty = df.getRDFSLabel(); 036 037 /** 038 * @param ontology 039 */ 040 public SimpleWordSenseDisambiguation(OWLOntology ontology) { 041 super(ontology); 042 } 043 044 /* (non-Javadoc) 045 * @see org.dllearner.algorithms.isle.WordSenseDisambiguation#disambiguate(org.dllearner.algorithms.isle.index.Annotation, java.util.Set) 046 */ 047 @Override 048 public SemanticAnnotation disambiguate(Annotation annotation, Set<EntityScorePair> candidateEntities) { 049 logger.debug("Linguistic annotations:\n" + annotation); 050 logger.debug("Candidate entities:" + candidateEntities); 051 String token = annotation.getString().trim(); 052 //check if annotation token matches label of entity or the part behind #(resp. /) 053 for (EntityScorePair entityScorePair : candidateEntities) { 054 OWLEntity entity = entityScorePair.getEntity(); 055 Set<String> labels = getLabels(entity); 056 for (String label : labels) { 057 if (label.equals(token)) { 058 logger.debug("Disambiguated entity: " + entity); 059 return new SemanticAnnotation(annotation, entity); 060 } 061 } 062 String shortForm = sfp.getShortForm(entity.getIRI()); 063 if (annotation.equals(shortForm)) { 064 logger.debug("Disambiguated entity: " + entity); 065 return new SemanticAnnotation(annotation, entity); 066 } 067 } 068 return null; 069 } 070 071 private Set<String> getLabels(OWLEntity entity){ 072 Set<String> labels = new HashSet<>(); 073 Set<OWLAnnotationAssertionAxiom> axioms = ontology.getAnnotationAssertionAxioms(entity.getIRI()); 074 for (OWLAnnotationAssertionAxiom annotation : axioms) { 075 if(annotation.getProperty().equals(annotationProperty)){ 076 if (annotation.getValue() instanceof OWLLiteral) { 077 OWLLiteral val = (OWLLiteral) annotation.getValue(); 078 labels.add(val.getLiteral()); 079 } 080 } 081 } 082 return labels; 083 } 084 085 private Set<String> getRelatedWordPhrases(OWLEntity entity){ 086 //add the labels if exist 087 Set<String> relatedWordPhrases = new HashSet<>(); 088 Set<OWLAnnotationAssertionAxiom> axioms = ontology.getAnnotationAssertionAxioms(entity.getIRI()); 089 for (OWLAnnotationAssertionAxiom annotation : axioms) { 090 if(annotation.getProperty().equals(annotationProperty)){ 091 if (annotation.getValue() instanceof OWLLiteral) { 092 OWLLiteral val = (OWLLiteral) annotation.getValue(); 093 relatedWordPhrases.add(val.getLiteral()); 094 } 095 } 096 } 097 //add the short form of the URI if no labels are available 098 if(relatedWordPhrases.isEmpty()){ 099 relatedWordPhrases.add(sfp.getShortForm(entity.getIRI())); 100 } 101 return relatedWordPhrases; 102 } 103 104}