Lexicon

Struct Lexicon 

Source
pub struct Lexicon<T: Eq, Category: Eq> { /* private fields */ }
Expand description

The struct which represents a specific MG. They can be created by parsing:

let grammar: Lexicon<&str, &str> = Lexicon::from_string("John::d\nsaw::d= =d v\nMary::d")?;

It is also possible to make a Lexicon which use different types for categories or lemmas, such as integers or Strings. These can be made with Lexicon::remap_lexicon or by passing a vector of LexicalEntry using Lexicon::new.

§Lemmas

Lemmas are representated as Option<T>, so empty strings are None rather than an empty string to allow for arbitrary lemma type T.

§Useful functions

§Representation

The struct represents a lexicon as a graph, as per Stabler (2013). This means certain functions exploit this, such as Lexicon::n_nodes which doesn’t reference the number of features of the grammar as written out, but rather the number of nodes in the graph representation.

Implementations§

Source§

impl<T: Eq + Debug + Clone + SymbolCost, Category: Eq + Debug + Clone + Hash> Lexicon<T, Category>

Source

pub fn mdl_score_fixed_category_size( &self, n_phonemes: u16, n_categories: u16, ) -> Result<f64, LexiconError>

Returns the MDL score of a lexicon assuming the number of phonemes is fixed.

Source

pub fn mdl_score(&self, n_phonemes: u16) -> Result<f64, LexiconError>

Returns the MDL Score of the lexicon

§Arguments
  • n_phonemes - The size of required to encode a symbol of the phonology. e.g in English orthography, it would be 26.
Source§

impl<T, C> Lexicon<T, C>
where T: Eq + Debug + Clone, C: Eq + Debug + Clone,

Source

pub fn prune(&mut self, start: &C)

Prunes a grammar of any inaccessible grammatical categories that can’t be found given a base category of start

Source§

impl<T, C> Lexicon<T, C>
where T: Eq + Debug + Clone + Hash, C: Eq + Debug + Clone + FreshCategory + Hash,

Source

pub fn uniform_distribution(&mut self)

Remap the weights of all lexical items to be the uniform distribution.

Source

pub fn unify(&mut self, other: &Self) -> Vec<LexemeDetails>

Combines two lexicons and returns a vector of the leaves from other as LexemeDetails consisting of the index of the old leaves in other and their new index in the self.

Source

pub fn add_new_lexeme_from_sibling( &mut self, lemma: T, rng: &mut impl Rng, ) -> Option<NewLexeme>

Adds a new lexeme with the same features but a different lemma as another lexeme. Returns the node of the new lexeme as a NewLexeme with data about its copied sibling.

Source

pub fn add_new_lexeme( &mut self, lemma: Option<T>, config: Option<LexicalProbConfig>, rng: &mut impl Rng, ) -> Option<LexemeId>

Adds a new lexeme and return its node index if it is novel.

Source

pub fn delete_lexeme(&mut self, lexeme: LexemeId) -> Result<(), MutationError>

Deletes a lexeme from the grammar. Returns MutationError if the grammar has only one lexeme or if the NodeIndex passed is not a leaf.

Source

pub fn delete_from_node(&mut self, rng: &mut impl Rng)

Delets a random branch of the grammar

Source

pub fn delete_node(&mut self, rng: &mut impl Rng) -> Option<LexemeId>

Find a random node and deletes it, and then restitches its parent and children.

Source

pub fn random( base_category: &C, lemmas: &[T], config: Option<LexicalProbConfig>, rng: &mut impl Rng, ) -> Self

Samples an entirely random grammar

Source

pub fn change_feature( &mut self, lemmas: &[T], config: Option<LexicalProbConfig>, rng: &mut impl Rng, )

Chose a random feature and change it to something else.

Source

pub fn resample_below_node( &mut self, lemmas: &[T], config: Option<LexicalProbConfig>, rng: &mut impl Rng, )

Finds a random node and deletes its children and samples from scratch below that node.

Source§

impl<T, Category> Lexicon<T, Category>
where T: Eq + Debug + Clone + Display, Category: Eq + Debug + Clone + Display,

Source

pub fn graphviz(&self) -> String

Prints a lexicon as a GraphViz dot file.

Source§

impl<T: Eq, Category: Eq> Lexicon<T, Category>

Source

pub fn leaf_to_features<'a>( &'a self, lexeme: LexemeId, ) -> Option<Climber<'a, T, Category>>

Climb up a lexeme over all of its features

Source

pub fn leaf_to_lemma(&self, lexeme_id: LexemeId) -> Option<&Option<T>>

Gets the lemma of a lexeme.

Source

pub fn category(&self, lexeme_id: LexemeId) -> Option<&Category>

Get the category of a lexeme.

Source§

impl<T: Eq + Debug + Clone, Category: Eq + Debug + Clone> Lexicon<T, Category>

Source

pub fn is_complement(&self, nx: NodeIndex) -> bool

Checks if nx is a complement

Source

pub fn n_nodes(&self) -> usize

The number of nodes in a lexicon.

Source

pub fn leaves(&self) -> &[LexemeId]

Returns the leaves of a grammar

Source

pub fn sibling_leaves(&self) -> Vec<Vec<LexemeId>>

Returns all leaves with their sibling nodes (e.g. exemes that are identical except for their lemma)

Source

pub fn lexemes_and_ids( &self, ) -> Result<impl Iterator<Item = (LexemeId, LexicalEntry<T, Category>)>, LexiconError>

Gets each lexical entry along with its ID.

Source

pub fn lexemes(&self) -> Result<Vec<LexicalEntry<T, Category>>, LexiconError>

Turns a lexicon into a Vec<LexicalEntry<T, Category>> which can be useful for printing or investigating individual lexical entries.

Source

pub fn get_lexical_entry( &self, lexeme_id: LexemeId, ) -> Result<LexicalEntry<T, Category>, LexiconError>

Given a leaf node, return its LexicalEntry

Source

pub fn lemmas(&self) -> impl Iterator<Item = &Option<T>>

Get all lemmas of a grammar

Source

pub fn new(items: Vec<LexicalEntry<T, Category>>, collapse_lemmas: bool) -> Self

Create a new grammar from a Vec of LexicalEntry

Source

pub fn new_with_weights( items: Vec<LexicalEntry<T, Category>>, weights: Vec<f64>, collapse_lemmas: bool, ) -> Self

Create a new grammar from a Vec of LexicalEntry where lexical entries are weighted in probability

Source

pub fn categories(&self) -> impl Iterator<Item = &Category>

Iterate over all categories of a grammar

Source

pub fn licensor_types(&self) -> impl Iterator<Item = &Category>

Iterate over all licensors of a grammar

Source§

impl<'src> Lexicon<&'src str, &'src str>

Source

pub fn from_string(s: &'src str) -> Result<Self, LexiconParsingError<'src>>

Parse a grammar and return a Lexicon<&str, &str>

Source§

impl<T: Eq, C: Eq> Lexicon<T, C>

Source

pub fn remap_lexicon<'lex, T2: Eq, C2: Eq>( &'lex self, lemma_map: impl Fn(&'lex T) -> T2, category_map: impl Fn(&'lex C) -> C2, ) -> Lexicon<T2, C2>

Converts a Lexicon<T,C> to a Lexicon<T2, C2> according to two functions which remap them.

Source§

impl<'a> Lexicon<&'a str, &'a str>

Source

pub fn to_owned_values(&self) -> Lexicon<String, String>

Converts from Lexicon<&str, &str> to Lexicon<String, String>

Source§

impl<T, C> Lexicon<T, C>
where T: Eq + Debug + Clone + Hash, C: Eq + Clone + Debug + Hash,

Source

pub fn valid_continuations( &self, initial_category: C, prefix: &[PhonContent<T>], config: &ParsingConfig, ) -> Result<HashSet<Continuation<T>>, ParsingError<C>>

Given a grammar and a prefix string, return a [HashSet] of the possible Continuations (i.e. next words) that are valid in the grammar. Returns an ParsingError if there is no node with the category of initial_category.


let lex = Lexicon::from_string("a::S= b= S\n::S\nb::b")?;
let continuations = lex.valid_continuations("S", &PhonContent::from(["a"]), &ParsingConfig::default())?;
assert_eq!(continuations, HashSet::from_iter([Continuation::Word("b"), Continuation::Word("a")].into_iter()));
let continuations = lex.valid_continuations("S", &PhonContent::from(["a", "b"]), &ParsingConfig::default())?;
assert_eq!(continuations, HashSet::from_iter([Continuation::EndOfSentence]));
Source§

impl<T: Eq + Clone, C: Eq + Clone + Display> Lexicon<T, C>

Source

pub fn derivation(&self, rules: RulePool) -> Derivation<'static, T, C>

Converts a RulePool into a Derivation which allows the construction of Trees which can be used to plot syntactic trees throughout the derivation of the parse.

Source§

impl<T, Category> Lexicon<T, Category>
where T: Eq + Debug + Clone, Category: Eq + Clone + Debug,

Source

pub fn generate( &self, category: Category, config: &ParsingConfig, ) -> Result<Generator<&Self, T, Category>, ParsingError<Category>>

Generates the strings in a grammar that are findable according to the parsing config.

Returns an iterator, Parser which has items of type ([LogProb<f64>], Vec<T>, RulePool) where the middle items are the generated strings and the RulePool has the structure of each genrerated string.

Source

pub fn into_generate( self, category: Category, config: &ParsingConfig, ) -> Result<Generator<Self, T, Category>, ParsingError<Category>>

Like Lexicon::generate but consumes the lexicon.

Source

pub fn parse<'a, 'b>( &'a self, s: &'b [PhonContent<T>], category: Category, config: &'a ParsingConfig, ) -> Result<Parser<'a, 'b, T, Category>, ParsingError<Category>>

Parses a sentence under the assumption it is has the category, category.

Returns an iterator, Parser which has items of type ([LogProb<f64>], &'a [T], RulePool)

Source

pub fn parse_multiple<'a, 'b, U>( &'a self, sentences: &'b [U], category: Category, config: &'a ParsingConfig, ) -> Result<Parser<'a, 'b, T, Category>, ParsingError<Category>>
where U: AsRef<[PhonContent<T>]>,

Functions like Lexicon::parse but parsing multiple grammars at once.

Source

pub fn fuzzy_parse<'a, 'b, U>( &'a self, sentences: &'b [U], category: Category, config: &'a ParsingConfig, ) -> Result<FuzzyParser<'a, 'b, T, Category>, ParsingError<Category>>
where U: AsRef<[PhonContent<T>]>,

Functions like Lexicon::parse or Lexicon::generate but will return parses that are close to the provided sentences, but are not necessarily the same.

Trait Implementations§

Source§

impl<T: Clone + Eq, Category: Clone + Eq> Clone for Lexicon<T, Category>

Source§

fn clone(&self) -> Lexicon<T, Category>

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<T: Debug + Eq, Category: Debug + Eq> Debug for Lexicon<T, Category>

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<T, C> Display for Lexicon<T, C>
where T: Eq + Display + Debug + Clone, C: Eq + Display + Debug + Clone,

Source§

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

Formats the value using the given formatter. Read more
Source§

impl<T: Eq, C: Eq> From<SemanticLexicon<'_, T, C>> for Lexicon<T, C>

Source§

fn from(value: SemanticLexicon<'_, T, C>) -> Self

Converts to this type from the input type.
Source§

impl<T: Eq, Category: Eq> PartialEq for Lexicon<T, Category>

Source§

fn eq(&self, other: &Self) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<T: Eq, Category: Eq> Eq for Lexicon<T, Category>

Auto Trait Implementations§

§

impl<T, Category> Freeze for Lexicon<T, Category>

§

impl<T, Category> RefUnwindSafe for Lexicon<T, Category>
where Category: RefUnwindSafe, T: RefUnwindSafe,

§

impl<T, Category> Send for Lexicon<T, Category>
where Category: Send, T: Send,

§

impl<T, Category> Sync for Lexicon<T, Category>
where Category: Sync, T: Sync,

§

impl<T, Category> Unpin for Lexicon<T, Category>
where Category: Unpin, T: Unpin,

§

impl<T, Category> UnwindSafe for Lexicon<T, Category>
where Category: UnwindSafe, T: UnwindSafe,

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
§

impl<T> Conv for T

§

fn conv<T>(self) -> T
where Self: Into<T>,

Converts self into T using Into<T>. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
§

impl<T> FmtForward for T

§

fn fmt_binary(self) -> FmtBinary<Self>
where Self: Binary,

Causes self to use its Binary implementation when Debug-formatted.
§

fn fmt_display(self) -> FmtDisplay<Self>
where Self: Display,

Causes self to use its Display implementation when Debug-formatted.
§

fn fmt_lower_exp(self) -> FmtLowerExp<Self>
where Self: LowerExp,

Causes self to use its LowerExp implementation when Debug-formatted.
§

fn fmt_lower_hex(self) -> FmtLowerHex<Self>
where Self: LowerHex,

Causes self to use its LowerHex implementation when Debug-formatted.
§

fn fmt_octal(self) -> FmtOctal<Self>
where Self: Octal,

Causes self to use its Octal implementation when Debug-formatted.
§

fn fmt_pointer(self) -> FmtPointer<Self>
where Self: Pointer,

Causes self to use its Pointer implementation when Debug-formatted.
§

fn fmt_upper_exp(self) -> FmtUpperExp<Self>
where Self: UpperExp,

Causes self to use its UpperExp implementation when Debug-formatted.
§

fn fmt_upper_hex(self) -> FmtUpperHex<Self>
where Self: UpperHex,

Causes self to use its UpperHex implementation when Debug-formatted.
§

fn fmt_list(self) -> FmtList<Self>
where &'a Self: for<'a> IntoIterator,

Formats each item in a sequence. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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
§

impl<'src, T> IntoMaybe<'src, T> for T
where T: 'src,

§

type Proj<U: 'src> = U

§

fn map_maybe<R>( self, _f: impl FnOnce(&'src T) -> &'src R, g: impl FnOnce(T) -> R, ) -> <T as IntoMaybe<'src, T>>::Proj<R>
where R: 'src,

§

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

§

fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> R
where Self: Sized,

Pipes by value. This is generally the method you want to use. Read more
§

fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> R
where R: 'a,

Borrows self and passes that borrow into the pipe function. Read more
§

fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> R
where R: 'a,

Mutably borrows self and passes that borrow into the pipe function. Read more
§

fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
where Self: Borrow<B>, B: 'a + ?Sized, R: 'a,

Borrows self, then passes self.borrow() into the pipe function. Read more
§

fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
where Self: BorrowMut<B>, B: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.borrow_mut() into the pipe function. Read more
§

fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
where Self: AsRef<U>, U: 'a + ?Sized, R: 'a,

Borrows self, then passes self.as_ref() into the pipe function.
§

fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
where Self: AsMut<U>, U: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.as_mut() into the pipe function.
§

fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
where Self: Deref<Target = T>, T: 'a + ?Sized, R: 'a,

Borrows self, then passes self.deref() into the pipe function.
§

fn pipe_deref_mut<'a, T, R>( &'a mut self, func: impl FnOnce(&'a mut T) -> R, ) -> R
where Self: DerefMut<Target = T> + Deref, T: 'a + ?Sized, R: 'a,

Mutably borrows self, then passes self.deref_mut() into the pipe function.
§

impl<'p, T> Seq<'p, T> for T
where T: Clone,

§

type Item<'a> = &'a T where T: 'a

The item yielded by the iterator.
§

type Iter<'a> = Once<&'a T> where T: 'a

An iterator over the items within this container, by reference.
§

fn seq_iter(&self) -> <T as Seq<'p, T>>::Iter<'_>

Iterate over the elements of the container.
§

fn contains(&self, val: &T) -> bool
where T: PartialEq,

Check whether an item is contained within this sequence.
§

fn to_maybe_ref<'b>(item: <T as Seq<'p, T>>::Item<'b>) -> Maybe<T, &'p T>
where 'p: 'b,

Convert an item of the sequence into a [MaybeRef].
§

impl<T> Tap for T

§

fn tap(self, func: impl FnOnce(&Self)) -> Self

Immutable access to a value. Read more
§

fn tap_mut(self, func: impl FnOnce(&mut Self)) -> Self

Mutable access to a value. Read more
§

fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
where Self: Borrow<B>, B: ?Sized,

Immutable access to the Borrow<B> of a value. Read more
§

fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
where Self: BorrowMut<B>, B: ?Sized,

Mutable access to the BorrowMut<B> of a value. Read more
§

fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
where Self: AsRef<R>, R: ?Sized,

Immutable access to the AsRef<R> view of a value. Read more
§

fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
where Self: AsMut<R>, R: ?Sized,

Mutable access to the AsMut<R> view of a value. Read more
§

fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
where Self: Deref<Target = T>, T: ?Sized,

Immutable access to the Deref::Target of a value. Read more
§

fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
where Self: DerefMut<Target = T> + Deref, T: ?Sized,

Mutable access to the Deref::Target of a value. Read more
§

fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self

Calls .tap() only in debug builds, and is erased in release builds.
§

fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self

Calls .tap_mut() only in debug builds, and is erased in release builds.
§

fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
where Self: Borrow<B>, B: ?Sized,

Calls .tap_borrow() only in debug builds, and is erased in release builds.
§

fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
where Self: BorrowMut<B>, B: ?Sized,

Calls .tap_borrow_mut() only in debug builds, and is erased in release builds.
§

fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
where Self: AsRef<R>, R: ?Sized,

Calls .tap_ref() only in debug builds, and is erased in release builds.
§

fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
where Self: AsMut<R>, R: ?Sized,

Calls .tap_ref_mut() only in debug builds, and is erased in release builds.
§

fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
where Self: Deref<Target = T>, T: ?Sized,

Calls .tap_deref() only in debug builds, and is erased in release builds.
§

fn tap_deref_mut_dbg<T>(self, func: impl FnOnce(&mut T)) -> Self
where Self: DerefMut<Target = T> + Deref, T: ?Sized,

Calls .tap_deref_mut() only in debug builds, and is erased in release builds.
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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
§

impl<T> TryConv for T

§

fn try_conv<T>(self) -> Result<T, Self::Error>
where Self: TryInto<T>,

Attempts to convert self into T using TryInto<T>. 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.
§

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

§

fn vzip(self) -> V

§

impl<T> OrderedSeq<'_, T> for T
where T: Clone,