@Test public void testParseName() throws Exception { VerbatimNameUsage v = new VerbatimNameUsage(); v.setCoreField(DwcTerm.scientificName, "Abies alba Mill., 1982"); assertName(v, Rank.SPECIES, "Abies alba Mill., 1982", "Abies alba", NameType.SCIENTIFIC); v = new VerbatimNameUsage(); v.setCoreField(DwcTerm.scientificName, "Abies alba"); assertName(v, Rank.SPECIES, "Abies alba", "Abies alba", NameType.SCIENTIFIC); v = new VerbatimNameUsage(); v.setCoreField(DwcTerm.scientificName, "? alba"); assertName(v, Rank.SPECIES, "? alba", "? alba", NameType.PLACEHOLDER); v = new VerbatimNameUsage(); v.setCoreField(DwcTerm.genus, "Abies"); v.setCoreField(DwcTerm.specificEpithet, "alba"); assertName(v, Rank.SPECIES, "Abies alba", "Abies alba", NameType.SCIENTIFIC); v = new VerbatimNameUsage(); v.setCoreField(DwcTerm.genus, "Abies"); v.setCoreField(DwcTerm.infraspecificEpithet, "alpina"); assertName(v, Rank.SUBSPECIES, "Abies subsp.", "Abies subsp.", NameType.SCIENTIFIC); v = new VerbatimNameUsage(); v.setCoreField(DwcTerm.genus, "Abies"); v.setCoreField(DwcTerm.specificEpithet, "alba"); v.setCoreField(DwcTerm.infraspecificEpithet, "alpina"); assertName(v, Rank.SUBSPECIES, "Abies alba subsp. alpina", "Abies alba alpina", NameType.SCIENTIFIC); v.setCoreField(DwcTerm.scientificNameAuthorship, "Duméril & Bibron"); assertName(v, Rank.SUBSPECIES, "Abies alba subsp. alpina Duméril & Bibron", "Abies alba alpina", NameType.SCIENTIFIC); v.setCoreField(DwcTerm.namePublishedInYear, "1937"); assertName(v, Rank.SUBSPECIES, "Abies alba subsp. alpina Duméril & Bibron, 1937", "Abies alba alpina", NameType.SCIENTIFIC); v.setCoreField(DwcTerm.scientificNameAuthorship, "Duméril & Bibron 1937"); assertName(v, Rank.SUBSPECIES, "Abies alba subsp. alpina Duméril & Bibron, 1937", "Abies alba alpina", NameType.SCIENTIFIC); }
@VisibleForTesting protected ParsedName parseName(NameUsage u, VerbatimNameUsage v, Rank rank) throws IgnoreNameUsageException { ParsedName pn = new ParsedName(); final String sciname = clean(v.getCoreField(DwcTerm.scientificName)); try { if (sciname != null) { pn = nameParser.parse(sciname, rank); } else { String genus = firstClean(v, GbifTerm.genericName, DwcTerm.genus); if (genus == null) { throw new IgnoreNameUsageException("No name found"); } else { pn.setGenusOrAbove(genus); pn.setSpecificEpithet(v.getCoreField(DwcTerm.specificEpithet)); pn.setInfraSpecificEpithet(v.getCoreField(DwcTerm.infraspecificEpithet)); pn.setRank(rank); pn.setType(NameType.SCIENTIFIC); u.addIssue(NameUsageIssue.SCIENTIFIC_NAME_ASSEMBLED); } } String vAuthorship = buildAuthorship(v); if (!Strings.isNullOrEmpty(vAuthorship) && !pn.hasAuthorship()) { ParsedName auth = nameParser.parseQuietly("Abies alba " + vAuthorship, Rank.SPECIES); if (auth.isParsed() && auth.hasAuthorship()) { pn.setAuthorship(auth.getAuthorship()); pn.setYear(auth.getYear()); pn.setBracketAuthorship(auth.getBracketAuthorship()); pn.setBracketYear(auth.getBracketYear()); u.addIssue(NameUsageIssue.SCIENTIFIC_NAME_ASSEMBLED); } } if (u.getIssues().contains(NameUsageIssue.SCIENTIFIC_NAME_ASSEMBLED)) { pn.setScientificName(pn.fullName()); } if (pn.isParsedPartially()) { u.addIssue(NameUsageIssue.PARTIALLY_PARSABLE); } } catch (UnparsableException e) { if (e.type.isParsable()) { } LOG.debug("Unparsable {} name {}", e.type, e.name); pn = new ParsedName(); pn.setType(e.type); pn.setScientificName(sciname); pn.setParsed(false); if (e.type.isParsable()) { u.addIssue(NameUsageIssue.UNPARSABLE); } } u.setScientificName(pn.getScientificName()); u.setCanonicalName(Strings.emptyToNull(pn.canonicalName())); u.setNameType(pn.getType()); return pn; }
NeoInserter implements AutoCloseable { @VisibleForTesting protected ParsedName parseName(NameUsage u, VerbatimNameUsage v, Rank rank) throws IgnoreNameUsageException { ParsedName pn = new ParsedName(); final String sciname = clean(v.getCoreField(DwcTerm.scientificName)); try { if (sciname != null) { pn = nameParser.parse(sciname, rank); } else { String genus = firstClean(v, GbifTerm.genericName, DwcTerm.genus); if (genus == null) { throw new IgnoreNameUsageException("No name found"); } else { pn.setGenusOrAbove(genus); pn.setSpecificEpithet(v.getCoreField(DwcTerm.specificEpithet)); pn.setInfraSpecificEpithet(v.getCoreField(DwcTerm.infraspecificEpithet)); pn.setRank(rank); pn.setType(NameType.SCIENTIFIC); u.addIssue(NameUsageIssue.SCIENTIFIC_NAME_ASSEMBLED); } } String vAuthorship = buildAuthorship(v); if (!Strings.isNullOrEmpty(vAuthorship) && !pn.hasAuthorship()) { ParsedName auth = nameParser.parseQuietly("Abies alba " + vAuthorship, Rank.SPECIES); if (auth.isParsed() && auth.hasAuthorship()) { pn.setAuthorship(auth.getAuthorship()); pn.setYear(auth.getYear()); pn.setBracketAuthorship(auth.getBracketAuthorship()); pn.setBracketYear(auth.getBracketYear()); u.addIssue(NameUsageIssue.SCIENTIFIC_NAME_ASSEMBLED); } } if (u.getIssues().contains(NameUsageIssue.SCIENTIFIC_NAME_ASSEMBLED)) { pn.setScientificName(pn.fullName()); } if (pn.isParsedPartially()) { u.addIssue(NameUsageIssue.PARTIALLY_PARSABLE); } } catch (UnparsableException e) { if (e.type.isParsable()) { } LOG.debug("Unparsable {} name {}", e.type, e.name); pn = new ParsedName(); pn.setType(e.type); pn.setScientificName(sciname); pn.setParsed(false); if (e.type.isParsable()) { u.addIssue(NameUsageIssue.UNPARSABLE); } } u.setScientificName(pn.getScientificName()); u.setCanonicalName(Strings.emptyToNull(pn.canonicalName())); u.setNameType(pn.getType()); return pn; } }
NeoInserter implements AutoCloseable { @VisibleForTesting protected ParsedName parseName(NameUsage u, VerbatimNameUsage v, Rank rank) throws IgnoreNameUsageException { ParsedName pn = new ParsedName(); final String sciname = clean(v.getCoreField(DwcTerm.scientificName)); try { if (sciname != null) { pn = nameParser.parse(sciname, rank); } else { String genus = firstClean(v, GbifTerm.genericName, DwcTerm.genus); if (genus == null) { throw new IgnoreNameUsageException("No name found"); } else { pn.setGenusOrAbove(genus); pn.setSpecificEpithet(v.getCoreField(DwcTerm.specificEpithet)); pn.setInfraSpecificEpithet(v.getCoreField(DwcTerm.infraspecificEpithet)); pn.setRank(rank); pn.setType(NameType.SCIENTIFIC); u.addIssue(NameUsageIssue.SCIENTIFIC_NAME_ASSEMBLED); } } String vAuthorship = buildAuthorship(v); if (!Strings.isNullOrEmpty(vAuthorship) && !pn.hasAuthorship()) { ParsedName auth = nameParser.parseQuietly("Abies alba " + vAuthorship, Rank.SPECIES); if (auth.isParsed() && auth.hasAuthorship()) { pn.setAuthorship(auth.getAuthorship()); pn.setYear(auth.getYear()); pn.setBracketAuthorship(auth.getBracketAuthorship()); pn.setBracketYear(auth.getBracketYear()); u.addIssue(NameUsageIssue.SCIENTIFIC_NAME_ASSEMBLED); } } if (u.getIssues().contains(NameUsageIssue.SCIENTIFIC_NAME_ASSEMBLED)) { pn.setScientificName(pn.fullName()); } if (pn.isParsedPartially()) { u.addIssue(NameUsageIssue.PARTIALLY_PARSABLE); } } catch (UnparsableException e) { if (e.type.isParsable()) { } LOG.debug("Unparsable {} name {}", e.type, e.name); pn = new ParsedName(); pn.setType(e.type); pn.setScientificName(sciname); pn.setParsed(false); if (e.type.isParsable()) { u.addIssue(NameUsageIssue.UNPARSABLE); } } u.setScientificName(pn.getScientificName()); u.setCanonicalName(Strings.emptyToNull(pn.canonicalName())); u.setNameType(pn.getType()); return pn; } private  NeoInserter(UsageDao dao, File storeDir, int batchSize, @Nullable Meter insertMeter); }
NeoInserter implements AutoCloseable { @VisibleForTesting protected ParsedName parseName(NameUsage u, VerbatimNameUsage v, Rank rank) throws IgnoreNameUsageException { ParsedName pn = new ParsedName(); final String sciname = clean(v.getCoreField(DwcTerm.scientificName)); try { if (sciname != null) { pn = nameParser.parse(sciname, rank); } else { String genus = firstClean(v, GbifTerm.genericName, DwcTerm.genus); if (genus == null) { throw new IgnoreNameUsageException("No name found"); } else { pn.setGenusOrAbove(genus); pn.setSpecificEpithet(v.getCoreField(DwcTerm.specificEpithet)); pn.setInfraSpecificEpithet(v.getCoreField(DwcTerm.infraspecificEpithet)); pn.setRank(rank); pn.setType(NameType.SCIENTIFIC); u.addIssue(NameUsageIssue.SCIENTIFIC_NAME_ASSEMBLED); } } String vAuthorship = buildAuthorship(v); if (!Strings.isNullOrEmpty(vAuthorship) && !pn.hasAuthorship()) { ParsedName auth = nameParser.parseQuietly("Abies alba " + vAuthorship, Rank.SPECIES); if (auth.isParsed() && auth.hasAuthorship()) { pn.setAuthorship(auth.getAuthorship()); pn.setYear(auth.getYear()); pn.setBracketAuthorship(auth.getBracketAuthorship()); pn.setBracketYear(auth.getBracketYear()); u.addIssue(NameUsageIssue.SCIENTIFIC_NAME_ASSEMBLED); } } if (u.getIssues().contains(NameUsageIssue.SCIENTIFIC_NAME_ASSEMBLED)) { pn.setScientificName(pn.fullName()); } if (pn.isParsedPartially()) { u.addIssue(NameUsageIssue.PARTIALLY_PARSABLE); } } catch (UnparsableException e) { if (e.type.isParsable()) { } LOG.debug("Unparsable {} name {}", e.type, e.name); pn = new ParsedName(); pn.setType(e.type); pn.setScientificName(sciname); pn.setParsed(false); if (e.type.isParsable()) { u.addIssue(NameUsageIssue.UNPARSABLE); } } u.setScientificName(pn.getScientificName()); u.setCanonicalName(Strings.emptyToNull(pn.canonicalName())); u.setNameType(pn.getType()); return pn; } private  NeoInserter(UsageDao dao, File storeDir, int batchSize, @Nullable Meter insertMeter); static NeoInserter create(UsageDao dao, File storeDir, int batchSize, @Nullable MetricRegistry registry); InsertMetadata insert(File dwca, Map<String, UUID> constituents); static String clean(String x); @Override void close(); }
NeoInserter implements AutoCloseable { @VisibleForTesting protected ParsedName parseName(NameUsage u, VerbatimNameUsage v, Rank rank) throws IgnoreNameUsageException { ParsedName pn = new ParsedName(); final String sciname = clean(v.getCoreField(DwcTerm.scientificName)); try { if (sciname != null) { pn = nameParser.parse(sciname, rank); } else { String genus = firstClean(v, GbifTerm.genericName, DwcTerm.genus); if (genus == null) { throw new IgnoreNameUsageException("No name found"); } else { pn.setGenusOrAbove(genus); pn.setSpecificEpithet(v.getCoreField(DwcTerm.specificEpithet)); pn.setInfraSpecificEpithet(v.getCoreField(DwcTerm.infraspecificEpithet)); pn.setRank(rank); pn.setType(NameType.SCIENTIFIC); u.addIssue(NameUsageIssue.SCIENTIFIC_NAME_ASSEMBLED); } } String vAuthorship = buildAuthorship(v); if (!Strings.isNullOrEmpty(vAuthorship) && !pn.hasAuthorship()) { ParsedName auth = nameParser.parseQuietly("Abies alba " + vAuthorship, Rank.SPECIES); if (auth.isParsed() && auth.hasAuthorship()) { pn.setAuthorship(auth.getAuthorship()); pn.setYear(auth.getYear()); pn.setBracketAuthorship(auth.getBracketAuthorship()); pn.setBracketYear(auth.getBracketYear()); u.addIssue(NameUsageIssue.SCIENTIFIC_NAME_ASSEMBLED); } } if (u.getIssues().contains(NameUsageIssue.SCIENTIFIC_NAME_ASSEMBLED)) { pn.setScientificName(pn.fullName()); } if (pn.isParsedPartially()) { u.addIssue(NameUsageIssue.PARTIALLY_PARSABLE); } } catch (UnparsableException e) { if (e.type.isParsable()) { } LOG.debug("Unparsable {} name {}", e.type, e.name); pn = new ParsedName(); pn.setType(e.type); pn.setScientificName(sciname); pn.setParsed(false); if (e.type.isParsable()) { u.addIssue(NameUsageIssue.UNPARSABLE); } } u.setScientificName(pn.getScientificName()); u.setCanonicalName(Strings.emptyToNull(pn.canonicalName())); u.setNameType(pn.getType()); return pn; } private  NeoInserter(UsageDao dao, File storeDir, int batchSize, @Nullable Meter insertMeter); static NeoInserter create(UsageDao dao, File storeDir, int batchSize, @Nullable MetricRegistry registry); InsertMetadata insert(File dwca, Map<String, UUID> constituents); static String clean(String x); @Override void close();  }