@Test public void testPostalAddressExtraction3() throws ModelRuntimeException, IOException, DataMiningException { Model model = RDF2Go.getModelFactory().createModel(); model.open(); URI addr = model.createURI("urn:juan:loc36"); ResourceModel resourceModel = new ResourceModelImpl(addr); resourceModel.set(NAO.prefLabel, "Cumbria, England"); ResourceModel newResourceModel = postalAddressExtractor.extract(resourceModel); assertEquals("Cumbria", newResourceModel.get(NCO.region)); assertEquals("England", newResourceModel.get(NCO.country)); }
public ResourceModel extract(ResourceModel postalAddress) throws DataMiningException { ResourceModel result = new ResourceModelImpl(postalAddress.getModel(), postalAddress.getIdentifier()); String content = postalAddress.getString(NAO.prefLabel); if ((content == null) || (content.trim().isEmpty())) { logger.info("PostalAddress "+postalAddress.getIdentifier()+" doesn't specify the full postal address as the NAO.prefLabel; " + "skipping postal address extraction..."); return result; } Document doc = null; try { doc = Factory.newDocument(content); synchronized(application) { Corpus corpus = Factory.newCorpus("PostalAddress"+System.currentTimeMillis()); corpus.add(doc); application.setCorpus(corpus); application.execute(); AnnotationSet defaultAnnotSet = doc.getAnnotations(); Set<String> annotTypesRequired = new HashSet<String>(); annotTypesRequired.add("LocationStreet"); annotTypesRequired.add("LocationCity"); annotTypesRequired.add("LocationRegion"); annotTypesRequired.add("LocationPostCode"); annotTypesRequired.add("LocationPOBox"); annotTypesRequired.add("LocationCountry"); annotTypesRequired.add("LocationProvince"); AnnotationSet entityAnnSet = defaultAnnotSet.get(new HashSet<String>(annotTypesRequired)); List<Annotation> entityAnnots = gate.Utils.inDocumentOrder(entityAnnSet); for (Annotation annot : entityAnnots) { String entityValue = null; String entityType = annot.getType(); if ((entityType.equals("LocationCountry")) && ((!(result.has(NCO.country))) || ((result.get(NCO.country)).equals("")))) { entityValue = annot.getFeatures().get("country").toString(); result.set(NCO.country, entityValue); } else if ((entityType.equals("LocationProvince")) && ((!(result.has(NCO.region))) || ((result.get(NCO.region)).equals("")))) { entityValue = annot.getFeatures().get("province").toString(); result.set(NCO.region, entityValue); } else if ((entityType.equals("LocationCity")) && ((!(result.has(NCO.locality))) || ((result.get(NCO.locality)).equals("")))) { entityValue = annot.getFeatures().get("city").toString(); result.set(NCO.locality, entityValue); } else if ((entityType.equals("LocationStreet")) && ((!(result.has(NCO.streetAddress))) || ((result.get(NCO.streetAddress)).equals("")))) { entityValue = annot.getFeatures().get("street").toString(); result.set(NCO.streetAddress, entityValue); } else if ((entityType.equals("LocationPostCode")) && ((!(result.has(NCO.postalcode))) || ((result.get(NCO.postalcode)).equals("")))) { entityValue = annot.getFeatures().get("postcode").toString(); result.set(NCO.postalcode, entityValue); } else if ((entityType.equals("LocationPOBox")) && ((!(result.has(NCO.pobox))) || ((result.get(NCO.pobox)).equals("")))) { entityValue = annot.getFeatures().get("pobox").toString(); result.set(NCO.pobox, entityValue); } else if ((entityType.equals("LocationRegion")) && ((!(result.has(NCO.region))) || ((result.get(NCO.region)).equals("")))) { entityValue = annot.getFeatures().get("region").toString(); result.set(NCO.region, entityValue); } } corpus.cleanup(); corpus.clear(); } } catch (ResourceInstantiationException e) { throw new DataMiningException("Failed to extract named entities from '"+content+"'", e); } catch (ExecutionException e) { throw new DataMiningException("Failed to extract named entities from '"+content+"'", e); } finally { if (doc != null) { Factory.deleteResource(doc); } } return result; }
PostalAddressExtractor extends Base { public ResourceModel extract(ResourceModel postalAddress) throws DataMiningException { ResourceModel result = new ResourceModelImpl(postalAddress.getModel(), postalAddress.getIdentifier()); String content = postalAddress.getString(NAO.prefLabel); if ((content == null) || (content.trim().isEmpty())) { logger.info("PostalAddress "+postalAddress.getIdentifier()+" doesn't specify the full postal address as the NAO.prefLabel; " + "skipping postal address extraction..."); return result; } Document doc = null; try { doc = Factory.newDocument(content); synchronized(application) { Corpus corpus = Factory.newCorpus("PostalAddress"+System.currentTimeMillis()); corpus.add(doc); application.setCorpus(corpus); application.execute(); AnnotationSet defaultAnnotSet = doc.getAnnotations(); Set<String> annotTypesRequired = new HashSet<String>(); annotTypesRequired.add("LocationStreet"); annotTypesRequired.add("LocationCity"); annotTypesRequired.add("LocationRegion"); annotTypesRequired.add("LocationPostCode"); annotTypesRequired.add("LocationPOBox"); annotTypesRequired.add("LocationCountry"); annotTypesRequired.add("LocationProvince"); AnnotationSet entityAnnSet = defaultAnnotSet.get(new HashSet<String>(annotTypesRequired)); List<Annotation> entityAnnots = gate.Utils.inDocumentOrder(entityAnnSet); for (Annotation annot : entityAnnots) { String entityValue = null; String entityType = annot.getType(); if ((entityType.equals("LocationCountry")) && ((!(result.has(NCO.country))) || ((result.get(NCO.country)).equals("")))) { entityValue = annot.getFeatures().get("country").toString(); result.set(NCO.country, entityValue); } else if ((entityType.equals("LocationProvince")) && ((!(result.has(NCO.region))) || ((result.get(NCO.region)).equals("")))) { entityValue = annot.getFeatures().get("province").toString(); result.set(NCO.region, entityValue); } else if ((entityType.equals("LocationCity")) && ((!(result.has(NCO.locality))) || ((result.get(NCO.locality)).equals("")))) { entityValue = annot.getFeatures().get("city").toString(); result.set(NCO.locality, entityValue); } else if ((entityType.equals("LocationStreet")) && ((!(result.has(NCO.streetAddress))) || ((result.get(NCO.streetAddress)).equals("")))) { entityValue = annot.getFeatures().get("street").toString(); result.set(NCO.streetAddress, entityValue); } else if ((entityType.equals("LocationPostCode")) && ((!(result.has(NCO.postalcode))) || ((result.get(NCO.postalcode)).equals("")))) { entityValue = annot.getFeatures().get("postcode").toString(); result.set(NCO.postalcode, entityValue); } else if ((entityType.equals("LocationPOBox")) && ((!(result.has(NCO.pobox))) || ((result.get(NCO.pobox)).equals("")))) { entityValue = annot.getFeatures().get("pobox").toString(); result.set(NCO.pobox, entityValue); } else if ((entityType.equals("LocationRegion")) && ((!(result.has(NCO.region))) || ((result.get(NCO.region)).equals("")))) { entityValue = annot.getFeatures().get("region").toString(); result.set(NCO.region, entityValue); } } corpus.cleanup(); corpus.clear(); } } catch (ResourceInstantiationException e) { throw new DataMiningException("Failed to extract named entities from '"+content+"'", e); } catch (ExecutionException e) { throw new DataMiningException("Failed to extract named entities from '"+content+"'", e); } finally { if (doc != null) { Factory.deleteResource(doc); } } return result; } }
PostalAddressExtractor extends Base { public ResourceModel extract(ResourceModel postalAddress) throws DataMiningException { ResourceModel result = new ResourceModelImpl(postalAddress.getModel(), postalAddress.getIdentifier()); String content = postalAddress.getString(NAO.prefLabel); if ((content == null) || (content.trim().isEmpty())) { logger.info("PostalAddress "+postalAddress.getIdentifier()+" doesn't specify the full postal address as the NAO.prefLabel; " + "skipping postal address extraction..."); return result; } Document doc = null; try { doc = Factory.newDocument(content); synchronized(application) { Corpus corpus = Factory.newCorpus("PostalAddress"+System.currentTimeMillis()); corpus.add(doc); application.setCorpus(corpus); application.execute(); AnnotationSet defaultAnnotSet = doc.getAnnotations(); Set<String> annotTypesRequired = new HashSet<String>(); annotTypesRequired.add("LocationStreet"); annotTypesRequired.add("LocationCity"); annotTypesRequired.add("LocationRegion"); annotTypesRequired.add("LocationPostCode"); annotTypesRequired.add("LocationPOBox"); annotTypesRequired.add("LocationCountry"); annotTypesRequired.add("LocationProvince"); AnnotationSet entityAnnSet = defaultAnnotSet.get(new HashSet<String>(annotTypesRequired)); List<Annotation> entityAnnots = gate.Utils.inDocumentOrder(entityAnnSet); for (Annotation annot : entityAnnots) { String entityValue = null; String entityType = annot.getType(); if ((entityType.equals("LocationCountry")) && ((!(result.has(NCO.country))) || ((result.get(NCO.country)).equals("")))) { entityValue = annot.getFeatures().get("country").toString(); result.set(NCO.country, entityValue); } else if ((entityType.equals("LocationProvince")) && ((!(result.has(NCO.region))) || ((result.get(NCO.region)).equals("")))) { entityValue = annot.getFeatures().get("province").toString(); result.set(NCO.region, entityValue); } else if ((entityType.equals("LocationCity")) && ((!(result.has(NCO.locality))) || ((result.get(NCO.locality)).equals("")))) { entityValue = annot.getFeatures().get("city").toString(); result.set(NCO.locality, entityValue); } else if ((entityType.equals("LocationStreet")) && ((!(result.has(NCO.streetAddress))) || ((result.get(NCO.streetAddress)).equals("")))) { entityValue = annot.getFeatures().get("street").toString(); result.set(NCO.streetAddress, entityValue); } else if ((entityType.equals("LocationPostCode")) && ((!(result.has(NCO.postalcode))) || ((result.get(NCO.postalcode)).equals("")))) { entityValue = annot.getFeatures().get("postcode").toString(); result.set(NCO.postalcode, entityValue); } else if ((entityType.equals("LocationPOBox")) && ((!(result.has(NCO.pobox))) || ((result.get(NCO.pobox)).equals("")))) { entityValue = annot.getFeatures().get("pobox").toString(); result.set(NCO.pobox, entityValue); } else if ((entityType.equals("LocationRegion")) && ((!(result.has(NCO.region))) || ((result.get(NCO.region)).equals("")))) { entityValue = annot.getFeatures().get("region").toString(); result.set(NCO.region, entityValue); } } corpus.cleanup(); corpus.clear(); } } catch (ResourceInstantiationException e) { throw new DataMiningException("Failed to extract named entities from '"+content+"'", e); } catch (ExecutionException e) { throw new DataMiningException("Failed to extract named entities from '"+content+"'", e); } finally { if (doc != null) { Factory.deleteResource(doc); } } return result; } PostalAddressExtractor(); }
PostalAddressExtractor extends Base { public ResourceModel extract(ResourceModel postalAddress) throws DataMiningException { ResourceModel result = new ResourceModelImpl(postalAddress.getModel(), postalAddress.getIdentifier()); String content = postalAddress.getString(NAO.prefLabel); if ((content == null) || (content.trim().isEmpty())) { logger.info("PostalAddress "+postalAddress.getIdentifier()+" doesn't specify the full postal address as the NAO.prefLabel; " + "skipping postal address extraction..."); return result; } Document doc = null; try { doc = Factory.newDocument(content); synchronized(application) { Corpus corpus = Factory.newCorpus("PostalAddress"+System.currentTimeMillis()); corpus.add(doc); application.setCorpus(corpus); application.execute(); AnnotationSet defaultAnnotSet = doc.getAnnotations(); Set<String> annotTypesRequired = new HashSet<String>(); annotTypesRequired.add("LocationStreet"); annotTypesRequired.add("LocationCity"); annotTypesRequired.add("LocationRegion"); annotTypesRequired.add("LocationPostCode"); annotTypesRequired.add("LocationPOBox"); annotTypesRequired.add("LocationCountry"); annotTypesRequired.add("LocationProvince"); AnnotationSet entityAnnSet = defaultAnnotSet.get(new HashSet<String>(annotTypesRequired)); List<Annotation> entityAnnots = gate.Utils.inDocumentOrder(entityAnnSet); for (Annotation annot : entityAnnots) { String entityValue = null; String entityType = annot.getType(); if ((entityType.equals("LocationCountry")) && ((!(result.has(NCO.country))) || ((result.get(NCO.country)).equals("")))) { entityValue = annot.getFeatures().get("country").toString(); result.set(NCO.country, entityValue); } else if ((entityType.equals("LocationProvince")) && ((!(result.has(NCO.region))) || ((result.get(NCO.region)).equals("")))) { entityValue = annot.getFeatures().get("province").toString(); result.set(NCO.region, entityValue); } else if ((entityType.equals("LocationCity")) && ((!(result.has(NCO.locality))) || ((result.get(NCO.locality)).equals("")))) { entityValue = annot.getFeatures().get("city").toString(); result.set(NCO.locality, entityValue); } else if ((entityType.equals("LocationStreet")) && ((!(result.has(NCO.streetAddress))) || ((result.get(NCO.streetAddress)).equals("")))) { entityValue = annot.getFeatures().get("street").toString(); result.set(NCO.streetAddress, entityValue); } else if ((entityType.equals("LocationPostCode")) && ((!(result.has(NCO.postalcode))) || ((result.get(NCO.postalcode)).equals("")))) { entityValue = annot.getFeatures().get("postcode").toString(); result.set(NCO.postalcode, entityValue); } else if ((entityType.equals("LocationPOBox")) && ((!(result.has(NCO.pobox))) || ((result.get(NCO.pobox)).equals("")))) { entityValue = annot.getFeatures().get("pobox").toString(); result.set(NCO.pobox, entityValue); } else if ((entityType.equals("LocationRegion")) && ((!(result.has(NCO.region))) || ((result.get(NCO.region)).equals("")))) { entityValue = annot.getFeatures().get("region").toString(); result.set(NCO.region, entityValue); } } corpus.cleanup(); corpus.clear(); } } catch (ResourceInstantiationException e) { throw new DataMiningException("Failed to extract named entities from '"+content+"'", e); } catch (ExecutionException e) { throw new DataMiningException("Failed to extract named entities from '"+content+"'", e); } finally { if (doc != null) { Factory.deleteResource(doc); } } return result; } PostalAddressExtractor(); ResourceModel extract(ResourceModel postalAddress); }
PostalAddressExtractor extends Base { public ResourceModel extract(ResourceModel postalAddress) throws DataMiningException { ResourceModel result = new ResourceModelImpl(postalAddress.getModel(), postalAddress.getIdentifier()); String content = postalAddress.getString(NAO.prefLabel); if ((content == null) || (content.trim().isEmpty())) { logger.info("PostalAddress "+postalAddress.getIdentifier()+" doesn't specify the full postal address as the NAO.prefLabel; " + "skipping postal address extraction..."); return result; } Document doc = null; try { doc = Factory.newDocument(content); synchronized(application) { Corpus corpus = Factory.newCorpus("PostalAddress"+System.currentTimeMillis()); corpus.add(doc); application.setCorpus(corpus); application.execute(); AnnotationSet defaultAnnotSet = doc.getAnnotations(); Set<String> annotTypesRequired = new HashSet<String>(); annotTypesRequired.add("LocationStreet"); annotTypesRequired.add("LocationCity"); annotTypesRequired.add("LocationRegion"); annotTypesRequired.add("LocationPostCode"); annotTypesRequired.add("LocationPOBox"); annotTypesRequired.add("LocationCountry"); annotTypesRequired.add("LocationProvince"); AnnotationSet entityAnnSet = defaultAnnotSet.get(new HashSet<String>(annotTypesRequired)); List<Annotation> entityAnnots = gate.Utils.inDocumentOrder(entityAnnSet); for (Annotation annot : entityAnnots) { String entityValue = null; String entityType = annot.getType(); if ((entityType.equals("LocationCountry")) && ((!(result.has(NCO.country))) || ((result.get(NCO.country)).equals("")))) { entityValue = annot.getFeatures().get("country").toString(); result.set(NCO.country, entityValue); } else if ((entityType.equals("LocationProvince")) && ((!(result.has(NCO.region))) || ((result.get(NCO.region)).equals("")))) { entityValue = annot.getFeatures().get("province").toString(); result.set(NCO.region, entityValue); } else if ((entityType.equals("LocationCity")) && ((!(result.has(NCO.locality))) || ((result.get(NCO.locality)).equals("")))) { entityValue = annot.getFeatures().get("city").toString(); result.set(NCO.locality, entityValue); } else if ((entityType.equals("LocationStreet")) && ((!(result.has(NCO.streetAddress))) || ((result.get(NCO.streetAddress)).equals("")))) { entityValue = annot.getFeatures().get("street").toString(); result.set(NCO.streetAddress, entityValue); } else if ((entityType.equals("LocationPostCode")) && ((!(result.has(NCO.postalcode))) || ((result.get(NCO.postalcode)).equals("")))) { entityValue = annot.getFeatures().get("postcode").toString(); result.set(NCO.postalcode, entityValue); } else if ((entityType.equals("LocationPOBox")) && ((!(result.has(NCO.pobox))) || ((result.get(NCO.pobox)).equals("")))) { entityValue = annot.getFeatures().get("pobox").toString(); result.set(NCO.pobox, entityValue); } else if ((entityType.equals("LocationRegion")) && ((!(result.has(NCO.region))) || ((result.get(NCO.region)).equals("")))) { entityValue = annot.getFeatures().get("region").toString(); result.set(NCO.region, entityValue); } } corpus.cleanup(); corpus.clear(); } } catch (ResourceInstantiationException e) { throw new DataMiningException("Failed to extract named entities from '"+content+"'", e); } catch (ExecutionException e) { throw new DataMiningException("Failed to extract named entities from '"+content+"'", e); } finally { if (doc != null) { Factory.deleteResource(doc); } } return result; } PostalAddressExtractor(); ResourceModel extract(ResourceModel postalAddress);  }