/** * find candidate ontology elements * * @return */ private List<OntologyElement> findCandidates(SemanticConcept sc, String text) throws Exception { // Set<String> allSuggestions = new HashSet<String>(); OntologyElement el = sc.getOntologyElement(); // what is the population of california? // what is the population of cities in california? // what is the highest point of california? // String uri = null; Set<String> classUris = new HashSet<String>(); if (el instanceof InstanceElement) { List<String> allClassUris = ((InstanceElement) el).getClassURIList(); logger.info("There were:" + allClassUris.size() + " direct types"); // classUris = filterDirectTypes(allClassUris); classUris = new HashSet(allClassUris); logger.info("There is NO FILTERING of direct types..."); } else if (el instanceof InstanceListElement) { List allClassUris = ((InstanceListElement) el).getClassURIList(); logger.info("There were:" + allClassUris.size() + " direct types"); // classUris = filterDirectTypes(allClassUris); classUris = new HashSet(allClassUris); logger.info("There is NO FILTERING of direct types..."); } else if (el instanceof ClassElement) { String uri = ((SerializableURI) el.getData()).toString(); classUris.add(uri); logger.info("NN is class" + uri); } else if (el instanceof DatatypePropertyValueElement) { logger.info("NN is DPVE " + el.getData()); return findCandidatesForADTPV(((DatatypePropertyValueElement) el), text); } else if (el instanceof PropertyElement) { logger.info("NN is property " + el.getData()); return findCandidatesForAProperty(((PropertyElement) el), text); } Set<OntologyElement> elements = new HashSet<OntologyElement>(); Set<String> listOfPotentialCandidates = new HashSet<String>(); Set<String> classes = new HashSet<String>(); long start = System.currentTimeMillis(); for (String uri : classUris) { // //////////properties first////////////////////////// listOfPotentialCandidates = findCandidatesForClass(uri); elements.addAll(returnPropertyElements(listOfPotentialCandidates)); // allSuggestions.addAll(listOfPotentialCandidates); } logger.info("Found " + elements.size() + " elements so far."); // //////////////////////////////////// for (String uri : classUris) { classes.addAll( luceneAnnotator.getNeighbouringClassesWhereGivenClassIsADomain(uri, forceSuperClasses)); logger.info("Found " + classes.size() + " class candidates from IsADomain method."); classes.addAll( luceneAnnotator.getNeighbouringClassesWhereGivenClassIsARange(uri, forceSuperClasses)); logger.info( "Found more, now total: " + classes.size() + " class candidates from IsADomain and isARange method."); } Set<String> filteredClasses = new HashSet<String>(); for (String t : classes) { if (!isInIgnoreNameSpaceList(t)) { filteredClasses.add(t); } } elements.addAll(returnClassElements(filteredClasses)); long end = System.currentTimeMillis(); logger.info( "Found " + elements.size() + " cadidates for " + classUris.size() + " classes for " + (end - start) + "ms."); return new ArrayList(elements); }
/** * @param property * @param text * @return */ private List<OntologyElement> findCandidatesForAProperty(PropertyElement property, String text) throws Exception { // Set<String> allSuggestions = new HashSet<String>(); String uri = ((SerializableURI) property.getData()).toString(); logger.debug("Finding candidates for property:" + property.getData().toString()); // all elements List<OntologyElement> elements = new ArrayList<OntologyElement>(); // feed classes: these are used to find more suggestions but they are also // added to the list! Set<String> feedClasses = new HashSet<String>(); Set<String> properties = new HashSet<String>(); Set<String> allClasses = new HashSet<String>(); Set<String> rangeClasses = luceneAnnotator.findPropertyRange(uri); feedClasses.addAll(rangeClasses); logger.debug("getRangeClassesForProperty:" + uri + " found " + rangeClasses.size()); Set<String> domainClasses = luceneAnnotator.findPropertyDomain(uri); feedClasses.addAll(domainClasses); logger.debug("getDomainClassesForProperty:" + uri + " found " + domainClasses.size()); if (feedClasses == null || feedClasses.size() <= 1) { // find top classes logger.debug("Number of feed classes is 0....forceSuggestions=" + forceSuggestions); if (new Boolean(forceSuggestions).booleanValue() == true) { feedClasses.addAll(luceneAnnotator.findClassURIs()); } } // a onda za te classes nadji properties where class is a domain/range for (String classUri : feedClasses) { properties.addAll( luceneAnnotator.getDefinedPropertiesWhereClassIsADomain(classUri, forceSuperClasses)); properties.addAll( luceneAnnotator.getDefinedPropertiesWhereClassIsARange(classUri, forceSuperClasses)); } // if (new Boolean(forceSuggestions).booleanValue() == true) { logger.info( "cheating!!!!!!!!!!!!!!!!!!!!!!!!!!! here you should not get ALL properties but only relevant ones"); Set<String> datatypePropertiesList = luceneAnnotator.findDatatypePropertyURIs(); Set<String> objectPropertiesList = luceneAnnotator.findObjectPropertyURIs(); Set<String> rdfPropertiesList = luceneAnnotator.findRDFPropertyURIs(null); properties.addAll(datatypePropertiesList); properties.addAll(objectPropertiesList); properties.addAll(rdfPropertiesList); } Set<String> filteredProperties = new HashSet<String>(); for (String t : properties) { if (!isInIgnoreNameSpaceList(t)) { filteredProperties.add(t); } } // add datatype properties which do not have domain defined: mb endDate // beginDate // String table = getSparqlUtils().getDatatypePropertiesNoDomain(); // properties.addAll(gate.freya.util.StringUtil.fromStringToSet(table)); elements.addAll(returnPropertyElements(filteredProperties)); for (String classUri : feedClasses) { allClasses.addAll( luceneAnnotator.getNeighbouringClassesWhereGivenClassIsADomain( classUri, forceSuperClasses)); allClasses.addAll( luceneAnnotator.getNeighbouringClassesWhereGivenClassIsARange( classUri, forceSuperClasses)); } Set<String> filteredFeedClasses = new HashSet<String>(); for (String t : feedClasses) { if (!isInIgnoreNameSpaceList(t)) { filteredFeedClasses.add(t); } } allClasses.addAll(filteredFeedClasses); elements.addAll(returnClassElements(allClasses)); return elements; }