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
Lexicon::parseparse a string.Lexicon::parse_multipleparse multiple strings simultaneously.Lexicon::generategenerate some of the strings of a grammar.Lexicon::valid_continuationsfind the valid next tokens of a grammar given a prefix
§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.
- Stabler, E. (2013). Two Models of Minimalist, Incremental Syntactic Analysis. Topics in Cognitive Science, 5(3), 611–633. https://doi.org/10.1111/tops.12031
Implementations§
Source§impl<T: Eq + Debug + Clone + SymbolCost, Category: Eq + Debug + Clone + Hash> Lexicon<T, Category>
impl<T: Eq + Debug + Clone + SymbolCost, Category: Eq + Debug + Clone + Hash> Lexicon<T, Category>
Sourcepub fn mdl_score_fixed_category_size(
&self,
n_phonemes: u16,
n_categories: u16,
) -> Result<f64, LexiconError>
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§impl<T, C> Lexicon<T, C>
impl<T, C> Lexicon<T, C>
Sourcepub fn uniform_distribution(&mut self)
pub fn uniform_distribution(&mut self)
Remap the weights of all lexical items to be the uniform distribution.
Sourcepub fn unify(&mut self, other: &Self) -> Vec<LexemeDetails>
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.
Sourcepub fn add_new_lexeme_from_sibling(
&mut self,
lemma: T,
rng: &mut impl Rng,
) -> Option<NewLexeme>
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.
Sourcepub fn add_new_lexeme(
&mut self,
lemma: Option<T>,
config: Option<LexicalProbConfig>,
rng: &mut impl Rng,
) -> Option<LexemeId>
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.
Sourcepub fn delete_lexeme(&mut self, lexeme: LexemeId) -> Result<(), MutationError>
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.
Sourcepub fn delete_from_node(&mut self, rng: &mut impl Rng)
pub fn delete_from_node(&mut self, rng: &mut impl Rng)
Delets a random branch of the grammar
Sourcepub fn delete_node(&mut self, rng: &mut impl Rng) -> Option<LexemeId>
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.
Sourcepub fn random(
base_category: &C,
lemmas: &[T],
config: Option<LexicalProbConfig>,
rng: &mut impl Rng,
) -> Self
pub fn random( base_category: &C, lemmas: &[T], config: Option<LexicalProbConfig>, rng: &mut impl Rng, ) -> Self
Samples an entirely random grammar
Sourcepub fn change_feature(
&mut self,
lemmas: &[T],
config: Option<LexicalProbConfig>,
rng: &mut impl Rng,
)
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.
Sourcepub fn resample_below_node(
&mut self,
lemmas: &[T],
config: Option<LexicalProbConfig>,
rng: &mut impl Rng,
)
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: Eq, Category: Eq> Lexicon<T, Category>
impl<T: Eq, Category: Eq> Lexicon<T, Category>
Sourcepub fn leaf_to_features<'a>(
&'a self,
lexeme: LexemeId,
) -> Option<Climber<'a, T, Category>>
pub fn leaf_to_features<'a>( &'a self, lexeme: LexemeId, ) -> Option<Climber<'a, T, Category>>
Climb up a lexeme over all of its features
Sourcepub fn leaf_to_lemma(&self, lexeme_id: LexemeId) -> Option<&Option<T>>
pub fn leaf_to_lemma(&self, lexeme_id: LexemeId) -> Option<&Option<T>>
Gets the lemma of a lexeme.
Source§impl<T: Eq + Debug + Clone, Category: Eq + Debug + Clone> Lexicon<T, Category>
impl<T: Eq + Debug + Clone, Category: Eq + Debug + Clone> Lexicon<T, Category>
Sourcepub fn is_complement(&self, nx: NodeIndex) -> bool
pub fn is_complement(&self, nx: NodeIndex) -> bool
Checks if nx is a complement
Sourcepub fn sibling_leaves(&self) -> Vec<Vec<LexemeId>>
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)
Sourcepub fn lexemes_and_ids(
&self,
) -> Result<impl Iterator<Item = (LexemeId, LexicalEntry<T, Category>)>, LexiconError>
pub fn lexemes_and_ids( &self, ) -> Result<impl Iterator<Item = (LexemeId, LexicalEntry<T, Category>)>, LexiconError>
Gets each lexical entry along with its ID.
Sourcepub fn lexemes(&self) -> Result<Vec<LexicalEntry<T, Category>>, LexiconError>
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.
Sourcepub fn get_lexical_entry(
&self,
lexeme_id: LexemeId,
) -> Result<LexicalEntry<T, Category>, LexiconError>
pub fn get_lexical_entry( &self, lexeme_id: LexemeId, ) -> Result<LexicalEntry<T, Category>, LexiconError>
Given a leaf node, return its LexicalEntry
Sourcepub fn new(items: Vec<LexicalEntry<T, Category>>, collapse_lemmas: bool) -> Self
pub fn new(items: Vec<LexicalEntry<T, Category>>, collapse_lemmas: bool) -> Self
Create a new grammar from a Vec of LexicalEntry
Sourcepub fn new_with_weights(
items: Vec<LexicalEntry<T, Category>>,
weights: Vec<f64>,
collapse_lemmas: bool,
) -> Self
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
Sourcepub fn categories(&self) -> impl Iterator<Item = &Category>
pub fn categories(&self) -> impl Iterator<Item = &Category>
Iterate over all categories of a grammar
Sourcepub fn licensor_types(&self) -> impl Iterator<Item = &Category>
pub fn licensor_types(&self) -> impl Iterator<Item = &Category>
Iterate over all licensors of a grammar
Source§impl<'src> Lexicon<&'src str, &'src str>
impl<'src> Lexicon<&'src str, &'src str>
Sourcepub fn from_string(s: &'src str) -> Result<Self, LexiconParsingError<'src>>
pub fn from_string(s: &'src str) -> Result<Self, LexiconParsingError<'src>>
Parse a grammar and return a Lexicon<&str, &str>
Source§impl<'a> Lexicon<&'a str, &'a str>
impl<'a> Lexicon<&'a str, &'a str>
Sourcepub fn to_owned_values(&self) -> Lexicon<String, String>
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>
impl<T, C> Lexicon<T, C>
Sourcepub fn valid_continuations(
&self,
initial_category: C,
prefix: &[PhonContent<T>],
config: &ParsingConfig,
) -> Result<HashSet<Continuation<T>>, ParsingError<C>>
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>
impl<T: Eq + Clone, C: Eq + Clone + Display> Lexicon<T, C>
Sourcepub fn derivation(&self, rules: RulePool) -> Derivation<'static, T, C>
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>
impl<T, Category> Lexicon<T, Category>
Sourcepub fn generate(
&self,
category: Category,
config: &ParsingConfig,
) -> Result<Generator<&Self, T, Category>, ParsingError<Category>>
pub fn generate( &self, category: Category, config: &ParsingConfig, ) -> Result<Generator<&Self, T, Category>, ParsingError<Category>>
Sourcepub fn into_generate(
self,
category: Category,
config: &ParsingConfig,
) -> Result<Generator<Self, T, Category>, ParsingError<Category>>
pub fn into_generate( self, category: Category, config: &ParsingConfig, ) -> Result<Generator<Self, T, Category>, ParsingError<Category>>
Like Lexicon::generate but consumes the lexicon.
Sourcepub fn parse<'a, 'b>(
&'a self,
s: &'b [PhonContent<T>],
category: Category,
config: &'a ParsingConfig,
) -> Result<Parser<'a, 'b, T, Category>, ParsingError<Category>>
pub fn parse<'a, 'b>( &'a self, s: &'b [PhonContent<T>], category: Category, config: &'a ParsingConfig, ) -> Result<Parser<'a, 'b, T, Category>, ParsingError<Category>>
Sourcepub 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>]>,
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.
Sourcepub 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>]>,
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: Eq, C: Eq> From<SemanticLexicon<'_, T, C>> for Lexicon<T, C>
impl<T: Eq, C: Eq> From<SemanticLexicon<'_, T, C>> for Lexicon<T, C>
Source§fn from(value: SemanticLexicon<'_, T, C>) -> Self
fn from(value: SemanticLexicon<'_, T, C>) -> Self
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>
impl<T, Category> Sync for Lexicon<T, Category>
impl<T, Category> Unpin for Lexicon<T, Category>
impl<T, Category> UnwindSafe for Lexicon<T, Category>where
Category: UnwindSafe,
T: UnwindSafe,
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> Conv for T
impl<T> Conv for T
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.§impl<T> FmtForward for T
impl<T> FmtForward for T
§fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
fn fmt_binary(self) -> FmtBinary<Self>where
Self: Binary,
self to use its Binary implementation when Debug-formatted.§fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
fn fmt_display(self) -> FmtDisplay<Self>where
Self: Display,
self to use its Display implementation when
Debug-formatted.§fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
fn fmt_lower_exp(self) -> FmtLowerExp<Self>where
Self: LowerExp,
self to use its LowerExp implementation when
Debug-formatted.§fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
fn fmt_lower_hex(self) -> FmtLowerHex<Self>where
Self: LowerHex,
self to use its LowerHex implementation when
Debug-formatted.§fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
fn fmt_octal(self) -> FmtOctal<Self>where
Self: Octal,
self to use its Octal implementation when Debug-formatted.§fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
fn fmt_pointer(self) -> FmtPointer<Self>where
Self: Pointer,
self to use its Pointer implementation when
Debug-formatted.§fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
fn fmt_upper_exp(self) -> FmtUpperExp<Self>where
Self: UpperExp,
self to use its UpperExp implementation when
Debug-formatted.§fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
fn fmt_upper_hex(self) -> FmtUpperHex<Self>where
Self: UpperHex,
self to use its UpperHex implementation when
Debug-formatted.§fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
fn fmt_list(self) -> FmtList<Self>where
&'a Self: for<'a> IntoIterator,
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§impl<'src, T> IntoMaybe<'src, T> for Twhere
T: 'src,
impl<'src, T> IntoMaybe<'src, T> for Twhere
T: 'src,
§impl<T> Pipe for Twhere
T: ?Sized,
impl<T> Pipe for Twhere
T: ?Sized,
§fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
fn pipe<R>(self, func: impl FnOnce(Self) -> R) -> Rwhere
Self: Sized,
§fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
fn pipe_ref<'a, R>(&'a self, func: impl FnOnce(&'a Self) -> R) -> Rwhere
R: 'a,
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) -> Rwhere
R: 'a,
fn pipe_ref_mut<'a, R>(&'a mut self, func: impl FnOnce(&'a mut Self) -> R) -> Rwhere
R: 'a,
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
fn pipe_borrow<'a, B, R>(&'a self, func: impl FnOnce(&'a B) -> R) -> R
§fn pipe_borrow_mut<'a, B, R>(
&'a mut self,
func: impl FnOnce(&'a mut B) -> R,
) -> R
fn pipe_borrow_mut<'a, B, R>( &'a mut self, func: impl FnOnce(&'a mut B) -> R, ) -> R
§fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
fn pipe_as_ref<'a, U, R>(&'a self, func: impl FnOnce(&'a U) -> R) -> R
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
fn pipe_as_mut<'a, U, R>(&'a mut self, func: impl FnOnce(&'a mut U) -> R) -> R
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
fn pipe_deref<'a, T, R>(&'a self, func: impl FnOnce(&'a T) -> R) -> R
self, then passes self.deref() into the pipe function.§impl<'p, T> Seq<'p, T> for Twhere
T: Clone,
impl<'p, T> Seq<'p, T> for Twhere
T: Clone,
§type Iter<'a> = Once<&'a T>
where
T: 'a
type Iter<'a> = Once<&'a T> where T: 'a
§fn contains(&self, val: &T) -> boolwhere
T: PartialEq,
fn contains(&self, val: &T) -> boolwhere
T: PartialEq,
§fn to_maybe_ref<'b>(item: <T as Seq<'p, T>>::Item<'b>) -> Maybe<T, &'p T>where
'p: 'b,
fn to_maybe_ref<'b>(item: <T as Seq<'p, T>>::Item<'b>) -> Maybe<T, &'p T>where
'p: 'b,
MaybeRef].§impl<T> Tap for T
impl<T> Tap for T
§fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow<B>(self, func: impl FnOnce(&B)) -> Self
Borrow<B> of a value. Read more§fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
fn tap_borrow_mut<B>(self, func: impl FnOnce(&mut B)) -> Self
BorrowMut<B> of a value. Read more§fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref<R>(self, func: impl FnOnce(&R)) -> Self
AsRef<R> view of a value. Read more§fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
fn tap_ref_mut<R>(self, func: impl FnOnce(&mut R)) -> Self
AsMut<R> view of a value. Read more§fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref<T>(self, func: impl FnOnce(&T)) -> Self
Deref::Target of a value. Read more§fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
fn tap_deref_mut<T>(self, func: impl FnOnce(&mut T)) -> Self
Deref::Target of a value. Read more§fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
fn tap_dbg(self, func: impl FnOnce(&Self)) -> Self
.tap() only in debug builds, and is erased in release builds.§fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
fn tap_mut_dbg(self, func: impl FnOnce(&mut Self)) -> Self
.tap_mut() only in debug builds, and is erased in release
builds.§fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
fn tap_borrow_dbg<B>(self, func: impl FnOnce(&B)) -> Self
.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
fn tap_borrow_mut_dbg<B>(self, func: impl FnOnce(&mut B)) -> Self
.tap_borrow_mut() only in debug builds, and is erased in release
builds.§fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
fn tap_ref_dbg<R>(self, func: impl FnOnce(&R)) -> Self
.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
fn tap_ref_mut_dbg<R>(self, func: impl FnOnce(&mut R)) -> Self
.tap_ref_mut() only in debug builds, and is erased in release
builds.§fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
fn tap_deref_dbg<T>(self, func: impl FnOnce(&T)) -> Self
.tap_deref() only in debug builds, and is erased in release
builds.