@Test public void testFullnameExtraction10() throws ModelRuntimeException, IOException, DataMiningException { Model model = RDF2Go.getModelFactory().createModel(); model.open(); URI name = model.createURI("urn:juan:personName4"); ResourceModel resourceModel = new ResourceModelImpl(name); resourceModel.set(NCO.fullname, "Ismael"); ResourceModel newResourceModel = fullnameExtractor.extract(resourceModel); assertEquals("Ismael", newResourceModel.get(NCO.nameGiven)); }
public ResourceModel extract(ResourceModel personName) throws DataMiningException { ResourceModel result = new ResourceModelImpl(personName.getModel(), personName.getIdentifier()); String content = personName.getString(NCO.fullname); if ((content == null) || (content.trim().isEmpty())) { logger.info("PersonName "+personName.getIdentifier()+" doesn't specify the contact's fullname as the NCO.fullname; " + "skipping fullname extraction..."); return result; } Document doc = null; try { doc = Factory.newDocument(content); synchronized(application) { Corpus corpus = Factory.newCorpus("Fullname"+System.currentTimeMillis()); corpus.add(doc); application.setCorpus(corpus); application.execute(); AnnotationSet defaultAnnotSet = doc.getAnnotations(); Set<String> annotTypesRequired = new HashSet<String>(); annotTypesRequired.add("PersonEntities"); AnnotationSet entityAnnSet = defaultAnnotSet.get(new HashSet<String>(annotTypesRequired)); List<Annotation> entityAnnots = gate.Utils.inDocumentOrder(entityAnnSet); for (Annotation annot : entityAnnots) { String entityType = annot.getType(); String firstname = null; String surname = null; String prefix = null; String suffix = null; String gender = null; if (entityType.equals("PersonEntities")) { if ((!(annot.getFeatures().get("prefix").toString().isEmpty())) && ((!(result.has(NCO.nameHonorificPrefix))) || ((result.get(NCO.nameHonorificPrefix)).equals("")))) { prefix = annot.getFeatures().get("prefix").toString(); result.set(NCO.nameHonorificPrefix, prefix); } if ((!(annot.getFeatures().get("firstname").toString().isEmpty())) && ((!(result.has(NCO.nameGiven))) || ((result.get(NCO.nameGiven)).equals("")))) { firstname = annot.getFeatures().get("firstname").toString(); result.set(NCO.nameGiven, firstname); } if ((!(annot.getFeatures().get("surname").toString().isEmpty())) && ((!(result.has(NCO.nameFamily))) || ((result.get(NCO.nameFamily)).equals("")))) { surname = annot.getFeatures().get("surname").toString(); result.set(NCO.nameFamily, surname); } if ((!(annot.getFeatures().get("suffix").toString().isEmpty())) && ((!(result.has(NCO.nameHonorificSuffix))) || ((result.get(NCO.nameHonorificSuffix)).equals("")))) { suffix = annot.getFeatures().get("suffix").toString(); result.set(NCO.nameHonorificSuffix, suffix); } if ((!(annot.getFeatures().get("gender").toString().isEmpty())) && ((!(result.has(NCO.gender))) || ((result.get(NCO.gender)).equals("")))) { gender = annot.getFeatures().get("gender").toString(); result.set(NCO.gender, gender); } } } 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); } } if (((!(result.has(NCO.nameGiven))) || ((result.get(NCO.nameGiven)).equals(""))) && ((!(result.has(NCO.nameAdditional))) || ((result.get(NCO.nameAdditional)).equals(""))) && ((!(result.has(NCO.nameFamily))) || ((result.get(NCO.nameFamily)).equals("")))) { String[] fullname = content.split(" "); if (fullname.length == 1) { result.set(NCO.nameGiven, fullname[0]); } else if (fullname.length == 2) { result.set(NCO.nameGiven, fullname[0]); result.set(NCO.nameFamily, fullname[1]); } else if (fullname.length == 3) { result.set(NCO.nameGiven, fullname[0]); result.set(NCO.nameAdditional, fullname[1]); result.set(NCO.nameFamily, fullname[2]); } } return result; }
FullnameExtractor extends Base { public ResourceModel extract(ResourceModel personName) throws DataMiningException { ResourceModel result = new ResourceModelImpl(personName.getModel(), personName.getIdentifier()); String content = personName.getString(NCO.fullname); if ((content == null) || (content.trim().isEmpty())) { logger.info("PersonName "+personName.getIdentifier()+" doesn't specify the contact's fullname as the NCO.fullname; " + "skipping fullname extraction..."); return result; } Document doc = null; try { doc = Factory.newDocument(content); synchronized(application) { Corpus corpus = Factory.newCorpus("Fullname"+System.currentTimeMillis()); corpus.add(doc); application.setCorpus(corpus); application.execute(); AnnotationSet defaultAnnotSet = doc.getAnnotations(); Set<String> annotTypesRequired = new HashSet<String>(); annotTypesRequired.add("PersonEntities"); AnnotationSet entityAnnSet = defaultAnnotSet.get(new HashSet<String>(annotTypesRequired)); List<Annotation> entityAnnots = gate.Utils.inDocumentOrder(entityAnnSet); for (Annotation annot : entityAnnots) { String entityType = annot.getType(); String firstname = null; String surname = null; String prefix = null; String suffix = null; String gender = null; if (entityType.equals("PersonEntities")) { if ((!(annot.getFeatures().get("prefix").toString().isEmpty())) && ((!(result.has(NCO.nameHonorificPrefix))) || ((result.get(NCO.nameHonorificPrefix)).equals("")))) { prefix = annot.getFeatures().get("prefix").toString(); result.set(NCO.nameHonorificPrefix, prefix); } if ((!(annot.getFeatures().get("firstname").toString().isEmpty())) && ((!(result.has(NCO.nameGiven))) || ((result.get(NCO.nameGiven)).equals("")))) { firstname = annot.getFeatures().get("firstname").toString(); result.set(NCO.nameGiven, firstname); } if ((!(annot.getFeatures().get("surname").toString().isEmpty())) && ((!(result.has(NCO.nameFamily))) || ((result.get(NCO.nameFamily)).equals("")))) { surname = annot.getFeatures().get("surname").toString(); result.set(NCO.nameFamily, surname); } if ((!(annot.getFeatures().get("suffix").toString().isEmpty())) && ((!(result.has(NCO.nameHonorificSuffix))) || ((result.get(NCO.nameHonorificSuffix)).equals("")))) { suffix = annot.getFeatures().get("suffix").toString(); result.set(NCO.nameHonorificSuffix, suffix); } if ((!(annot.getFeatures().get("gender").toString().isEmpty())) && ((!(result.has(NCO.gender))) || ((result.get(NCO.gender)).equals("")))) { gender = annot.getFeatures().get("gender").toString(); result.set(NCO.gender, gender); } } } 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); } } if (((!(result.has(NCO.nameGiven))) || ((result.get(NCO.nameGiven)).equals(""))) && ((!(result.has(NCO.nameAdditional))) || ((result.get(NCO.nameAdditional)).equals(""))) && ((!(result.has(NCO.nameFamily))) || ((result.get(NCO.nameFamily)).equals("")))) { String[] fullname = content.split(" "); if (fullname.length == 1) { result.set(NCO.nameGiven, fullname[0]); } else if (fullname.length == 2) { result.set(NCO.nameGiven, fullname[0]); result.set(NCO.nameFamily, fullname[1]); } else if (fullname.length == 3) { result.set(NCO.nameGiven, fullname[0]); result.set(NCO.nameAdditional, fullname[1]); result.set(NCO.nameFamily, fullname[2]); } } return result; } }
FullnameExtractor extends Base { public ResourceModel extract(ResourceModel personName) throws DataMiningException { ResourceModel result = new ResourceModelImpl(personName.getModel(), personName.getIdentifier()); String content = personName.getString(NCO.fullname); if ((content == null) || (content.trim().isEmpty())) { logger.info("PersonName "+personName.getIdentifier()+" doesn't specify the contact's fullname as the NCO.fullname; " + "skipping fullname extraction..."); return result; } Document doc = null; try { doc = Factory.newDocument(content); synchronized(application) { Corpus corpus = Factory.newCorpus("Fullname"+System.currentTimeMillis()); corpus.add(doc); application.setCorpus(corpus); application.execute(); AnnotationSet defaultAnnotSet = doc.getAnnotations(); Set<String> annotTypesRequired = new HashSet<String>(); annotTypesRequired.add("PersonEntities"); AnnotationSet entityAnnSet = defaultAnnotSet.get(new HashSet<String>(annotTypesRequired)); List<Annotation> entityAnnots = gate.Utils.inDocumentOrder(entityAnnSet); for (Annotation annot : entityAnnots) { String entityType = annot.getType(); String firstname = null; String surname = null; String prefix = null; String suffix = null; String gender = null; if (entityType.equals("PersonEntities")) { if ((!(annot.getFeatures().get("prefix").toString().isEmpty())) && ((!(result.has(NCO.nameHonorificPrefix))) || ((result.get(NCO.nameHonorificPrefix)).equals("")))) { prefix = annot.getFeatures().get("prefix").toString(); result.set(NCO.nameHonorificPrefix, prefix); } if ((!(annot.getFeatures().get("firstname").toString().isEmpty())) && ((!(result.has(NCO.nameGiven))) || ((result.get(NCO.nameGiven)).equals("")))) { firstname = annot.getFeatures().get("firstname").toString(); result.set(NCO.nameGiven, firstname); } if ((!(annot.getFeatures().get("surname").toString().isEmpty())) && ((!(result.has(NCO.nameFamily))) || ((result.get(NCO.nameFamily)).equals("")))) { surname = annot.getFeatures().get("surname").toString(); result.set(NCO.nameFamily, surname); } if ((!(annot.getFeatures().get("suffix").toString().isEmpty())) && ((!(result.has(NCO.nameHonorificSuffix))) || ((result.get(NCO.nameHonorificSuffix)).equals("")))) { suffix = annot.getFeatures().get("suffix").toString(); result.set(NCO.nameHonorificSuffix, suffix); } if ((!(annot.getFeatures().get("gender").toString().isEmpty())) && ((!(result.has(NCO.gender))) || ((result.get(NCO.gender)).equals("")))) { gender = annot.getFeatures().get("gender").toString(); result.set(NCO.gender, gender); } } } 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); } } if (((!(result.has(NCO.nameGiven))) || ((result.get(NCO.nameGiven)).equals(""))) && ((!(result.has(NCO.nameAdditional))) || ((result.get(NCO.nameAdditional)).equals(""))) && ((!(result.has(NCO.nameFamily))) || ((result.get(NCO.nameFamily)).equals("")))) { String[] fullname = content.split(" "); if (fullname.length == 1) { result.set(NCO.nameGiven, fullname[0]); } else if (fullname.length == 2) { result.set(NCO.nameGiven, fullname[0]); result.set(NCO.nameFamily, fullname[1]); } else if (fullname.length == 3) { result.set(NCO.nameGiven, fullname[0]); result.set(NCO.nameAdditional, fullname[1]); result.set(NCO.nameFamily, fullname[2]); } } return result; } FullnameExtractor(); }
FullnameExtractor extends Base { public ResourceModel extract(ResourceModel personName) throws DataMiningException { ResourceModel result = new ResourceModelImpl(personName.getModel(), personName.getIdentifier()); String content = personName.getString(NCO.fullname); if ((content == null) || (content.trim().isEmpty())) { logger.info("PersonName "+personName.getIdentifier()+" doesn't specify the contact's fullname as the NCO.fullname; " + "skipping fullname extraction..."); return result; } Document doc = null; try { doc = Factory.newDocument(content); synchronized(application) { Corpus corpus = Factory.newCorpus("Fullname"+System.currentTimeMillis()); corpus.add(doc); application.setCorpus(corpus); application.execute(); AnnotationSet defaultAnnotSet = doc.getAnnotations(); Set<String> annotTypesRequired = new HashSet<String>(); annotTypesRequired.add("PersonEntities"); AnnotationSet entityAnnSet = defaultAnnotSet.get(new HashSet<String>(annotTypesRequired)); List<Annotation> entityAnnots = gate.Utils.inDocumentOrder(entityAnnSet); for (Annotation annot : entityAnnots) { String entityType = annot.getType(); String firstname = null; String surname = null; String prefix = null; String suffix = null; String gender = null; if (entityType.equals("PersonEntities")) { if ((!(annot.getFeatures().get("prefix").toString().isEmpty())) && ((!(result.has(NCO.nameHonorificPrefix))) || ((result.get(NCO.nameHonorificPrefix)).equals("")))) { prefix = annot.getFeatures().get("prefix").toString(); result.set(NCO.nameHonorificPrefix, prefix); } if ((!(annot.getFeatures().get("firstname").toString().isEmpty())) && ((!(result.has(NCO.nameGiven))) || ((result.get(NCO.nameGiven)).equals("")))) { firstname = annot.getFeatures().get("firstname").toString(); result.set(NCO.nameGiven, firstname); } if ((!(annot.getFeatures().get("surname").toString().isEmpty())) && ((!(result.has(NCO.nameFamily))) || ((result.get(NCO.nameFamily)).equals("")))) { surname = annot.getFeatures().get("surname").toString(); result.set(NCO.nameFamily, surname); } if ((!(annot.getFeatures().get("suffix").toString().isEmpty())) && ((!(result.has(NCO.nameHonorificSuffix))) || ((result.get(NCO.nameHonorificSuffix)).equals("")))) { suffix = annot.getFeatures().get("suffix").toString(); result.set(NCO.nameHonorificSuffix, suffix); } if ((!(annot.getFeatures().get("gender").toString().isEmpty())) && ((!(result.has(NCO.gender))) || ((result.get(NCO.gender)).equals("")))) { gender = annot.getFeatures().get("gender").toString(); result.set(NCO.gender, gender); } } } 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); } } if (((!(result.has(NCO.nameGiven))) || ((result.get(NCO.nameGiven)).equals(""))) && ((!(result.has(NCO.nameAdditional))) || ((result.get(NCO.nameAdditional)).equals(""))) && ((!(result.has(NCO.nameFamily))) || ((result.get(NCO.nameFamily)).equals("")))) { String[] fullname = content.split(" "); if (fullname.length == 1) { result.set(NCO.nameGiven, fullname[0]); } else if (fullname.length == 2) { result.set(NCO.nameGiven, fullname[0]); result.set(NCO.nameFamily, fullname[1]); } else if (fullname.length == 3) { result.set(NCO.nameGiven, fullname[0]); result.set(NCO.nameAdditional, fullname[1]); result.set(NCO.nameFamily, fullname[2]); } } return result; } FullnameExtractor(); ResourceModel extract(ResourceModel personName); }
FullnameExtractor extends Base { public ResourceModel extract(ResourceModel personName) throws DataMiningException { ResourceModel result = new ResourceModelImpl(personName.getModel(), personName.getIdentifier()); String content = personName.getString(NCO.fullname); if ((content == null) || (content.trim().isEmpty())) { logger.info("PersonName "+personName.getIdentifier()+" doesn't specify the contact's fullname as the NCO.fullname; " + "skipping fullname extraction..."); return result; } Document doc = null; try { doc = Factory.newDocument(content); synchronized(application) { Corpus corpus = Factory.newCorpus("Fullname"+System.currentTimeMillis()); corpus.add(doc); application.setCorpus(corpus); application.execute(); AnnotationSet defaultAnnotSet = doc.getAnnotations(); Set<String> annotTypesRequired = new HashSet<String>(); annotTypesRequired.add("PersonEntities"); AnnotationSet entityAnnSet = defaultAnnotSet.get(new HashSet<String>(annotTypesRequired)); List<Annotation> entityAnnots = gate.Utils.inDocumentOrder(entityAnnSet); for (Annotation annot : entityAnnots) { String entityType = annot.getType(); String firstname = null; String surname = null; String prefix = null; String suffix = null; String gender = null; if (entityType.equals("PersonEntities")) { if ((!(annot.getFeatures().get("prefix").toString().isEmpty())) && ((!(result.has(NCO.nameHonorificPrefix))) || ((result.get(NCO.nameHonorificPrefix)).equals("")))) { prefix = annot.getFeatures().get("prefix").toString(); result.set(NCO.nameHonorificPrefix, prefix); } if ((!(annot.getFeatures().get("firstname").toString().isEmpty())) && ((!(result.has(NCO.nameGiven))) || ((result.get(NCO.nameGiven)).equals("")))) { firstname = annot.getFeatures().get("firstname").toString(); result.set(NCO.nameGiven, firstname); } if ((!(annot.getFeatures().get("surname").toString().isEmpty())) && ((!(result.has(NCO.nameFamily))) || ((result.get(NCO.nameFamily)).equals("")))) { surname = annot.getFeatures().get("surname").toString(); result.set(NCO.nameFamily, surname); } if ((!(annot.getFeatures().get("suffix").toString().isEmpty())) && ((!(result.has(NCO.nameHonorificSuffix))) || ((result.get(NCO.nameHonorificSuffix)).equals("")))) { suffix = annot.getFeatures().get("suffix").toString(); result.set(NCO.nameHonorificSuffix, suffix); } if ((!(annot.getFeatures().get("gender").toString().isEmpty())) && ((!(result.has(NCO.gender))) || ((result.get(NCO.gender)).equals("")))) { gender = annot.getFeatures().get("gender").toString(); result.set(NCO.gender, gender); } } } 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); } } if (((!(result.has(NCO.nameGiven))) || ((result.get(NCO.nameGiven)).equals(""))) && ((!(result.has(NCO.nameAdditional))) || ((result.get(NCO.nameAdditional)).equals(""))) && ((!(result.has(NCO.nameFamily))) || ((result.get(NCO.nameFamily)).equals("")))) { String[] fullname = content.split(" "); if (fullname.length == 1) { result.set(NCO.nameGiven, fullname[0]); } else if (fullname.length == 2) { result.set(NCO.nameGiven, fullname[0]); result.set(NCO.nameFamily, fullname[1]); } else if (fullname.length == 3) { result.set(NCO.nameGiven, fullname[0]); result.set(NCO.nameAdditional, fullname[1]); result.set(NCO.nameFamily, fullname[2]); } } return result; } FullnameExtractor(); ResourceModel extract(ResourceModel personName);  }