Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 21 additions & 11 deletions crates/nameparser/src/model/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,9 @@ impl ParsedName {
/// *Grevillea brachystylis* and `Abies cf. alba` on the genus *Abies*.
///
/// The anchor keeps the name parts down to the deepest one that is present and not qualified
/// (cf., aff., ?, ...), at the rank of that part and with the authorship that belongs to it: the
/// (cf., aff., ?, ...). A genus doubted with `?` is the exception: `?Sydonia alba` and
/// `Ferganoconcha? oblonga` are anchored on that genus, as no part of the name is surer. 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,
/// publication, warnings and flags other than `candidatus` and `extinct` are left out. A complete
Expand All @@ -410,6 +412,12 @@ impl ParsedName {
.as_ref()
.is_some_and(|q| q.contains_key(&p))
};
// only doubted, the genus anchors the name all the same: `?Sydonia alba`
let doubted_genus = self
.epithet_qualifier
.as_ref()
.and_then(|q| q.get(&NamePart::Generic))
.is_some_and(|q| q == "?");
let mut a = ParsedName {
type_: NameType::Scientific,
code: self.code,
Expand All @@ -434,7 +442,7 @@ impl ParsedName {

let Some(genus) = self.genus.clone() else {
if let Some(uninomial) = self.uninomial.clone() {
if qualified(NamePart::Generic) {
if qualified(NamePart::Generic) && !doubted_genus {
return None;
}
a.uninomial = Some(uninomial);
Expand Down Expand Up @@ -470,18 +478,20 @@ impl ParsedName {
return Some(a);
};

if qualified(NamePart::Generic) {
if qualified(NamePart::Generic) && !doubted_genus {
return None;
}
let mut kept = NamePart::Generic;
if self.infrageneric_epithet.is_some() && !qualified(NamePart::Infrageneric) {
kept = NamePart::Infrageneric;
}
let cut = self.infrageneric_epithet.is_some() && kept == NamePart::Generic;
if !cut && self.specific_epithet.is_some() && !qualified(NamePart::Specific) {
kept = NamePart::Specific;
if self.infraspecific_epithet.is_some() && !qualified(NamePart::Infraspecific) {
kept = NamePart::Infraspecific;
if !doubted_genus {
if self.infrageneric_epithet.is_some() && !qualified(NamePart::Infrageneric) {
kept = NamePart::Infrageneric;
}
let cut = self.infrageneric_epithet.is_some() && kept == NamePart::Generic;
if !cut && self.specific_epithet.is_some() && !qualified(NamePart::Specific) {
kept = NamePart::Specific;
if self.infraspecific_epithet.is_some() && !qualified(NamePart::Infraspecific) {
kept = NamePart::Infraspecific;
}
}
}
// nothing cut and nothing missing: the name is its own anchor, at its own rank
Expand Down
4 changes: 4 additions & 0 deletions crates/nameparser/src/pipeline/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,9 @@ pub(crate) struct ParseContext {
/// (`?Sydonia alba`, `cf. Platypeltis croftii`);
/// `Pipeline::run` types the name INFORMAL once it knows a species epithet follows.
pub qualified_genus: bool,
/// Set by StripAndStash when it removed a free-standing trailing `?` (`Abies alba ?`);
/// `Pipeline::run` gives it to the last part of a name without authorship.
pub trailing_qmark: bool,
/// An "(Approved Lists 1980)" citation was stripped: the name is a prokaryote's (see
/// `code_inference::infer`).
pub approved_lists: bool,
Expand Down Expand Up @@ -180,6 +183,7 @@ impl ParseContext {
pending_serovar: None,
preflight_complete: false,
qualified_genus: false,
trailing_qmark: false,
approved_lists: false,
bracketed_infrageneric_marker: false,
written_out_infrageneric_marker: false,
Expand Down
66 changes: 64 additions & 2 deletions crates/nameparser/src/pipeline/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ use std::sync::LazyLock;
use regex::Regex;

use crate::model::{
warnings, Authorship, CombinedAuthorship, NameType, NomCode, ParseError, ParsedName, Rank,
State,
warnings, Authorship, CombinedAuthorship, NamePart, NameType, NomCode, ParseError, ParsedName,
Rank, State,
};
use crate::pipeline::authorship_parser::AuthState;
use crate::token::tokenize;
Expand Down Expand Up @@ -508,11 +508,73 @@ pub fn run(
}
}
restore_lost_letters(&mut ctx);
qualify_trailing_doubt(&mut ctx);
keep_unplaced_doubt(&mut ctx);
assemble::flag_indetermined(&mut ctx.name);

Ok(ctx.name)
}

/// A word ending in a "?" after a letter or a dot (`pentagona?`, `sensu?,`), as long as no "?"
/// stands between its letters: a lost letter, `Petrovi?-Obradovi?` (see [`restore_lost_letters`]).
static WORD_DOUBT_QMARK: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"[\p{L}.]\?[,;)\]]*$").unwrap());
static LOST_LETTER_QMARK: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"\p{L}\?[\p{L}\-]").unwrap());

/// A "?" glued to the end of a word that no step placed — as a qualifier or an uncertain author —
/// still flags the name doubtful, where the shape around it was not understood: `Argiope Argiope
/// pentagona?`, `Colpidium cucullus Kent, sensu?, 1881`. Java took every such "?" for an uncertain
/// author.
fn keep_unplaced_doubt(ctx: &mut ParseContext) {
if ctx.name.doubtful {
return;
}
let doubt = [Some(ctx.original.as_str()), ctx.authorship_input.as_deref()]
.into_iter()
.flatten()
.flat_map(str::split_whitespace)
.any(|w| WORD_DOUBT_QMARK.is_match(w) && !LOST_LETTER_QMARK.is_match(w));
if doubt {
ctx.name.doubtful = true;
ctx.name.add_warning(warnings::QUESTION_MARKS_REMOVED);
}
}

/// A free-standing "?" after a bare name (`Abies alba ?`, `Faunus (Pseudobellardia) ?`) doubts its
/// last part, as a glued one does (`Abies alba?`). Rust-only: Java dropped it, and only the
/// doubtful flag told of it. After an authorship, a note or a phrase it stays no more than that
/// flag, and a part already qualified keeps its qualifier (`Machairodus cf. crenatidens ?`).
fn qualify_trailing_doubt(ctx: &mut ParseContext) {
let n = &mut ctx.name;
if !ctx.trailing_qmark
|| !n.type_.is_parsable()
|| n.epithet_qualifier.is_some()
|| n.phrase.is_some()
|| n.cultivar_epithet.is_some()
|| n.has_authorship()
|| n.nomenclatural_note.is_some()
|| n.taxonomic_note.is_some()
|| n.unparsed.is_some()
{
return;
}
let part = if n.infraspecific_epithet.is_some() {
NamePart::Infraspecific
} else if n.specific_epithet.is_some() {
NamePart::Specific
} else if n.infrageneric_epithet.is_some() {
NamePart::Infrageneric
} else {
NamePart::Generic
};
n.set_epithet_qualifier(part, "?");
// a doubted uninomial keeps its type, as `?Monotremata` does
if part != NamePart::Generic {
n.type_ = NameType::Informal;
}
}

/// A note that a spelling is a misspelling, a lapsus or an error, perhaps of other authors:
/// `auct. misspelling for 'immatura'`, `lapsus`, `auct. err.`, `Filograna auct. misspelling`,
/// `species name widely misspelled as diagramma`.
Expand Down
44 changes: 38 additions & 6 deletions crates/nameparser/src/pipeline/name_tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,11 +268,40 @@ pub(crate) fn classify(ctx: &mut ParseContext, boundary: usize) {
i += 1;
continue;
}
// Open-nomenclature doubtful-identification marker: "?" between epithets, like
// "Ferganoconcha? oblonga" or "Buteo borealis ? ventralis". Treat exactly like
// cf./aff.: skip the token, attach the qualifier to the next epithet (specific when
// no species yet, infraspecific when one exists), set type=INFORMAL, doubtful, and
// emit QUESTION_MARKS_REMOVED.
// A "?" glued to the end of a word doubts that word: the genus of "Ferganoconcha?
// oblonga" (as "?Ferganoconcha oblonga"), the species of "Buteo borealis? ventralis",
// the epithet of "Abies alba var. nana?". Rust-only: Java gave it to the next epithet,
// and to a species missing from "Abies?".
if genus.is_some() && t.kind == TokenKind::Other && t.text == "?" && i > 0 {
let prev = &ts[i - 1];
let glued = prev.kind == TokenKind::Word && prev.end == t.start;
let doubts_genus = glued
&& lower_epithets.is_empty()
&& genus
.as_deref()
.is_some_and(|g| g.eq_ignore_ascii_case(&prev.text));
let doubts_epithet = glued && lower_epithets.last() == Some(&prev.text.to_lowercase());
if doubts_genus {
ctx.name.set_epithet_qualifier(NamePart::Generic, "?");
ctx.qualified_genus = true;
} else if doubts_epithet && cf_aff_qualifier.is_none() {
// a cf. already there stays: "Afiacyathus cf. sibiricus?"
qualifier_at = lower_epithets.len() - 1;
cf_aff_qualifier = Some("?".to_string());
ctx.name.type_ = NameType::Informal;
}
if doubts_genus || doubts_epithet {
ctx.name.doubtful = true;
ctx.name.add_warning(warnings::QUESTION_MARKS_REMOVED);
i += 1;
continue;
}
}
// Open-nomenclature doubtful-identification marker: a "?" standing free or glued to the
// next epithet, "Buteo borealis ? ventralis", "Abies ?alba". Treat exactly like cf./aff.:
// skip the token, attach the qualifier to the next epithet (specific when no species
// yet, infraspecific when one exists), set type=INFORMAL, doubtful, and emit
// QUESTION_MARKS_REMOVED.
if genus.is_some()
&& cf_aff_qualifier.is_none()
&& t.kind == TokenKind::Other
Expand Down Expand Up @@ -1104,8 +1133,11 @@ pub(crate) fn classify(ctx: &mut ParseContext, boundary: usize) {
}
if let Some(q) = cf_aff_qualifier.as_ref().filter(|_| !qualifier_phrase) {
// Qualifier applies to the epithet after it, or the last one when it trails them all.
// With no epithet at all it can only doubt the genus.
let followed = lower_epithets.len() > qualifier_at;
let part = if infraspecific.is_none() || (qualifier_at == 0 && followed) {
let part = if specific.is_none() {
NamePart::Generic
} else if infraspecific.is_none() || (qualifier_at == 0 && followed) {
NamePart::Specific
} else {
NamePart::Infraspecific
Expand Down
64 changes: 56 additions & 8 deletions crates/nameparser/src/pipeline/stripandstash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,8 +258,18 @@ static INED_QMARK: LazyLock<Regex> =
/// Rust-only, also before a year when the word is capitalised: `Barovski? 1926`, `Rondot? 1887?`,
/// which Java dropped silently (#80). A lower-case word before the year is an epithet, not an
/// author.
///
/// Rust-only, the word must look like an author, as before the year: capitalised, after a space,
/// bracket or slash, maybe abbreviated (`Sess?`, `DC.?`, `d'Orb.?`, `Roque et al./Savoie?`), and
/// may close a square bracket (`Schaufuss, C. [or L.W.?]`). Java took any letter, so the doubted
/// epithet of `Abies alba?` and the genus of `Abies?` were uncertain authors, and missed the dot of
/// `Mikania attenuata DC.?`, whose `?` was dropped without a trace. A separate authorship comes
/// here with a space put in front ([`on_authorship`]).
static UNCERTAIN_AUTHOR_QMARK: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r"\p{L}\?\s*(?:$|[&,])|(?:^|[\s(])\p{Lu}[\p{L}'.\-]*\?\s+\d{4}").unwrap()
Regex::new(
r"[\s(/](?:\p{Ll}{1,3}')?\p{Lu}[\p{L}'.\-]*\?\s*(?:$|[&,\]])|(?:^|[\s(])\p{Lu}[\p{L}'.\-]*\?\s+\d{4}",
)
.unwrap()
});

/// Java UNCERTAIN_AUTHOR_OR (StripAndStash.java:304-305): `\p{Lu}\p{L}*\s+or\s+\p{Lu}`,
Expand All @@ -284,6 +294,7 @@ static UNCERTAIN_AUTHOR_SLASH: LazyLock<Regex> =
/// transcription artefacts ("Istv?nffi") are left to `repair_question_mark_in_word` (step 7).
fn flag_uncertain_authorship(ctx: &mut ParseContext, mut s: String) -> String {
if TRAILING_QMARK.is_match(&s) {
ctx.trailing_qmark = true;
ctx.name.doubtful = true;
ctx.name.add_warning(warnings::QUESTION_MARKS_REMOVED);
s = java_trim(&TRAILING_QMARK.replace_all(&s, "")).to_string();
Expand Down Expand Up @@ -537,11 +548,11 @@ static BRACKETED_GENUS_DOUBT: LazyLock<Regex> = LazyLock::new(|| {
.unwrap()
});

/// A question mark inside a subgenus's brackets, before or after it: `Glenea (? Macroglenea)
/// anulicornis`, `Glenea (Macroglenea?) anulicornis`. Group 1 is the genus, 2 or 3 the subgenus,
/// 4 the epithet and what follows.
/// A question mark inside a subgenus's brackets, before or after it, or glued after them: `Glenea
/// (? Macroglenea) anulicornis`, `Glenea (Macroglenea?) anulicornis`, `Glenea (Macroglenea)?
/// anulicornis`. Group 1 is the genus, 2, 3 or 4 the subgenus, 5 the epithet and what follows.
static BRACKETED_SUBGENUS_DOUBT: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r"^(\p{Lu}\p{Ll}+)(?-u:\s*)\((?:(?-u:\s*)\?(?-u:\s*)(\p{Lu}\p{Ll}+)|(?-u:\s*)(\p{Lu}\p{Ll}+)(?-u:\s*)\?)(?-u:\s*)\)(?-u:\s*)(\p{Ll}[\p{Ll}\-]+(?:(?-u:\s).*)?)$")
Regex::new(r"^(\p{Lu}\p{Ll}+)(?-u:\s*)\((?:(?-u:\s*)\?(?-u:\s*)(\p{Lu}\p{Ll}+)(?-u:\s*)\)|(?-u:\s*)(\p{Lu}\p{Ll}+)(?-u:\s*)\?(?-u:\s*)\)|(?-u:\s*)(\p{Lu}\p{Ll}+)(?-u:\s*)\)\?)(?-u:\s*)(\p{Ll}[\p{Ll}\-]+\??(?:(?-u:\s).*)?)$")
.unwrap()
});

Expand All @@ -557,11 +568,16 @@ fn strip_bracketed_genus_doubt(ctx: &mut ParseContext, s: String) -> String {
caps[2].split_whitespace().next().unwrap_or("").to_string(),
)
} else if let Some(caps) = BRACKETED_SUBGENUS_DOUBT.captures(&s) {
let subgenus = caps.get(2).or(caps.get(3)).unwrap().as_str();
let subgenus = caps
.get(2)
.or(caps.get(3))
.or(caps.get(4))
.unwrap()
.as_str();
(
format!("{} ({subgenus}) {}", &caps[1], &caps[4]),
format!("{} ({subgenus}) {}", &caps[1], &caps[5]),
NamePart::Infrageneric,
caps[4].split_whitespace().next().unwrap_or("").to_string(),
caps[5].split_whitespace().next().unwrap_or("").to_string(),
)
} else {
return s;
Expand Down Expand Up @@ -7637,6 +7653,38 @@ mod tests {
assert!(c.name.doubtful);
}

#[test]
fn qmark_after_an_abbreviated_author_flags_uncertain() {
for s in [
"Mikania attenuata DC.?",
"Bidens rosaefolius H.B.K.?",
" d'Orb.?",
] {
let mut c = ctx("x");
assert_eq!(flag_uncertain_authorship(&mut c, s.to_string()), s);
assert!(c.name.doubtful, "{s}");
assert!(c
.name
.warnings
.contains(&warnings::UNCERTAIN_AUTHORSHIP.to_string()));
}
}

#[test]
fn qmark_after_an_epithet_or_the_genus_is_no_uncertain_author() {
for s in [
"Abies alba?",
"Abies?",
"Abies alba var. nana?",
"Abies? alba",
] {
let mut c = ctx("x");
assert_eq!(flag_uncertain_authorship(&mut c, s.to_string()), s);
assert!(!c.name.doubtful, "{s}");
assert!(c.name.warnings.is_empty(), "{s}");
}
}

#[test]
fn clean_name_is_not_flagged() {
let mut c = ctx("x");
Expand Down
5 changes: 3 additions & 2 deletions crates/nameparser/tests/gna_03.rs
Original file line number Diff line number Diff line change
Expand Up @@ -452,12 +452,13 @@ fn names_with_multiple_dashes_in_specific_epithet() {
#[test]
fn genus_with_question_mark() {
// group: Genus with question mark — open-nomenclature doubtful identification.
// The "?" is captured as a SPECIFIC epithet qualifier (like cf. or aff.).
// The "?" glued to the genus doubts the genus, as "?Ferganoconcha oblonga" does (gnparser
// and Java gave it to the species).
assert_name("Ferganoconcha? oblonga")
.species("Ferganoconcha", "oblonga")
.type_(NameType::Informal)
.doubtful()
.qualifiers(&[(NamePart::Specific, "?")])
.qualifiers(&[(NamePart::Generic, "?")])
.warning(&[warnings::QUESTION_MARKS_REMOVED])
.nothing_else();
}
Expand Down
Loading
Loading