use super::entities::EntityCandidate;
use super::pack::LanguagePack;
use crate::types::Gender;
use std::collections::BTreeMap;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PronounClass {
Masculine,
Feminine,
Neuter,
Plural,
}
#[derive(Debug, Default, Clone)]
struct Entry {
established: bool,
masculine: u32,
feminine: u32,
masculine_title: bool,
feminine_title: bool,
neuter: u32,
locative: bool,
elsewhere: bool,
}
const TITLE: u32 = 4;
const SIGHTING: u32 = 1;
impl Entry {
fn wins(for_it: u32, against: u32) -> bool {
for_it > 0 && for_it >= against.saturating_mul(2)
}
fn personal(&self) -> u32 {
self.masculine + self.feminine
}
fn thing(&self) -> u32 {
self.neuter
+ if self.locative && !self.elsewhere {
TITLE
} else {
0
}
}
fn allows(&self, class: PronounClass) -> bool {
match class {
PronounClass::Masculine => self.gender() == Some(Gender::Masculine),
PronounClass::Feminine => self.gender() == Some(Gender::Feminine),
PronounClass::Neuter => Self::wins(self.thing(), self.personal()),
PronounClass::Plural => false,
}
}
fn gender(&self) -> Option<Gender> {
if !Self::wins(self.personal(), self.thing()) {
return None;
}
match (self.feminine_title, self.masculine_title) {
(true, false) => Some(Gender::Feminine),
(false, true) => Some(Gender::Masculine),
(true, true) => None,
(false, false) if Self::wins(self.feminine, self.masculine) => Some(Gender::Feminine),
(false, false) if Self::wins(self.masculine, self.feminine) => Some(Gender::Masculine),
_ => None,
}
}
}
#[derive(Debug, Default, Clone)]
pub struct Register {
entries: BTreeMap<String, Entry>,
}
impl Register {
pub fn observe(
&mut self,
sentence: &str,
mentions: &[&EntityCandidate],
pronouns: &[(&str, usize)],
pack: &LanguagePack,
) {
for mention in mentions {
let entry = self.entries.entry(mention.normalized.clone()).or_default();
let titled = title_gender(&mention.text, pack);
if let Some(masculine) = titled {
entry.established = true;
if masculine {
entry.masculine += TITLE;
entry.masculine_title = true;
} else {
entry.feminine += TITLE;
entry.feminine_title = true;
}
}
if !opens_sentence(sentence, mention.start) {
entry.established = true;
}
if preceded_by(sentence, mention.start, pack.determiners) {
entry.neuter += SIGHTING;
}
let normalized = mention.normalized.replace('_', " ");
if pack.temporal_names.contains(&normalized.as_str())
|| pack.place_names.contains(&normalized.as_str())
{
entry.neuter += TITLE;
entry.established = true;
}
if preceded_by(sentence, mention.start, pack.locative_prepositions) {
entry.locative = true;
} else {
entry.elsewhere = true;
}
}
self.learn_from_unambiguous_pronouns(sentence, mentions, pronouns, pack);
}
fn learn_from_unambiguous_pronouns(
&mut self,
sentence: &str,
mentions: &[&EntityCandidate],
pronouns: &[(&str, usize)],
pack: &LanguagePack,
) {
for (pronoun, start) in pronouns {
if !pack
.subject_pronouns
.iter()
.any(|p| pronoun.eq_ignore_ascii_case(p))
{
continue;
}
let mut before = mentions.iter().filter(|m| m.end <= *start);
let (Some(only), None) = (before.next(), before.next()) else {
continue;
};
if preceded_by(sentence, only.start, pack.locative_prepositions) {
continue;
}
let Some(class) = classify(pronoun, pack) else {
continue;
};
let entry = self.entries.entry(only.normalized.clone()).or_default();
match class {
PronounClass::Masculine => entry.masculine += SIGHTING,
PronounClass::Feminine => entry.feminine += SIGHTING,
PronounClass::Neuter => entry.neuter += SIGHTING,
PronounClass::Plural => {}
}
}
}
pub fn is_person(&self, name: &str) -> bool {
self.entries
.get(name)
.is_some_and(|e| Entry::wins(e.personal(), e.thing()))
}
pub fn is_thing(&self, name: &str) -> bool {
self.entries
.get(name)
.is_some_and(|e| Entry::wins(e.thing(), e.personal()))
}
pub fn is_established(&self, name: &str) -> bool {
self.entries
.get(name)
.is_some_and(|entry| entry.established)
}
pub fn gender(&self, name: &str) -> Option<Gender> {
self.entries.get(name).and_then(Entry::gender)
}
pub fn observe_role(&mut self, name: &str, gender: Gender) {
let entry = self.entries.entry(name.to_string()).or_default();
entry.established = true;
match gender {
Gender::Feminine => entry.feminine += TITLE,
Gender::Masculine => entry.masculine += TITLE,
}
}
pub fn compatible(&self, name: &str, class: PronounClass) -> bool {
match class {
PronounClass::Feminine => {
!self.is_thing(name) && self.gender(name) != Some(Gender::Masculine)
}
PronounClass::Masculine => {
!self.is_thing(name) && self.gender(name) != Some(Gender::Feminine)
}
PronounClass::Neuter => self.is_thing(name),
PronounClass::Plural => false,
}
}
pub fn allows(&self, pronoun: &str, referent: &str, pack: &LanguagePack) -> bool {
let Some(class) = classify(pronoun, pack) else {
return false;
};
let Some(entry) = self.entries.get(referent) else {
return false;
};
entry.established && entry.allows(class)
}
}
pub(super) fn classify(pronoun: &str, pack: &LanguagePack) -> Option<PronounClass> {
let has = |list: &[&str]| list.iter().any(|p| pronoun.eq_ignore_ascii_case(p));
if has(pack.masculine_pronouns) {
Some(PronounClass::Masculine)
} else if has(pack.feminine_pronouns) {
Some(PronounClass::Feminine)
} else if has(pack.neuter_pronouns) {
Some(PronounClass::Neuter)
} else if has(pack.plural_pronouns) {
Some(PronounClass::Plural)
} else {
None
}
}
fn title_gender(surface: &str, pack: &LanguagePack) -> Option<bool> {
for word in surface.split(' ') {
let word = word.to_lowercase();
if pack.masculine_titles.contains(&word.as_str()) {
return Some(true);
}
if pack.feminine_titles.contains(&word.as_str()) {
return Some(false);
}
if !pack.honorifics.contains(&word.as_str())
&& !pack.honorific_qualifiers.contains(&word.as_str())
{
return None;
}
}
None
}
fn opens_sentence(sentence: &str, start: usize) -> bool {
sentence[..start]
.chars()
.all(|c| c.is_whitespace() || "\"'\u{201C}\u{2018}([".contains(c))
}
fn preceded_by(sentence: &str, start: usize, words: &[&str]) -> bool {
sentence[..start]
.split_whitespace()
.next_back()
.is_some_and(|word| {
let word = word
.trim_matches(|c: char| !c.is_alphanumeric())
.to_lowercase();
words.contains(&word.as_str())
})
}