Skip to main content

DiscourseScope

Struct DiscourseScope 

Source
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:

  1. Syntactic distance isn’t the only constraint
  2. Active/passive, logical subjects, and pragmatic factors affect parallelism
  3. The candidate_antecedent_spans method 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

Source

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);
Source

pub fn sentence_count(&self) -> usize

Number of sentences detected.

Source

pub fn clause_count(&self) -> usize

Number of clauses detected.

Source

pub fn sentence_at(&self, offset: usize) -> Option<(usize, usize)>

Get the sentence containing a character offset.

Source

pub fn clause_at(&self, offset: usize) -> Option<(usize, usize)>

Get the clause containing a character offset.

Source

pub fn preceding_clauses(&self, offset: usize, n: usize) -> Vec<(usize, usize)>

Get the N preceding clauses from an offset.

Source

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 to analyze)
  • 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());
}
Source

pub fn candidate_antecedent_spans( &self, anaphor_offset: usize, ) -> Vec<(usize, usize)>

For an anaphor at a given offset, find candidate antecedent spans.

Returns spans in order of preference (nearest first).

Trait Implementations§

Source§

impl Clone for DiscourseScope

Source§

fn clone(&self) -> DiscourseScope

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for DiscourseScope

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more