diff --git a/bindings/java/pom.xml b/bindings/java/pom.xml index 782b750..debcca7 100644 --- a/bindings/java/pom.xml +++ b/bindings/java/pom.xml @@ -11,7 +11,7 @@ VERSIONING: this binding shares the Rust ENGINE's version — the Cargo [workspace.package] version at this repo's root (package gbif-name-parser) — released in lockstep with the CLI/Python/R bindings, so one version across all bindings means one engine. It is NOT the - version of the api it implements: `org.gbif:name-parser-api` (6.0.1, a dependency + version of the api it implements: `org.gbif:name-parser-api` (6.0.2-SNAPSHOT, a dependency below) is independently versioned and pinned. The bindings sit at 0.x while new / gathering real-use feedback, and graduate to the product's 5.x line once stable. Keep below in sync with the Cargo workspace version — scripts/bump-version.sh does this; see RELEASE.md. @@ -92,7 +92,7 @@ org.gbif name-parser-api - 6.0.1 + 6.0.2-SNAPSHOT diff --git a/crates/nameparser/src/format.rs b/crates/nameparser/src/format.rs index 1da580d..fadda9c 100644 --- a/crates/nameparser/src/format.rs +++ b/crates/nameparser/src/format.rs @@ -54,10 +54,10 @@ static AL: LazyLock = LazyLock::new(|| Regex::new(r"^al\.?$").unwrap()); /// 01_E13`), which rendered "Wolbachia sp. endosymbiont of …". And with the new-species markers /// `n. sp.` and `nsp` (#139), the unnamed genus of `Carabidae gen. sp.` (#174), and the aggregate /// marker of `Acalitus agg. brevitarsus/phyllereus`, which rendered "Acalitus agg. agg. …", and a -/// marker glued to its number, `Dynestinae sp2` (#242). +/// marker glued to its number, `Dynestinae sp2` (#242), and the dotless `n sp` (#246). static PHRASE_SPECIES_MARKER: LazyLock = LazyLock::new(|| { Regex::new( - r"(?i)^(?:(?:spp?|spec)\d|(?:species|spec|spp|sp|nsp|agg|n\.(?-u:\s*)sp|cf|aff|nr|near|[a-z]*(?:bacteri(?:um|a)|archae(?:on|a|ote)|symbionts?))(?-u:\b)|(?:n_)?sp(?:ec)?_|n_gen(?-u:\b)|gen(?:\.|(?-u:\s))).*$", + r"(?i)^(?:(?:spp?|spec)\d|(?:species|spec|spp|sp|nsp|agg|n\.?(?-u:\s*)sp\d*|cf|aff|nr|near|[a-z]*(?:bacteri(?:um|a)|archae(?:on|a|ote)|symbionts?))(?-u:\b)|(?:n_)?sp(?:ec)?_|n_gen(?-u:\b)|gen(?:\.|(?-u:\s))).*$", ) .unwrap() }); @@ -400,10 +400,7 @@ fn append_genus( show_qualifier: bool, html: bool, ) { - if show_qualifier && n.has_epithet_qualifier(NamePart::Generic) { - sb.push_str(qualifier(n, NamePart::Generic)); - sb.push(' '); - } + append_qualifier(sb, n, NamePart::Generic, show_qualifier); if hybrid_marker && n.graft_chimaera { sb.push(GRAFT_MARKER); sb.push(' '); @@ -427,10 +424,7 @@ fn append_infraspecific( html: bool, ) { sb.push(' '); - if show_qualifier && n.has_epithet_qualifier(NamePart::Infraspecific) { - sb.push_str(qualifier(n, NamePart::Infraspecific)); - sb.push(' '); - } + append_qualifier(sb, n, NamePart::Infraspecific, show_qualifier); if hybrid_marker && has_notho(n, NamePart::Infraspecific) { if rank_marker && is_infraspecific_marker(n.rank) { sb.push_str("notho"); @@ -462,22 +456,20 @@ fn append_infraspecific( } } -/// A qualifier of `part` and a space, if shown and there is one: the `?` of a doubted subgenus, -/// `Glenea (? Macroglenea) anulicornis`. +/// A qualifier of `part` and a space, if shown and there is one: `Abies cf. alba`. The `?` of a +/// doubted part is left out, the doubtful flag and the qualifier itself carry it (#243): +/// `Metacnephia ? sommermanae` is `Metacnephia sommermanae`, `Glenea (? Macroglenea) anulicornis` +/// is `Glenea (Macroglenea) anulicornis`. fn append_qualifier(sb: &mut String, n: &ParsedName, part: NamePart, show_qualifier: bool) { - if show_qualifier && n.has_epithet_qualifier(part) { - sb.push_str(qualifier(n, part)); - sb.push(' '); - } -} - -/// Fetch an epithet qualifier known to exist (caller guards with `has_epithet_qualifier`). -fn qualifier(n: &ParsedName, part: NamePart) -> &str { - n.epithet_qualifier + let shown = n + .epithet_qualifier .as_ref() .and_then(|m| m.get(&part)) - .map(String::as_str) - .unwrap_or("") + .filter(|q| show_qualifier && q.as_str() != "?"); + if let Some(q) = shown { + sb.push_str(q); + sb.push(' '); + } } /// Java `NameFormatter.joinAuthors(List, Integer maxAuthors)`. @@ -873,10 +865,7 @@ fn build_name(n: &ParsedName, f: &Flags) -> Option { } else { // species part sb.push(' '); - if f.show_qualifier && n.has_epithet_qualifier(NamePart::Specific) { - sb.push_str(qualifier(n, NamePart::Specific)); - sb.push(' '); - } + append_qualifier(&mut sb, n, NamePart::Specific, f.show_qualifier); if f.hybrid_marker && has_notho(n, NamePart::Specific) { sb.push(HYBRID_MARKER); sb.push(' '); diff --git a/crates/nameparser/src/model/enums.rs b/crates/nameparser/src/model/enums.rs index 362b3b1..e2f8f79 100644 --- a/crates/nameparser/src/model/enums.rs +++ b/crates/nameparser/src/model/enums.rs @@ -19,6 +19,11 @@ pub enum NameType { /// the catch-all `Other`. Placed before `Other` so the catch-all stays last; this makes it /// FFI-wire ordinal 4 and shifts `Other` to 5 (a lockstep change gated by the ABI-version bump). Identifier, + /// Any other unparsable name: no name could be read from it. With the code of the + /// `ParseResult::Unparsable` it is all the parser says about why (#248): a virus carries + /// [`NomCode::Virus`] (`HIV-1 06TG.HT064`); a code or label rather than a name (`Lepidostoma1`, + /// `ANME-1 cluster archaeon`), alternatives in different genera or without one (`Anser/Branta`, + /// `obsoletus/scoticus`), a vector or plasmid, numbers, abbreviations or free text carry none. Other, } @@ -1162,6 +1167,14 @@ pub mod warnings { /// Rust-only so far: a species epithet written with a capital, as Linnaeus did (`Ovis Aries`), /// was put in lower case (ICZN Art. 28). pub const CAPITALISED_EPITHET: &str = "capitalised epithet put in lower case"; + /// No genus or epithet has a single letter (#246). + pub const SINGLE_LETTER: &str = "single letter genus or epithet"; + /// A genus or uninomial holding a digit or a character no scientific name uses (#244): + /// `Imsh8ia aleurites`. + pub const UNUSUAL_GENUS: &str = "genus or uninomial with digit or unusual character"; + /// An infrageneric, specific or infraspecific epithet holding a digit or a character no + /// scientific name uses (#244, #236): `Imshia aleu3ites`, `Ephedra przewalskiiß`. + pub const UNUSUAL_EPITHET: &str = "epithet with digit or unusual character"; } #[cfg(test)] diff --git a/crates/nameparser/src/model/name.rs b/crates/nameparser/src/model/name.rs index 0acd869..8a9a84b 100644 --- a/crates/nameparser/src/model/name.rs +++ b/crates/nameparser/src/model/name.rs @@ -242,6 +242,18 @@ pub struct ParsedName { static PUBLISHED_IN_YEAR: LazyLock = LazyLock::new(|| Regex::new(r"(?-u:\b(1[5-9]\d{2}|20\d{2}|2100)\b)").unwrap()); +/// A word with the suffix GTDB gives a taxon it splits, an underscore and one to three capitals +/// (`Pseudomonas_E`, `Bacillus_BF`, `mitis_I`), as the taxon and the suffix. Not a lower-case label +/// or one with digits (`madecassus_01m`, `Stygiellidae_STYG2`). +pub(crate) fn split_gtdb_suffix(word: &str) -> Option<(&str, &str)> { + let (taxon, suffix) = word.rsplit_once('_')?; + (taxon.chars().next().is_some_and(char::is_alphabetic) + && !taxon.contains('_') + && (1..=3).contains(&suffix.len()) + && suffix.chars().all(|c| c.is_ascii_uppercase())) + .then_some((taxon, suffix)) +} + impl ParsedName { /// Add a warning if not already present — mirrors Java's `warnings` HashSet (deduping). /// (The golden harness sorts warnings before diffing, so order is irrelevant; dedup is what matters.) @@ -404,6 +416,8 @@ impl ParsedName { /// The anchor keeps the name parts down to the deepest one that is present and not qualified /// (cf., aff., ?, ...). A qualified genus is the exception: `?Sydonia alba`, `Ferganoconcha? /// oblonga` and `cf. Craticula` are anchored on that genus, as no part of the name is surer. + /// The suffix GTDB gives a split taxon is dropped: `Pseudomonas_E putida` is anchored on + /// *Pseudomonas putida*, `Bacillus_BF` on *Bacillus* (see [`split_gtdb_suffix`]). /// The anchor takes the rank of its deepest part and the authorship that belongs to it: the /// specific authorship for a species anchor of an infraspecific name, the generic one for a /// genus, the name's own when nothing below the anchor is given. The phrase, qualifiers, notes, @@ -465,6 +479,7 @@ impl ParsedName { if deepest == NamePart::Generic { own_authorship(&mut a); } + a.drop_gtdb_suffixes(); return Some(a); } // an infrageneric name without its genus, "subgen. Trematostoma" @@ -573,9 +588,26 @@ impl ParsedName { } else if kept == deepest { own_authorship(&mut a); } + a.drop_gtdb_suffixes(); Some(a) } + /// The anchor of a GTDB name is the taxon GTDB split it from: `Pseudomonas_E putida` hangs off + /// `Pseudomonas putida`, `Streptococcus mitis_I` off `Streptococcus mitis`, `Bacillus_BF` off + /// `Bacillus`. + fn drop_gtdb_suffixes(&mut self) { + for part in [ + &mut self.uninomial, + &mut self.genus, + &mut self.specific_epithet, + &mut self.infraspecific_epithet, + ] { + if let Some((taxon, _)) = part.as_deref().and_then(split_gtdb_suffix) { + *part = Some(taxon.to_string()); + } + } + } + fn keep_notho(&self, anchor: &mut ParsedName, part: NamePart) { if self.notho.as_ref().is_some_and(|n| n.contains(&part)) { anchor.add_notho(part); diff --git a/crates/nameparser/src/pipeline/assemble.rs b/crates/nameparser/src/pipeline/assemble.rs index a6ec7db..4f8d7d5 100644 --- a/crates/nameparser/src/pipeline/assemble.rs +++ b/crates/nameparser/src/pipeline/assemble.rs @@ -40,6 +40,7 @@ use std::sync::LazyLock; use regex::Regex; +use crate::model::name::split_gtdb_suffix; use crate::model::{rank_utils, warnings, Authorship, NameType, NomCode, ParsedName, Rank, State}; use crate::pipeline::authorship_parser::AuthState; use crate::pipeline::blacklisted_epithets; @@ -163,13 +164,11 @@ pub(crate) fn finish(ctx: &mut ParseContext, auth_state: Option<&AuthState>) { } // Step 7: a monomial with an underscore is either "Genus_species" (underscore as space: - // genus + specific epithet, when the after-part starts lowercase) or a GTDB-style phrase - // name (e.g. "Desulfobacterota_B": uninomial + phrase, when the after-part starts - // uppercase). Under the bacterial code a GTDB suffix of capitals stays on the uninomial, - // though: `Bacillus_BF` is one of several genera GTDB splits Bacillus into, not Bacillus with - // a designation, and the suffixed genus of a binomial (`Acholeplasma_D palmae`) is kept whole - // too (#102). Without it the suffix may as well be a BOLD placeholder code - // (`Blattellinae_SB`). + // genus + specific epithet, when the after-part starts lowercase), a GTDB taxon or a + // designation of the taxon before it (uninomial + phrase, `Entodinium_1`). A GTDB suffix of + // capitals stays on the uninomial, as it does on the genus or epithet of a binomial + // (`Acholeplasma_D palmae`, #102): `Bacillus_BF` is one of several genera GTDB splits + // Bacillus into, not Bacillus with a designation. Such a name is informal, see step 19. if ctx.name.type_ == NameType::Scientific { if let Some(idx) = ctx.name.uninomial.as_ref().and_then(|u| u.find('_')) { let uni = ctx.name.uninomial.clone().expect("just matched Some above"); @@ -181,13 +180,10 @@ pub(crate) fn finish(ctx: &mut ParseContext, auth_state: Option<&AuthState>) { ctx.name.genus = Some(before); ctx.name.specific_epithet = Some(after); ctx.name.rank = Rank::Species; - } else if ctx.requested_code == Some(NomCode::Bacterial) - && after.chars().count() <= 4 - && after.chars().all(|c| c.is_ascii_uppercase()) - { + } else if split_gtdb_suffix(&uni).is_some() { // "Bacillus_BF" → one GTDB genus } else { - // "Desulfobacterota_B" → GTDB-style phrase name + // "Entodinium_1" → a designation of the taxon before it ctx.name.uninomial = Some(before); ctx.name.phrase = Some(after); ctx.name.type_ = NameType::Informal; @@ -418,8 +414,7 @@ pub(crate) fn finish(ctx: &mut ParseContext, auth_state: Option<&AuthState>) { // Step 18: a phrase epithet without a cultivar always denotes an INFORMAL name → promote // a monomial uninomial to a genus so callers see "Baeckea ssp. " as a phrase name // on a genus, not a uninomial scientific name. Skip promotion for suprageneric phrase - // forms (e.g. GTDB "Desulfobacterota_B") where the uninomial really is at family/order - // level. + // forms (e.g. "Eusiridae NZD") where the uninomial really is at family/order level. if ctx.name.phrase.as_ref().is_some_and(|p| !p.is_empty()) && ctx.name.cultivar_epithet.is_none() { @@ -430,6 +425,21 @@ pub(crate) fn finish(ctx: &mut ParseContext, auth_state: Option<&AuthState>) { } ctx.name.type_ = NameType::Informal; } + + // Step 19: a taxon GTDB split off is no regular scientific name, but an informal one anchored + // on the taxon it was split from: `Pseudomonas_E putida`, `Streptococcus mitis_I`, + // `Bacillus_BF`. Whatever the code: the same suffixes are used elsewhere in the tree. + let gtdb = [ + &ctx.name.uninomial, + &ctx.name.genus, + &ctx.name.specific_epithet, + &ctx.name.infraspecific_epithet, + ] + .into_iter() + .any(|part| part.as_deref().and_then(split_gtdb_suffix).is_some()); + if gtdb { + ctx.name.type_ = NameType::Informal; + } } /// The INDETERMINED warning flags exactly the names [`ParsedName::is_indetermined`] holds for: the @@ -713,18 +723,25 @@ mod tests { } #[test] - fn underscore_split_uppercase_after_becomes_gtdb_phrase_name() { + fn underscore_split_keeps_a_gtdb_suffix_on_an_informal_uninomial() { let mut ctx = base_ctx(); ctx.name.uninomial = Some("Desulfobacterota_B".to_string()); finish(&mut ctx, None); - assert_eq!(ctx.name.uninomial, Some("Desulfobacterota".to_string())); - assert_eq!(ctx.name.phrase, Some("B".to_string())); + assert_eq!(ctx.name.uninomial, Some("Desulfobacterota_B".to_string())); + assert_eq!(ctx.name.phrase, None); assert_eq!(ctx.name.type_, NameType::Informal); assert_eq!(ctx.name.rank, Rank::Unranked); - assert_eq!( - ctx.name.genus, None, - "step 18 must not promote a suprageneric GTDB phrase name to genus" - ); + assert_eq!(ctx.name.genus, None); + } + + #[test] + fn underscore_split_makes_a_designation_the_phrase() { + let mut ctx = base_ctx(); + ctx.name.uninomial = Some("Entodinium_1".to_string()); + finish(&mut ctx, None); + assert_eq!(ctx.name.uninomial, Some("Entodinium".to_string())); + assert_eq!(ctx.name.phrase, Some("1".to_string())); + assert_eq!(ctx.name.type_, NameType::Informal); } #[test] diff --git a/crates/nameparser/src/pipeline/authorship_split.rs b/crates/nameparser/src/pipeline/authorship_split.rs index 828386b..d26c9a9 100644 --- a/crates/nameparser/src/pipeline/authorship_split.rs +++ b/crates/nameparser/src/pipeline/authorship_split.rs @@ -269,14 +269,16 @@ pub fn find_boundary(tokens: &[Token], ctx: &ParseContext) -> usize { } // "n. sp." before any epithet is the species marker "sp." of a new species // ("Jardinella n. sp. keel", #139): walk on to the "sp" + let dot = usize::from(tokens.get(i + 1).is_some_and(|d| d.kind == TokenKind::Dot)); if t.text == "n" && !have_epithet - && tokens.get(i + 1).is_some_and(|d| d.kind == TokenKind::Dot) - && tokens.get(i + 2).is_some_and(|m| { - m.kind == TokenKind::Word && m.text.eq_ignore_ascii_case("sp") + && tokens.get(i + 1 + dot).is_some_and(|m| { + m.kind == TokenKind::Word + && (m.text.eq_ignore_ascii_case("sp") + || crate::pipeline::name_tokens::is_glued_indet_marker(&m.text)) }) { - i += 2; + i += 1 + dot; continue; } // cf./aff. qualifiers and indet markers — keep walking diff --git a/crates/nameparser/src/pipeline/mod.rs b/crates/nameparser/src/pipeline/mod.rs index b1fd642..e5831e6 100644 --- a/crates/nameparser/src/pipeline/mod.rs +++ b/crates/nameparser/src/pipeline/mod.rs @@ -23,6 +23,7 @@ use std::sync::LazyLock; use regex::Regex; +use crate::model::name::split_gtdb_suffix; use crate::model::{ warnings, Authorship, CombinedAuthorship, NamePart, NameType, NomCode, ParseError, ParsedName, Rank, State, @@ -30,6 +31,8 @@ use crate::model::{ use crate::pipeline::authorship_parser::AuthState; use crate::token::tokenize; use crate::unicode::{has_mojibake, is_fullwidth, java_trim, normalize_input, normalize_quotes}; +use unicode_normalization::char::is_combining_mark; +use unicode_normalization::UnicodeNormalization; /// Java `Pipeline.MAX_LENGTH`. Hard upper bound on the input length. Beyond this the /// input is rejected as unparsable rather than parsed: real scientific names — even with @@ -51,8 +54,8 @@ const LONG_NAME_LENGTH: usize = 250; /// regardless of that flag. /// /// Pattern: Latin-style prefix glued to an all-caps / alphanumeric phrase suffix -/// ("OdontellidaeGEN", "GenusANIC_3"). Underscored prefixes ("Blattellinae_SB") are -/// handled later in Assemble; this doesn't match those. +/// ("OdontellidaeGEN", "GenusANIC_3"). Underscored prefixes ("Entodinium_1", +/// "Blattellinae_SB") are handled later in Assemble; this doesn't match those. static GLUED_PHRASE: LazyLock = LazyLock::new(|| Regex::new(r"^([\p{Lu}][\p{Ll}]+)([\p{Lu}]{2,}[\p{Lu}\d_]*)$").unwrap()); @@ -537,6 +540,8 @@ pub fn run( apply_placeholder_tail(&mut ctx); assemble::flag_indetermined(&mut ctx.name); flag_numbered_authors(&mut ctx.name); + flag_single_letters(&mut ctx.name); + flag_unusual_characters(&mut ctx.name); apply_pending_qualifier(&mut ctx); Ok(ctx.name) @@ -559,6 +564,58 @@ fn apply_pending_qualifier(ctx: &mut ParseContext) { ctx.name.type_ = NameType::Informal; } +/// A genus or an epithet of a single letter is no name of a taxon but a designation or a broken +/// record (#246): `M mus`, `Polypedates leucomystax b`, `Galium spurium var. L.` An abbreviated +/// genus keeps its dot and its own warning: `M. mus`. +fn flag_single_letters(name: &mut ParsedName) { + // not the `?` standing for a missing genus (`? alba Smith`) + let single = |part: &Option| { + part.as_deref() + .is_some_and(|w| w.chars().count() == 1 && w != "?") + }; + if single(&name.genus) + || single(&name.uninomial) + || single(&name.specific_epithet) + || single(&name.infraspecific_epithet) + { + name.add_warning(warnings::SINGLE_LETTER); + } +} + +/// A genus or an epithet with a digit or a character no scientific name uses (#244, #236): `Imsh8ia +/// aleurites`, `Imshia aleu3ites`, `Ephedra przewalskiiß`, `Coccinella 6maculata`, `var. β`. Latin +/// letters may carry a diaeresis or another accent (`Cephaëlis`, `Pardøsa`), the ligatures æ and œ +/// stand for two of them, a hyphen joins words (`Pseudo-nitzschia`, `novae-angliae`), and an +/// apostrophe or a dot is an old spelling (`o'donelli`, `st.-johnii`) or the dot of an abbreviated +/// genus (`M. mus`). A virus species may hold a number (`Simplexvirus humanalpha1`), and a genus or +/// epithet the capital suffix GTDB gives a split taxon (`Pseudomonas_E putida`, `Streptococcus +/// mitis_I`, `Bacillus_BF`). The `?` standing for a missing genus is no odd character, and the +/// cultivar epithet, which may hold anything, is not looked at. +fn flag_unusual_characters(name: &mut ParsedName) { + let virus = name.code == Some(NomCode::Virus); + let usual = |c: char| { + c.is_ascii_alphabetic() + || matches!(c, '-' | '\'' | '.' | 'æ' | 'Æ' | 'œ' | 'Œ' | 'ø' | 'Ø') + || is_combining_mark(c) + || virus && c.is_ascii_digit() + }; + let unusual = |part: &Option| { + part.as_deref().is_some_and(|w| { + let w = split_gtdb_suffix(w).map_or(w, |(taxon, _)| taxon); + w != "?" && !w.nfd().all(usual) + }) + }; + if unusual(&name.genus) || unusual(&name.uninomial) { + name.add_warning(warnings::UNUSUAL_GENUS); + } + if unusual(&name.infrageneric_epithet) + || unusual(&name.specific_epithet) + || unusual(&name.infraspecific_epithet) + { + name.add_warning(warnings::UNUSUAL_EPITHET); + } +} + /// An author with a digit in it is no person's name: `Aus bus Smith2, 1900` (#213). fn flag_numbered_authors(name: &mut ParsedName) { let numbered = [&name.combination_authorship, &name.basionym_authorship] diff --git a/crates/nameparser/src/pipeline/name_tokens.rs b/crates/nameparser/src/pipeline/name_tokens.rs index 2395a99..331c8aa 100644 --- a/crates/nameparser/src/pipeline/name_tokens.rs +++ b/crates/nameparser/src/pipeline/name_tokens.rs @@ -95,7 +95,7 @@ pub(crate) fn is_aggregate_marker(w: &str) -> bool { /// A species marker glued to its number, the designation of an unnamed species: `Dynestinae sp2`, /// `Mus spec2` (#242). It became the epithet `sp2`. The phrase keeps it as written. -fn is_glued_indet_marker(w: &str) -> bool { +pub(crate) fn is_glued_indet_marker(w: &str) -> bool { let letters = w.trim_end_matches(|c: char| c.is_ascii_digit()); letters.len() < w.len() && matches!(letters, "sp" | "spp" | "spec") } @@ -137,6 +137,9 @@ pub(crate) fn classify(ctx: &mut ParseContext, boundary: usize) { let mut indet = false; // a lone-letter species designation (`Collettea a`), see step 8 below let mut letter_designation = false; + // the last epithet read is a lone letter with no dot after it: a designation in the + // infraspecific slot too (`Polypedates leucomystax b`), not an abbreviation (`rugosum r.`, #246) + let mut last_epithet_letter = false; // A bare supraspecific indet ("Genus sp." with no distinguishing tail) — stays flagged // INDETERMINED even though its phrase now carries the verbatim marker (see the top-check // in the indet branch and the warning block near the end of this fn). @@ -514,16 +517,18 @@ pub(crate) fn classify(ctx: &mut ParseContext, boundary: usize) { continue; } // "n. sp." before any epithet: the marker of a new species, opening its phrase - // ("Jardinella n. sp. keel", #139) + // ("Jardinella n. sp. keel", #139), also without the dot or with its number glued + // ("Forcipata n sp. ancantha", "Emblyna n. sp1", #246) + let dot = usize::from(ts.get(i + 1).is_some_and(|d| d.kind == TokenKind::Dot)); if t.text == "n" && lower_epithets.is_empty() - && ts.get(i + 1).is_some_and(|d| d.kind == TokenKind::Dot) - && ts.get(i + 2).is_some_and(|m| { - m.kind == TokenKind::Word && m.text.eq_ignore_ascii_case("sp") + && ts.get(i + 1 + dot).is_some_and(|m| { + m.kind == TokenKind::Word + && (m.text.eq_ignore_ascii_case("sp") || is_glued_indet_marker(&m.text)) }) { nova_start = Some(t.start); - i += 2; + i += 1 + dot; continue; } // 2. indet markers — sp./spec./indet @@ -612,14 +617,15 @@ pub(crate) fn classify(ctx: &mut ParseContext, boundary: usize) { if i < ts.len() && ts[i].kind == TokenKind::Dot { i += 1; } - // #252: several words or a qualified one before the marker are no species to - // hang the unnamed one off, so it hangs off the genus and they open its phrase: - // `Prionospio iberian canyons sp. c`, `Prionospio aff. iberian sp. c`. One - // word is its species: `Abies alba sp. c` keeps `sp. c`, as `sp. 1` does. + // #252: several words before the marker are no species to hang the unnamed one + // off, so it hangs off the genus and they open its phrase: `Prionospio iberian + // canyons sp. c`. One word is its species, `Abies alba sp. c` keeps `sp. c` as + // `sp. 1` does, and so does a qualified one, which the qualifier only doubts: + // `Euxoa nr. idahoensis sp. 1clay`, `Ramazzottius aff. szeptycki sp. n. 1`. if (sp_designation || number_follows_marker) && !lower_epithets.is_empty() && marker_idx_in_epithets < 0 - && (lower_epithets.len() >= 2 || cf_aff_qualifier.is_some()) + && lower_epithets.len() >= 2 { let start = ts[..marker_idx] .iter() @@ -1076,6 +1082,12 @@ pub(crate) fn classify(ctx: &mut ParseContext, boundary: usize) { || (nx.kind == TokenKind::Word && starts_lower(nx)) }); } + last_epithet_letter = t.text.len() == 1 + && t.text.as_bytes()[0].is_ascii_lowercase() + && !ctx + .tokens + .get(i + 1) + .is_some_and(|nx| nx.kind == TokenKind::Dot); lower_epithets.push(t.text.clone()); i += 1; continue; @@ -1230,6 +1242,14 @@ pub(crate) fn classify(ctx: &mut ParseContext, boundary: usize) { if letter_designation && specific.is_some() { ctx.name.type_ = NameType::Informal; } + // a lone letter in the infraspecific slot is a designation too: `Polypedates leucomystax b` + // (#246), as after a rank marker (`Galium spurium var. L.`); with a dot it is an abbreviation + if specific.is_some() + && last_epithet_letter + && infraspecific.as_deref() == lower_epithets.last().map(String::as_str) + { + ctx.name.type_ = NameType::Informal; + } if indet { ctx.name.type_ = NameType::Informal; if infraspecific.is_none() { diff --git a/crates/nameparser/src/pipeline/preflight.rs b/crates/nameparser/src/pipeline/preflight.rs index 3d2ae2a..912e840 100644 --- a/crates/nameparser/src/pipeline/preflight.rs +++ b/crates/nameparser/src/pipeline/preflight.rs @@ -7,7 +7,7 @@ use std::sync::LazyLock; use regex::Regex; -use crate::model::{NameType, NomCode, ParseError, Rank, State}; +use crate::model::{NamePart, NameType, NomCode, ParseError, Rank, State}; use crate::pipeline::ParseContext; use crate::unicode::java_trim; use crate::viral::is_viral; @@ -150,6 +150,15 @@ pub(super) fn canonical_identifier(code: &str) -> String { } } +/// A first word of two or more capitals and digits, perhaps hyphenated, and more words after it: +/// see its use in [`run`]. +static CLADE_CODE_HEAD: LazyLock = + LazyLock::new(|| Regex::new(r"^[A-Z]{2,}[-_]?[0-9][A-Za-z0-9-]*(?-u:\s+)\S").unwrap()); + +/// The human and simian immunodeficiency and T-lymphotropic viruses, by their acronym and number. +static RETROVIRUS_HEAD: LazyLock = + LazyLock::new(|| Regex::new(r"^(?:HIV|SIV|HTLV)[-_]?[0-9]").unwrap()); + /// Java: `Pattern.CASE_INSENSITIVE`. Has `\d` (in a class) → scoped. static OTU_GTDB_UBA: LazyLock = LazyLock::new(|| Regex::new(r"(?i)^(?:UBA|GTDB|GCA|GCF)(?-u:[\d_-]+)$").unwrap()); @@ -204,11 +213,28 @@ const ALTERNATIVE_GENUS: &str = r"\p{Lu}\p{Ll}+(?:-\p{Lu}?\p{Ll}+)?"; /// `Aphaenogaster cf. rudis/texana` (#225), or with an infraspecific rank marker, `Sylvia cantillans /// subsp. inornata/iberiae`. The groups: genus, subgenus, the epithets, the rank marker, the /// alternatives. Read by [`read_alternatives`]. The marker is lazy, so that a qualifier stays the -/// epithet's: `Aus bus cf. cus/dus`. +/// epithet's: `Aus bus cf. cus/dus`. Before a slash or pipe an authorship may follow the epithets, +/// each alternative carrying its own (#235): `Chionodoxa forbesii BAKER / luciliae BOISS.`, +/// `Carduelis carduelis (Linnaeus, 1758) / Serinus canaria (Linnaeus, 1758)`. Alternative authors +/// stay authors, a capitalised word alone being no alternative: `Aus bus Smith/Jones`. After an +/// authorship the slash stands apart on one side at least, so that a word holding one is no +/// alternative: `Carnevali & M. Jua|arez`, `Prostanthera lasianthos Girraween/Polblue variant`. static ALTERNATIVE_EPITHETS: LazyLock = LazyLock::new(|| { let epithet = r"(?:(?:cf|aff|nr)\.?(?-u:\s+))?\p{Ll}[\p{Ll}\-]+"; Regex::new(&format!( - r"^({ALTERNATIVE_GENUS})(?:(?-u:\s+)\((\p{{Lu}}\p{{Ll}}+)\))?(?-u:\s+)({epithet}(?:(?-u:\s+)(?:(\p{{Ll}}+)\.?(?-u:\s+))??{epithet})?)(?:(?-u:\s*)[/|](?-u:\s*)|(?-u:\s+)or(?-u:\s+))(.+)$" + r"^({ALTERNATIVE_GENUS})(?:(?-u:\s+)\((\p{{Lu}}\p{{Ll}}+)\))?(?-u:\s+)({epithet}(?:(?-u:\s+)(?:(\p{{Ll}}+)\.?(?-u:\s+))??{epithet})?)(?:(?-u:\s+)(?P{ALTERNATIVE_AUTHORSHIP})(?:(?-u:\s+)[/|](?-u:\s*)|[/|](?-u:\s+))|(?-u:\s*)[/|](?-u:\s*)|(?-u:\s+)or(?-u:\s+))(?P.+)$" + )) + .unwrap() +}); +/// The authorship of one of several alternatives (#235): capitalised names, years, `&`, `et`, `ex`, +/// `in` and brackets, no lower-case word or code, so that a strain (`indica PD1998/pus`), a page +/// (`pl. 222 / figs 1 – 7`) or a host (`ex Ficus popenoei/nymphaefolia`) is no authorship. +const ALTERNATIVE_AUTHORSHIP: &str = r"\(?\p{Lu}[\p{L}.'\-]*\)?(?:,?(?-u:\s+)(?:\(?\p{Lu}[\p{L}.'\-]*\)?|\(?\d{4}\)?|&|et|ex|in))*,?"; +/// An alternative after one that carries an authorship: an epithet or a species, its genus written +/// out or abbreviated, perhaps with its own authorship, and nothing else. +static ALTERNATIVE_WITH_AUTHORSHIP: LazyLock = LazyLock::new(|| { + Regex::new(&format!( + r"^(?:{ALTERNATIVE_GENUS}(?-u:\s+)|\p{{Lu}}\.(?-u:\s*))?\p{{Ll}}[\p{{Ll}}\-]+(?:(?-u:\s+){ALTERNATIVE_AUTHORSHIP})?(?-u:\s*)$" )) .unwrap() }); @@ -274,6 +300,8 @@ enum Alternatives<'a> { genus: &'a str, subgenus: Option<&'a str>, species: Option<&'a str>, + /// The qualifier of that species: `Agabus cf. lutosus griseus/griseipennis`. + qualifier: Option<&'a str>, rank: Option, phrase: &'a str, }, @@ -305,7 +333,15 @@ fn read_alternatives(s: &str) -> Option> { } None => None, }; - let alternatives = &caps[5]; + let alternatives = &caps["alts"]; + // after an authorship every alternative is a whole one, with its own authorship or none + if caps.name("auth").is_some() + && !ALTERNATIVE_SEPARATOR + .split(alternatives) + .all(|alternative| ALTERNATIVE_WITH_AUTHORSHIP.is_match(alternative)) + { + return None; + } if let Some(species) = ALTERNATIVE_SPECIES.captures(alternatives) { if AUTHOR_LINK.contains(&&species[2]) { return None; @@ -324,14 +360,16 @@ fn read_alternatives(s: &str) -> Option> { return None; } let epithets = caps.get(3)?; - let species = shared_species(genus, epithets.as_str(), alternatives); - let phrase = &s[epithets.start()..]; + let shared = shared_species(genus, epithets.as_str(), alternatives); + // the phrase starts below the species, at the epithets if there is none + let below = shared.map_or(0, |(_, _, below)| epithets.len() - below.len()); Some(Alternatives::OneTaxon { genus, subgenus: caps.get(2).map(|m| m.as_str()), - species, - rank: rank.filter(|_| species.is_some()), - phrase: species.map_or(phrase, |species| phrase[species.len()..].trim_start()), + species: shared.map(|(_, species, _)| species), + qualifier: shared.and_then(|(qualifier, _, _)| qualifier), + rank: rank.filter(|_| shared.is_some()), + phrase: &s[epithets.start() + below..], }) } @@ -347,12 +385,24 @@ fn names_genus(written: &str, genus: &str) -> bool { /// The species all alternatives lie in, so that it is sure while what is below it is not: `Bubo /// virginianus pallescens / Bubo virginianus pinorum intergrade`, `Tachybaptus ruficollis /// tricolor/vulcanorum` (#212). `epithets` needs a word below the species, since a lone epithet after -/// a binomial is another species. A qualifier in front of the species doubts it. -fn shared_species<'a>(genus: &str, epithets: &'a str, alternatives: &str) -> Option<&'a str> { - let (species, _) = epithets.split_once(char::is_whitespace)?; - if matches!(species.trim_end_matches('.'), "cf" | "aff" | "nr") - || NO_ALTERNATIVE_EPITHET.contains(&species) - { +/// a binomial is another species. A qualifier in front of the species doubts it, and the species +/// keeps it: `Agabus cf. lutosus griseus/griseipennis`. Returns the qualifier without its full stop, +/// the species and the epithets below it. +fn shared_species<'a>( + genus: &str, + epithets: &'a str, + alternatives: &str, +) -> Option<(Option<&'a str>, &'a str, &'a str)> { + let (first, rest) = epithets.split_once(char::is_whitespace)?; + let (qualifier, (species, below)) = match first.trim_end_matches('.') { + qualifier @ ("cf" | "aff" | "nr") => ( + Some(qualifier), + rest.trim_start().split_once(char::is_whitespace)?, + ), + _ => (None, (first, rest)), + }; + let below = below.trim_start(); + if NO_ALTERNATIVE_EPITHET.contains(&species) { return None; } ALTERNATIVE_SEPARATOR @@ -363,7 +413,7 @@ fn shared_species<'a>(genus: &str, epithets: &'a str, alternatives: &str) -> Opt None => ALTERNATIVE_EPITHET.is_match(alternative), }, ) - .then_some(species) + .then_some((qualifier, species, below)) } /// A capitalised word, an underscore and a label opening with a capital or a digit, in a word with @@ -427,6 +477,11 @@ static GEN_NOV: LazyLock = LazyLock::new(|| { static OTU_SPECIMEN_SUFFIX: LazyLock = LazyLock::new(|| Regex::new(r"(?i)(?-u:\bsp\d{8,})$").unwrap()); +/// The unnamed species GTDB places in a genus, the genus with or without its GTDB suffix and `sp` +/// with the number of a genome assembly: `Pseudomonas_E sp002874965`. +static GTDB_UNNAMED_SPECIES: LazyLock = + LazyLock::new(|| Regex::new(r"^\p{Lu}\p{Ll}+(?:_[A-Z]{1,3})?(?-u:\s+)sp\d{8,}$").unwrap()); + // ---------- PLACEHOLDER ---------- /// Java: `Pattern.CASE_INSENSITIVE`. Has `\s`/`\b` (many), no `\p{…}`, no wildcard `.` (only /// escaped `\.`) → whole alternation `(?-u:…)`-wrapped. `incertae sedis` needs no word boundary @@ -1026,9 +1081,19 @@ pub fn run(original: &str, ctx: &mut ParseContext) -> Result<(), ParseError> { if GEN_NOV.is_match(&s) // Dead-but-faithful (same shape as the PR2_LIKE note above): the `@`-check in the delete-marker gate above (Preflight.java:212) already returns for any `s.starts_with('@')`, so this second one (Preflight.java:278) can never fire — ported verbatim anyway to stay faithful. || s.starts_with('@') + // Rust-only: a synonym reference alone, `=ixioides?` (#241), names nothing + || s.starts_with('=') { return Err(ParseError::new(NameType::Other, None, s)); } + // Rust-only: a code of capitals and digits in the genus's place is the label of a clade, no + // genus: `ANME-1 cluster archaeon GoMg3.2`, `SAR324 cluster bacterium`, `NC10 bacterium`, + // `UBA4372 sp`. It was title-cased into a genus `Anme-1`. The retroviruses' strains are + // viruses: `HIV-1 06TG.HT064`, `SIV-1 …`, `HTLV-1 …`. + if CLADE_CODE_HEAD.is_match(&s) { + let code = RETROVIRUS_HEAD.is_match(&s).then_some(NomCode::Virus); + return Err(ParseError::new(NameType::Other, code, s)); + } if let Some(caps) = DASH_NUMBER_LABEL.captures(&s) { rescue_label(ctx, &caps[1], &caps[2]); return Ok(()); @@ -1060,10 +1125,15 @@ pub fn run(original: &str, ctx: &mut ParseContext) -> Result<(), ParseError> { genus, subgenus, species, + qualifier, rank, phrase, }) => { rescue_informal_group(ctx, genus, phrase); + if let Some(qualifier) = qualifier { + ctx.name + .set_epithet_qualifier(NamePart::Specific, &format!("{qualifier}.")); + } if let Some(rank) = rank { ctx.name.rank = rank; // the code a marker tells: `Bacillus thuringiensis serovar aizawai/pacificus` @@ -1084,8 +1154,10 @@ pub fn run(original: &str, ctx: &mut ParseContext) -> Result<(), ParseError> { if PR2_LIKE.is_match(&s) && s.contains('-') && has_digit(&s) { return Err(ParseError::new(NameType::Other, None, s)); } - // GTDB/SILVA specimen codes ending with "sp" + 8+ digits (e.g. "18JY21-1 sp004344915"). - if s.contains(' ') && OTU_SPECIMEN_SUFFIX.is_match(&s) { + // GTDB/SILVA specimen codes ending with "sp" + 8+ digits (e.g. "18JY21-1 sp004344915"). After + // a genus, its GTDB suffix or not, it is the unnamed species GTDB placed there, an informal + // name: `Pseudomonas_E sp002874965`, `Acidaminococcus sp000437815`. + if s.contains(' ') && OTU_SPECIMEN_SUFFIX.is_match(&s) && !GTDB_UNNAMED_SPECIES.is_match(&s) { return Err(ParseError::new(NameType::Other, None, s)); } diff --git a/crates/nameparser/src/pipeline/stripandstash.rs b/crates/nameparser/src/pipeline/stripandstash.rs index 3d43de5..129c21a 100644 --- a/crates/nameparser/src/pipeline/stripandstash.rs +++ b/crates/nameparser/src/pipeline/stripandstash.rs @@ -64,6 +64,7 @@ pub(crate) fn run(ctx: &mut ParseContext) { s = strip_leading_genus_qualifier(ctx, s); s = strip_bracketed_genus_doubt(ctx, s); s = strip_shouted_vernacular(ctx, s); + s = stash_family_designation(ctx, s); s = stash_genus_designation(ctx, s); s = shorten_species_near(s); s = strip_rank_lineage(ctx, s); @@ -86,6 +87,7 @@ pub(crate) fn run(ctx: &mut ParseContext) { s = normalise_minor_shapes(s); s = repair_question_mark_in_word(ctx, s); s = stash_misplaced_qualifier(ctx, s); + s = stash_bracketed_rank_note(ctx, s); s = split_glued_year(ctx, s); s = strip_strain_designation(ctx, s); s = stash_trailing_rank_marker_code(ctx, s); @@ -126,8 +128,10 @@ pub(crate) fn run(ctx: &mut ParseContext) { s = normalise_cultivar_spelling(ctx, s); s = strip_cultivar_group_grex(ctx, s); s = lower_shouted_qualifier(s); + s = normalise_spelt_out_markers(s); s = strip_rank_nova(ctx, s); s = strip_quoted_attributed_author(s); + s = unquote_manuscript_epithet(s); s = strip_quoted_cultivar(ctx, s); s = strip_bare_cultivar_marker(ctx, s); s = strip_extinct_dagger(ctx, s); @@ -612,6 +616,30 @@ static GENUS_DESIGNATION: LazyLock = LazyLock::new(|| { Regex::new(r"^(\p{Lu}\p{Ll}+)(?-u:\s+)gen(?:\.(?-u:\s*)|(?-u:\s+))(\S.*?)(?-u:\s*)$").unwrap() }); +/// A taxon followed by `fam.` and the designation of an unnamed family in it, a letter, a code or an +/// unnamed genus: `Digenea fam. B sp. MAT.CG.2`, `Mesostigmata fam. MRY1 sp.`, `Tetraphyllidea fam. +/// gen. sp. scpl02`. Group 1 is the taxon, group 2 the designation. A family's name after `fam.` +/// and the placeholders (`fam. indet.`, `fam. Incertae sedis`, `fam. n.`) are not. +static FAMILY_DESIGNATION: LazyLock = LazyLock::new(|| { + Regex::new(r"^(\p{Lu}\p{Ll}+)(?-u:\s+)fam\.?(?-u:\s+)((?:\p{Lu}|\p{Lu}+\d[\p{L}\d]*|gen\.)(?:(?-u:\s+).*)?)$") + .unwrap() +}); + +/// See [`FAMILY_DESIGNATION`]. As for [`GENUS_DESIGNATION`], the designation, marker included, is +/// the phrase of an informal name on the taxon, of rank SPECIES when it designates a species too, +/// else FAMILY. Read as is, `fam` became the epithet and the rest its author. +fn stash_family_designation(ctx: &mut ParseContext, s: String) -> String { + let Some(caps) = FAMILY_DESIGNATION.captures(&s) else { + return s; + }; + let species = GENUS_DESIGNATION_SPECIES.is_match(&caps[2]); + let phrase_start = caps.get(1).unwrap().end(); + ctx.name.phrase = Some(java_trim(&s[phrase_start..]).to_string()); + ctx.name.rank = if species { Rank::Species } else { Rank::Family }; + ctx.name.type_ = NameType::Informal; + caps[1].to_string() +} + /// A uninomial labelled with its rank, `Ojrotella gen.`. Group 1 is the uninomial. static BARE_GENUS_MARKER: LazyLock = LazyLock::new(|| Regex::new(r"^(\p{Lu}\p{Ll}+)(?-u:\s+)gen\.?(?-u:\s*)$").unwrap()); @@ -1161,15 +1189,35 @@ fn strip_strain_designation(ctx: &mut ParseContext, s: String) -> String { /// as any other (`Candidatus Caldatribacterium saccharofermentans OP9-77CS`), as does a code of /// capitals joined by an underscore (`PW_SP`), which no author has (#84). A slash may stand in the /// code and a genus in square brackets before it: `Agrobacterium pusense L2/2/1`, `[Eubacterium] -/// rectale M104/1` (#213). +/// rectale M104/1` (#213). Full stops and underscores may stand inside a code too, `Anabaena bergii +/// PMC215.03`, `Escherichia coli STEC_H.I.8`, `Botrylloides cf. lenis SMRC_3.1`, and a word labelling +/// it before it, `Acaulospora colossica spore IB.15` (see [`CODE_LABEL`]): split at the full stop, +/// the code became an author and its last digits were left over. static TRAILING_STRAIN_CODE: LazyLock = LazyLock::new(|| { Regex::new( - &r"^((?:Candidatus\s+)?\[?[\p{Lu}][\p{Ll}]+\]?\s+(?:(?:cf|aff|nr|near)\.?\s+)?[\p{Ll}]+)\s+([dr]?RNA[a-zA-Z0-9_\-]*|[\p{Lu}][\p{L}\d:/\-]*\d[\p{L}\d_\-:/]*|[\p{Lu}][\p{Lu}\d]*_[\p{Lu}\d_]*[\p{Lu}\d]|\d(?:[\p{L}\d_:/.\-]*[\p{L}\d])?)\.?\s*$" - .replace("", INFRASPECIFIC_HEAD), + &r"^((?:Candidatus\s+)?\[?[\p{Lu}][\p{Ll}]+\]?\s+(?:(?:cf|aff|nr|near)\.?\s+)?[\p{Ll}]+)\s+((?: