pub struct DiscourseScope {
pub sentence_boundaries: Vec<usize>,
pub clause_boundaries: Vec<usize>,
/* private fields */
}Expand description
A simple clause/sentence boundary detector for discourse scope analysis.
§Why Bounded Context?
Abstract anaphora resolution requires finding antecedents in preceding discourse. But how far back should we look?
Empirical finding: Window size n=2-3 preceding clauses outperforms
max-length concatenation by ~6% F1. Larger windows add noise without
improving recall. This motivates bounded preceding_clauses(offset, n).
§Antecedent Distance Distribution
For abstract anaphora, the antecedent is typically:
- Immediately preceding clause (~60%): “X happened. This…”
- Same sentence, different clause (~20%): “When X, this…”
- Previous sentence (~15%): “X. Y. This…”
- 2+ sentences back (~5%): Rare, usually with explicit markers
§Theoretical Background (Dalrymple et al. 1991)
The equational view frames resolution as finding P such that
P(parallel_elements) = source_interpretation. Crucially, parallelism
need not be purely syntactic—semantic and pragmatic parallelism also
license resolution (Section 5.1).
This means:
- Syntactic distance isn’t the only constraint
- Active/passive, logical subjects, and pragmatic factors affect parallelism
- The
candidate_antecedent_spansmethod returns spans in preference order, but semantic parallelism must be checked by higher-level resolution logic
§Example
use anno::discourse::DiscourseScope;
let text = "Russia invaded Ukraine in 2022. This caused inflation. It affected everyone.";
let scope = DiscourseScope::analyze(text);
// For "This" at position 32, the immediately preceding clause is preferred
let candidates = scope.preceding_clauses(32, 2);
// Returns spans for "Russia invaded Ukraine in 2022" and potentially more§Character vs Byte Offsets
All offsets in DiscourseScope are character offsets, not byte offsets.
This is critical for Unicode text where characters may be multi-byte:
use anno::discourse::DiscourseScope;
let text = "日本語。英語。"; // Japanese with periods
let scope = DiscourseScope::analyze(text);
// Character-based: each kanji is 1 character (but 3 bytes)
assert!(scope.sentence_count() >= 1);Use extract_span to safely extract text from character offsets.
Fields§
§sentence_boundaries: Vec<usize>Sentence boundaries (character offsets, NOT byte offsets)
clause_boundaries: Vec<usize>Clause boundaries (character offsets, more fine-grained)
Implementations§
Source§impl DiscourseScope
impl DiscourseScope
Sourcepub fn analyze(text: &str) -> DiscourseScope
pub fn analyze(text: &str) -> DiscourseScope
Analyze text for discourse boundaries.
§Example
use anno::discourse::DiscourseScope;
let text = "Russia invaded Ukraine. This caused inflation.";
let scope = DiscourseScope::analyze(text);
assert_eq!(scope.sentence_count(), 2);Sourcepub fn sentence_count(&self) -> usize
pub fn sentence_count(&self) -> usize
Number of sentences detected.
Sourcepub fn clause_count(&self) -> usize
pub fn clause_count(&self) -> usize
Number of clauses detected.
Sourcepub fn sentence_at(&self, offset: usize) -> Option<(usize, usize)>
pub fn sentence_at(&self, offset: usize) -> Option<(usize, usize)>
Get the sentence containing a character offset.
Sourcepub fn clause_at(&self, offset: usize) -> Option<(usize, usize)>
pub fn clause_at(&self, offset: usize) -> Option<(usize, usize)>
Get the clause containing a character offset.
Sourcepub fn preceding_clauses(&self, offset: usize, n: usize) -> Vec<(usize, usize)>
pub fn preceding_clauses(&self, offset: usize, n: usize) -> Vec<(usize, usize)>
Get the N preceding clauses from an offset.
Sourcepub fn extract_span<'a>(
&self,
text: &'a str,
start: usize,
end: usize,
) -> &'a str
pub fn extract_span<'a>( &self, text: &'a str, start: usize, end: usize, ) -> &'a str
Get text for a span (character offsets).
Converts character offsets to byte offsets for safe extraction.
§Arguments
text- The original text (must match the text passed toanalyze)start- Start character offset (inclusive)end- End character offset (exclusive)
§Example
use anno::discourse::DiscourseScope;
let text = "日本語。英語。";
let scope = DiscourseScope::analyze(text);
// Extract first sentence (chars 0-4 = "日本語。")
if let Some((start, end)) = scope.sentence_at(0) {
let extracted = scope.extract_span(text, start, end);
assert!(!extracted.is_empty());
}Trait Implementations§
Source§impl Clone for DiscourseScope
impl Clone for DiscourseScope
Source§fn clone(&self) -> DiscourseScope
fn clone(&self) -> DiscourseScope
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for DiscourseScope
impl RefUnwindSafe for DiscourseScope
impl Send for DiscourseScope
impl Sync for DiscourseScope
impl Unpin for DiscourseScope
impl UnsafeUnpin for DiscourseScope
impl UnwindSafe for DiscourseScope
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more