use super::citation::{DOI, is_name, trim_id};
use super::plain_text::plain_of;
use crate::model::{Block, BlockKind, Document, Line, SUP, Span};
use regex::Regex;
use serde::Serialize;
use std::sync::LazyLock;
use unicode_normalization::UnicodeNormalization;
const NAME_GAP_EM: f32 = 1.0;
const SIDE_BY_SIDE_MIN_WORDS_COUNT: usize = 4;
const FRONT_PAGES_COUNT: usize = 2;
const EMAIL_MATCH_MIN_CHARS_COUNT: usize = 3;
const MARK_OPEN: char = '\u{2}';
const MARK_CLOSE: char = '\u{3}';
const MONTHS: [&str; 12] = [
"january",
"february",
"march",
"april",
"may",
"june",
"july",
"august",
"september",
"october",
"november",
"december",
];
static STAMP: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r"(?:\[\s*([a-z\-]+(?:\.[A-Za-z\-]+)?)\s*\]\s*)?\b(\d{1,2})\s*([A-Za-z]{3,9})\.?\s*(\d{4})").unwrap()
});
static DATE: LazyLock<Regex> = LazyLock::new(|| {
Regex::new(r"(?:\b(\d{1,2})(?:st|nd|rd|th)?\s+([A-Za-z]{3,9})\.?,?\s+(\d{4})\b|\b([A-Za-z]{3,9})\.?\s+(?:(\d{1,2}),?\s+)?(\d{4})\b)").unwrap()
});
static BRACED_EMAILS: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"\{([^}]+)\}\s*@\s*([\w\-]+(?:\.[\w\-]+)+)").unwrap());
static EMAIL: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"[\w.+\-]+@[\w\-]+(?:\.[\w\-]+)*\.[A-Za-z]{2,}").unwrap());
static NAME_SEPARATOR: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"\s*(?:,\s*(?:and\s+|&\s*)?|\s+and\s+|\s*&\s*|\u{2003}|;)\s*").unwrap());
static ET_AL: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"(?i)\s*\bet\.?\s+al\b\.?").unwrap());
static KEYWORDS: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r"(?i)^(?:keywords|key\s*words|index\s+terms)\s*[:.—–-]*\s*").unwrap());
static KEYWORD_SEPARATOR: LazyLock<Regex> = LazyLock::new(|| Regex::new(r"\s*(?:[,;·•]|\s[-–—]\s)\s*").unwrap());
#[derive(Debug, Default, Serialize)]
pub(crate) struct Front {
#[serde(skip_serializing_if = "Option::is_none")]
pub(crate) arxiv_category: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(crate) date: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub(crate) doi: Option<String>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub(crate) authors: Vec<Author>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub(crate) affiliations: Vec<String>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub(crate) author_notes: Vec<String>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub(crate) keywords: Vec<String>,
#[serde(rename = "abstract", skip_serializing_if = "Option::is_none")]
pub(crate) abstract_text: Option<String>,
}
#[derive(Debug, Default, Clone, PartialEq, Serialize)]
pub(crate) struct Author {
pub(crate) name: String,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub(crate) affiliations: Vec<usize>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub(crate) emails: Vec<String>,
#[serde(skip_serializing_if = "Vec::is_empty")]
pub(crate) notes: Vec<usize>,
#[serde(skip)]
marks: Vec<String>,
}
pub(crate) fn front_matter(doc: &Document) -> Front {
let mut front = Front::default();
if let Some(caps) = doc.meta.arxiv_stamp.as_deref().and_then(|stamp| STAMP.captures(stamp)) {
front.arxiv_category = caps.get(1).map(|category| category.as_str().to_string());
front.date = iso_date(caps[4].parse().ok(), month_number(&caps[3]), caps[2].parse().ok());
}
let mut people = People::default();
for block in doc
.blocks
.iter()
.filter(|block| matches!(block.kind, BlockKind::Authors))
{
people.card(block, &mut front.date);
}
let marked = marked_texts(doc, &people.mark_rows);
people.resolve(marked);
people.match_emails();
front.authors = people.authors;
front.affiliations = people.affiliations;
front.author_notes = people.notes;
front.doi = doi(doc);
front.keywords = keywords(doc);
let abstract_text = doc
.blocks
.iter()
.filter(|block| matches!(block.kind, BlockKind::Paragraph { in_abstract: true }))
.map(|block| plain_of(&block.spans))
.filter(|text| !KEYWORDS.is_match(text))
.collect::<Vec<_>>()
.join("\n");
front.abstract_text = (!abstract_text.is_empty()).then_some(abstract_text);
front
}
#[derive(Default)]
struct People {
authors: Vec<Author>,
affiliations: Vec<String>,
notes: Vec<String>,
loose_emails: Vec<String>,
mark_rows: Vec<(String, String)>,
unaffiliated: Option<std::ops::Range<usize>>,
}
impl People {
fn card(&mut self, block: &Block, date: &mut Option<String>) {
let first = self.authors.len();
let (affiliation_rows, emails) = self.read_rows(block, date);
let names = first..self.authors.len();
let unaffiliated = self.unaffiliated.take();
if names.is_empty() {
if let Some(before) = unaffiliated
&& !affiliation_rows.is_empty()
{
self.give_affiliation(before, &affiliation_rows.join(", "));
}
self.loose_emails.extend(emails);
return;
}
if affiliation_rows.is_empty() {
self.unaffiliated = Some(names.clone());
}
let side_by_side = match affiliation_rows.as_slice() {
[row] if names.len() > 1 => row_pieces(row, &block.lines).filter(|pieces| pieces.len() == names.len()),
_ => None,
};
if let Some(pieces) = side_by_side {
for (author, part) in names.clone().zip(pieces) {
self.give_affiliation(author..author + 1, &part);
}
} else if !affiliation_rows.is_empty() {
self.give_affiliation(names.clone(), &affiliation_rows.join(", "));
}
if emails.len() == names.len() {
for (author, email) in names.zip(emails) {
self.authors[author].emails.push(email);
}
} else {
self.loose_emails.extend(emails);
}
}
fn read_rows(&mut self, block: &Block, date: &mut Option<String>) -> (Vec<String>, Vec<String>) {
let rows: Vec<&[Span]> = if block.rows.is_empty() {
vec![&block.spans]
} else {
block.rows.iter().map(Vec::as_slice).collect()
};
let first = self.authors.len();
let mut names_done = false;
let mut names_continue = false;
let mut affiliation_rows: Vec<String> = Vec::new();
let mut emails: Vec<String> = Vec::new();
for row in rows {
let text = plain_of(row);
if text.contains('@') {
emails.extend(emails_in(&text));
} else if let Some(found) = row_date(&text) {
date.get_or_insert(found);
} else if starts_with_mark(row) {
self.mark_rows.extend(marked_row(row));
} else {
let continued = self.authors.len() == first || names_continue || text.starts_with("and ");
if !names_done
&& continued
&& let Some(names) = names_row(row, &block.lines)
{
self.authors.extend(names);
names_continue = text.ends_with(',');
continue;
}
if is_affiliation(&text) {
affiliation_rows.push(row.iter().map(|span| span.text.as_str()).collect());
}
}
names_done = true;
}
(affiliation_rows, emails)
}
fn give_affiliation(&mut self, authors: std::ops::Range<usize>, text: &str) {
let index = self.affiliation(text);
for author in authors {
self.authors[author].affiliations.push(index);
}
}
fn affiliation(&mut self, text: &str) -> usize {
index_of(
&mut self.affiliations,
text.split_whitespace().collect::<Vec<_>>().join(" "),
)
}
fn resolve(&mut self, marked: Vec<(String, String)>) {
for author in &mut self.authors {
for mark in std::mem::take(&mut author.marks) {
let Some((_, text)) = marked.iter().find(|(found, _)| *found == mark) else {
continue;
};
let emails = emails_in(text);
for email in &emails {
if !author.emails.contains(email) {
author.emails.push(email.clone());
}
}
let only_emails = EMAIL
.replace_all(text, "")
.trim_matches([' ', ',', ';', '.', ':'])
.is_empty();
if only_emails && !emails.is_empty() {
continue;
}
if mark.chars().all(char::is_alphanumeric) {
let index = index_of(&mut self.affiliations, text.clone());
if !author.affiliations.contains(&index) {
author.affiliations.push(index);
}
} else {
let index = index_of(&mut self.notes, text.clone());
if !author.notes.contains(&index) {
author.notes.push(index);
}
}
}
}
}
fn match_emails(&mut self) {
for email in std::mem::take(&mut self.loose_emails) {
let user = fold(email.split('@').next().unwrap_or(""));
let matching: Vec<usize> = (0..self.authors.len())
.filter(|&i| name_matches(&self.authors[i].name, &user))
.collect();
let author = match matching.as_slice() {
[only] => Some(*only),
[] if self.authors.len() == 1 => Some(0),
_ => None,
};
if let Some(author) = author {
self.authors[author].emails.push(email);
}
}
}
}
fn index_of(list: &mut Vec<String>, text: String) -> usize {
match list.iter().position(|found| *found == text) {
Some(index) => index,
None => {
list.push(text);
list.len() - 1
}
}
}
fn name_matches(name: &str, user: &str) -> bool {
let words: Vec<String> = name
.split_whitespace()
.map(fold)
.filter(|word| !word.is_empty())
.collect();
let (Some(first), Some(last)) = (words.first(), words.last()) else {
return false;
};
let long = |word: &String| word.chars().count() >= EMAIL_MATCH_MIN_CHARS_COUNT;
(long(last) && user.contains(last.as_str()))
|| (long(first) && user.contains(first.as_str()))
|| first.chars().next().is_some_and(|initial| {
let stem: String = last.chars().take(EMAIL_MATCH_MIN_CHARS_COUNT).collect();
long(&stem) && user.starts_with(&format!("{initial}{stem}"))
})
}
fn fold(text: &str) -> String {
text.nfd()
.filter(char::is_ascii_alphabetic)
.map(|ch| ch.to_ascii_lowercase())
.collect()
}
fn names_row(row: &[Span], lines: &[Line]) -> Option<Vec<Author>> {
let mut text = String::new();
let mut marks: Vec<String> = Vec::new();
for span in row {
if span.style & SUP != 0 {
marks.push(span.text.clone());
text.push(MARK_OPEN);
text.push_str(&(marks.len() - 1).to_string());
text.push(MARK_CLOSE);
} else {
text.push_str(&span.text);
}
}
let text = ET_AL.replace_all(&text, "");
let text = text.trim().trim_start_matches("By ").trim_start_matches("by ");
let mut authors = Vec::new();
for piece in NAME_SEPARATOR.split(text) {
let (name, piece_marks) = take_marks(piece, &marks);
let name = name
.trim()
.trim_end_matches([',', '*', '∗', '†', '‡', '§', '¶', '⋆'])
.trim()
.to_string();
if name.is_empty() {
if let Some(last) = authors.last_mut() {
let last: &mut Author = last;
last.marks.extend(piece_marks);
}
continue;
}
if name.split_whitespace().count() >= SIDE_BY_SIDE_MIN_WORDS_COUNT
&& let Some(names) = side_by_side(&name, lines)
{
authors.extend(names.into_iter().map(|name| Author {
name,
..Author::default()
}));
continue;
}
if !is_name(&name) {
return None;
}
authors.push(Author {
name,
marks: piece_marks,
..Author::default()
});
}
(!authors.is_empty()).then_some(authors)
}
fn take_marks(piece: &str, marks: &[String]) -> (String, Vec<String>) {
let mut name = String::new();
let mut found = Vec::new();
let mut rest = piece;
while let Some(open) = rest.find(MARK_OPEN) {
name.push_str(&rest[..open]);
let after = &rest[open + MARK_OPEN.len_utf8()..];
let close = after.find(MARK_CLOSE).unwrap_or(after.len());
if let Some(mark) = after[..close].parse::<usize>().ok().and_then(|i| marks.get(i)) {
found.extend(
mark.split([',', ' '])
.map(str::trim)
.filter(|mark| !mark.is_empty())
.map(str::to_string),
);
}
rest = after.get(close + MARK_CLOSE.len_utf8()..).unwrap_or("");
}
name.push_str(rest);
(name, found)
}
fn side_by_side(text: &str, lines: &[Line]) -> Option<Vec<String>> {
let line = line_of(text, lines)?;
let names = gap_pieces(line);
(names.len() > 1 && names.iter().all(|name| is_name(name))).then_some(names)
}
fn row_pieces(row: &str, lines: &[Line]) -> Option<Vec<String>> {
let parts: Vec<String> = row
.trim()
.split('\u{2003}')
.map(|part| part.split_whitespace().collect::<Vec<_>>().join(" "))
.collect();
if parts.len() > 1 {
return Some(parts);
}
let pieces = gap_pieces(line_of(&parts[0], lines)?);
(pieces.len() > 1).then_some(pieces)
}
fn line_of<'a>(text: &str, lines: &'a [Line]) -> Option<&'a Line> {
let compact: Vec<char> = text.chars().filter(|ch| !ch.is_whitespace()).collect();
lines.iter().find(|line| {
line.glyphs
.iter()
.filter(|glyph| glyph.script == 0)
.flat_map(|glyph| glyph.text.chars())
.filter(|ch| !ch.is_whitespace())
.eq(compact.iter().copied())
})
}
fn gap_pieces(line: &Line) -> Vec<String> {
let mut pieces: Vec<String> = Vec::new();
let mut last_x1: Option<f32> = None;
for glyph in line.glyphs.iter().filter(|glyph| glyph.script == 0) {
let wide = last_x1.is_some_and(|x1| glyph.x0 - x1 > NAME_GAP_EM * glyph.size);
if wide || pieces.is_empty() {
pieces.push(String::new());
} else if glyph.space_before {
pieces.last_mut().unwrap().push(' ');
}
pieces.last_mut().unwrap().push_str(&glyph.text);
last_x1 = Some(glyph.x1);
}
pieces.into_iter().map(|piece| piece.trim().to_string()).collect()
}
fn is_affiliation(text: &str) -> bool {
text.starts_with(|ch: char| !ch.is_lowercase() && ch != '(') && text.chars().any(char::is_alphabetic)
}
fn starts_with_mark(row: &[Span]) -> bool {
let mut spans = row.iter().filter(|span| !span.text.trim().is_empty());
spans.next().is_some_and(|span| span.style & SUP != 0) && spans.next().is_some_and(|span| span.style & SUP == 0)
}
fn marked_row(row: &[Span]) -> Vec<(String, String)> {
let mut out: Vec<(String, String)> = Vec::new();
for span in row {
if span.style & SUP != 0 && !span.text.trim().is_empty() {
out.push((span.text.trim().to_string(), String::new()));
} else if let Some((_, text)) = out.last_mut() {
text.push_str(&span.text);
}
}
out.into_iter()
.map(|(mark, text)| (mark, text.trim().trim_end_matches([',', ';']).trim().to_string()))
.filter(|(_, text)| !text.is_empty())
.collect()
}
fn marked_texts(doc: &Document, rows: &[(String, String)]) -> Vec<(String, String)> {
let mut marked = rows.to_vec();
for block in doc.blocks.iter().filter(|block| block.page == 0) {
if let BlockKind::Footnote { marker } = &block.kind
&& !marker.is_empty()
{
marked.push((marker.clone(), plain_of(&block.spans)));
}
}
marked
}
fn emails_in(text: &str) -> Vec<String> {
let mut emails = Vec::new();
let mut rest = text.to_string();
for caps in BRACED_EMAILS.captures_iter(text) {
for user in caps[1].split([',', ';']).map(str::trim).filter(|user| !user.is_empty()) {
emails.push(format!("{user}@{}", &caps[2]));
}
rest = rest.replace(&caps[0], " ");
}
emails.extend(EMAIL.find_iter(&rest).map(|found| found.as_str().to_string()));
emails
}
fn row_date(text: &str) -> Option<String> {
let lower = text.to_lowercase();
let at = lower.find("published").unwrap_or(0);
let caps = DATE.captures(&text[at..])?;
let whole = caps.get(0).unwrap();
let only_date = whole.as_str().len() + 2 >= text.trim().len();
if !only_date && !lower.starts_with("(received") && at == 0 {
return None;
}
let (day, month, year) = match caps.get(1) {
Some(day) => (day.as_str().parse().ok(), month_number(&caps[2]), caps[3].parse().ok()),
None => (
caps.get(5).and_then(|day| day.as_str().parse().ok()),
month_number(&caps[4]),
caps[6].parse().ok(),
),
};
month?;
iso_date(year, month, day)
}
fn month_number(name: &str) -> Option<u32> {
let lower = name.to_lowercase();
if lower.len() < 3 {
return None;
}
MONTHS
.iter()
.position(|month| {
month.starts_with(&lower) || (lower.len() >= 3 && month.starts_with(&lower[..3]) && lower.len() <= 4)
})
.map(|index| index as u32 + 1)
}
fn iso_date(year: Option<u32>, month: Option<u32>, day: Option<u32>) -> Option<String> {
let year = year?;
let month = month?;
Some(match day {
Some(day) if (1..=31).contains(&day) => format!("{year:04}-{month:02}-{day:02}"),
_ => format!("{year:04}-{month:02}"),
})
}
fn doi(doc: &Document) -> Option<String> {
doc.blocks
.iter()
.filter(|block| block.page == 0 && !matches!(block.kind, BlockKind::Reference { .. }))
.find_map(|block| {
let link = block
.spans
.iter()
.filter_map(|span| span.href.as_deref())
.find_map(|href| href.find("doi.org/").map(|at| trim_id(&href[at + "doi.org/".len()..])));
link.or_else(|| DOI.captures(&block.text()).map(|caps| trim_id(&caps[1])))
})
}
fn keywords(doc: &Document) -> Vec<String> {
doc.blocks
.iter()
.filter(|block| block.page < FRONT_PAGES_COUNT && !matches!(block.kind, BlockKind::Reference { .. }))
.find_map(|block| {
let text = plain_of(&block.spans);
let head = KEYWORDS.find(&text)?;
let words: Vec<String> = KEYWORD_SEPARATOR
.split(&text[head.end()..])
.map(|word| word.trim().trim_end_matches('.').trim().to_string())
.filter(|word| !word.is_empty())
.collect();
(!words.is_empty()).then_some(words)
})
.unwrap_or_default()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn braced_addresses_are_made_whole() {
assert_eq!(
emails_in("{kahe, v-xiangz}@microsoft.com"),
["kahe@microsoft.com", "v-xiangz@microsoft.com"]
);
assert_eq!(
emails_in("drob@mit.edu, shoyaida@fb.com"),
["drob@mit.edu", "shoyaida@fb.com"]
);
}
#[test]
fn dates_of_rows() {
assert_eq!(row_date("December 1997").as_deref(), Some("1997-12"));
assert_eq!(row_date("November 26, 2024").as_deref(), Some("2024-11-26"));
assert_eq!(
row_date("(Received 21 January 2016; published 11 February 2016)").as_deref(),
Some("2016-02-11")
);
assert_eq!(row_date("Google Research, Brain Team"), None);
assert_eq!(row_date("University of California, Santa Barbara, CA 93106"), None);
}
#[test]
fn addresses_hold_names() {
assert!(name_matches("Daniel A. Roberts", "drob"));
assert!(name_matches("Sho Yaida", "shoyaida"));
assert!(name_matches("Łukasz Kaiser", "lukaszkaiser"));
assert!(!name_matches("Boris Hanin", "drob"));
}
#[test]
fn marks_after_names() {
let mut sup = Span::plain("∗,†");
sup.style = SUP;
let mut one = Span::plain("1");
one.style = SUP;
let row = [
Span::plain("Alexey Dosovitskiy"),
sup,
Span::plain(", Lucas Beyer"),
one,
Span::plain(", and Jane Doe"),
];
let authors = names_row(&row, &[]).unwrap();
let names: Vec<_> = authors.iter().map(|a| (a.name.as_str(), a.marks.clone())).collect();
assert_eq!(
names,
[
("Alexey Dosovitskiy", vec!["∗".to_string(), "†".to_string()]),
("Lucas Beyer", vec!["1".to_string()]),
("Jane Doe", vec![]),
]
);
}
}