Fleet  0.0.9
Inference in the LOT
Classes | Public Types | Public Member Functions | Static Public Member Functions | Public Attributes | Static Public Attributes | List of all members
Grammar< _input_t, _output_t, GRAMMAR_TYPES > Class Template Reference

#include <Grammar.h>

Classes

class  RuleIterator
 

Public Types

using input_t = _input_t
 
using output_t = _output_t
 
using this_t = Grammar< input_t, output_t, GRAMMAR_TYPES... >
 
using TypeTuple = std::tuple< GRAMMAR_TYPES... >
 
using VirtualMachineState_t = VirtualMachineState< input_t, output_t, GRAMMAR_TYPES... >
 
using FT = typename VirtualMachineState_t::FT
 

Public Member Functions

 Grammar ()
 
 Grammar (const Grammar &g)=delete
 
 Grammar (const Grammar &&g)=delete
 
RuleIterator begin () const
 
RuleIterator end () const
 
constexpr nonterminal_t start ()
 The start nonterminal type. More...
 
constexpr size_t count_nonterminals () const
 
size_t count_rules (const nonterminal_t nt) const
 
size_t count_rules () const
 
size_t count_terminals (nonterminal_t nt) const
 
size_t count_nonterminals (nonterminal_t nt) const
 
template<typename T , typename... args>
void add_vms (std::string fmt, FT *f, double p=1.0, Op o=Op::Standard, int a=0)
 
template<typename T , typename... args>
void add (std::string fmt, Primitive< T, args... > &b, double p=1.0, int a=0)
 
template<typename T , typename... args>
void add (std::string fmt, std::function< T(args...)> f, double p=1.0, Op o=Op::Standard, int a=0)
 
template<typename T , typename... args>
void add (std::string fmt, T(*_f)(args...), double p=1.0, Op o=Op::Standard, int a=0)
 
template<typename T >
void add_terminal (std::string fmt, T x, double p=1.0, Op o=Op::Standard, int a=0)
 Add a variable that is NOT A function – simplification for adding alphabets etc. This just wraps stuff in a thunk for us to make it easier. NOTE: x is copied by value. More...
 
void remove_all (nonterminal_t nt)
 Remove all the nonterminals of this type from the grammar. NOTE: This is generally a really bad idea unless you know what you are doing. More...
 
size_t get_index_of (const Rule *r) const
 
virtual Ruleget_rule (const nonterminal_t nt, size_t k) const
 
virtual Ruleget_rule (const nonterminal_t nt, const Op o, const int a=0)
 
virtual Ruleget_rule (const nonterminal_t nt, size_t i)
 
virtual Ruleget_rule (const nonterminal_t nt, const std::string s) const
 
virtual Ruleget_rule (const std::string s) const
 
double rule_normalizer (const nonterminal_t nt) const
 
virtual Rulesample_rule (const nonterminal_t nt) const
 
Node makeNode (const Rule *r) const
 
Node generate (const nonterminal_t ntfrom=nt< output_t >(), unsigned long depth=0) const
 
Node copy_resample (const Node &node, bool f(const Node &n)) const
 
std::vector< size_t > get_cumulative_indices () const
 
std::vector< size_t > get_counts (const Node &node) const
 
template<typename T >
std::vector< size_t > get_counts (const std::vector< T > &v) const
 Compute a vector of counts of how often each rule was used, in a standard order given by iterating over nts and then iterating over rules. More...
 
template<typename K , typename T >
std::vector< size_t > get_counts (const std::map< K, T > &v) const
 
double log_probability (const Node &n) const
 
Node from_parseable (std::deque< std::string > &q) const
 
Node from_parseable (std::string s) const
 
Node from_parseable (const char *c) const
 
size_t neighbors (const Node &node) const
 
void expand_to_neighbor (Node &node, int &which)
 
double neighbor_prior (const Node &node, int &which) const
 
void complete (Node &node)
 
std::tuple< int, std::vector< int >, int > find_open_commas_close (const std::string s)
 
Node simple_parse (std::string s)
 Very simple parsing routine that takes a string like "and(not(or(eq_pos(pos(parent(x)),'NP-POSS'),eq_pos('NP-S',pos(x)))),corefers(x))" (from the Binding example) and parses it into a Node. More...
 

Static Public Member Functions

template<class T >
static constexpr nonterminal_t nt ()
 
template<typename X >
static constexpr bool is_in_GRAMMAR_TYPES ()
 For a given nt, returns the number of finite trees that nt can expand to if its finite; 0 if its infinite. *. More...
 

Public Attributes

std::vector< Rulerules [N_NTs]
 
std::array< double, N_NTsZ
 
size_t GRAMMAR_MAX_DEPTH = 64
 

Static Public Attributes

static constexpr size_t N_NTs = std::tuple_size<TypeTuple>::value
 

Detailed Description

template<typename _input_t, typename _output_t, typename... GRAMMAR_TYPES>
class Grammar< _input_t, _output_t, GRAMMAR_TYPES >

Author
Steven Piantadosi
Date
05/09/20

Member Typedef Documentation

◆ FT

template<typename _input_t, typename _output_t, typename... GRAMMAR_TYPES>
using Grammar< _input_t, _output_t, GRAMMAR_TYPES >::FT = typename VirtualMachineState_t::FT

◆ input_t

template<typename _input_t, typename _output_t, typename... GRAMMAR_TYPES>
using Grammar< _input_t, _output_t, GRAMMAR_TYPES >::input_t = _input_t

◆ output_t

template<typename _input_t, typename _output_t, typename... GRAMMAR_TYPES>
using Grammar< _input_t, _output_t, GRAMMAR_TYPES >::output_t = _output_t

◆ this_t

template<typename _input_t, typename _output_t, typename... GRAMMAR_TYPES>
using Grammar< _input_t, _output_t, GRAMMAR_TYPES >::this_t = Grammar<input_t, output_t, GRAMMAR_TYPES...>

◆ TypeTuple

template<typename _input_t, typename _output_t, typename... GRAMMAR_TYPES>
using Grammar< _input_t, _output_t, GRAMMAR_TYPES >::TypeTuple = std::tuple<GRAMMAR_TYPES...>

◆ VirtualMachineState_t

template<typename _input_t, typename _output_t, typename... GRAMMAR_TYPES>
using Grammar< _input_t, _output_t, GRAMMAR_TYPES >::VirtualMachineState_t = VirtualMachineState<input_t, output_t, GRAMMAR_TYPES...>

Constructor & Destructor Documentation

◆ Grammar() [1/3]

template<typename _input_t, typename _output_t, typename... GRAMMAR_TYPES>
Grammar< _input_t, _output_t, GRAMMAR_TYPES >::Grammar ( )
inline

◆ Grammar() [2/3]

template<typename _input_t, typename _output_t, typename... GRAMMAR_TYPES>
Grammar< _input_t, _output_t, GRAMMAR_TYPES >::Grammar ( const Grammar< _input_t, _output_t, GRAMMAR_TYPES > &  g)
delete

◆ Grammar() [3/3]

template<typename _input_t, typename _output_t, typename... GRAMMAR_TYPES>
Grammar< _input_t, _output_t, GRAMMAR_TYPES >::Grammar ( const Grammar< _input_t, _output_t, GRAMMAR_TYPES > &&  g)
delete

Member Function Documentation

◆ add() [1/3]

template<typename _input_t, typename _output_t, typename... GRAMMAR_TYPES>
template<typename T , typename... args>
void Grammar< _input_t, _output_t, GRAMMAR_TYPES >::add ( std::string  fmt,
Primitive< T, args... > &  b,
double  p = 1.0,
int  a = 0 
)
inline
Parameters
fmt
b
p

◆ add() [2/3]

template<typename _input_t, typename _output_t, typename... GRAMMAR_TYPES>
template<typename T , typename... args>
void Grammar< _input_t, _output_t, GRAMMAR_TYPES >::add ( std::string  fmt,
std::function< T(args...)>  f,
double  p = 1.0,
Op  o = Op::Standard,
int  a = 0 
)
inline
Parameters
fmt
f
p
o

◆ add() [3/3]

template<typename _input_t, typename _output_t, typename... GRAMMAR_TYPES>
template<typename T , typename... args>
void Grammar< _input_t, _output_t, GRAMMAR_TYPES >::add ( std::string  fmt,
T(*)(args...)  _f,
double  p = 1.0,
Op  o = Op::Standard,
int  a = 0 
)
inline
Parameters
fmt
p
o

◆ add_terminal()

template<typename _input_t, typename _output_t, typename... GRAMMAR_TYPES>
template<typename T >
void Grammar< _input_t, _output_t, GRAMMAR_TYPES >::add_terminal ( std::string  fmt,
x,
double  p = 1.0,
Op  o = Op::Standard,
int  a = 0 
)
inline

Add a variable that is NOT A function – simplification for adding alphabets etc. This just wraps stuff in a thunk for us to make it easier. NOTE: x is copied by value.

Parameters
fmt
x
p
o
a

◆ add_vms()

template<typename _input_t, typename _output_t, typename... GRAMMAR_TYPES>
template<typename T , typename... args>
void Grammar< _input_t, _output_t, GRAMMAR_TYPES >::add_vms ( std::string  fmt,
FT f,
double  p = 1.0,
Op  o = Op::Standard,
int  a = 0 
)
inline
Parameters
fmt
f
p
o

◆ begin()

template<typename _input_t, typename _output_t, typename... GRAMMAR_TYPES>
RuleIterator Grammar< _input_t, _output_t, GRAMMAR_TYPES >::begin ( ) const
inline

◆ complete()

template<typename _input_t, typename _output_t, typename... GRAMMAR_TYPES>
void Grammar< _input_t, _output_t, GRAMMAR_TYPES >::complete ( Node node)
inline

◆ copy_resample()

template<typename _input_t, typename _output_t, typename... GRAMMAR_TYPES>
Node Grammar< _input_t, _output_t, GRAMMAR_TYPES >::copy_resample ( const Node node,
bool   fconst Node &n 
) const
inline

Make a copy of node where all nodes satisfying f are regenerated from the grammar.

Parameters
node
f- a function saying what we should resample
Returns
NOTE: this does NOT allow f to apply to nullptr children (so cannot be used to fill in)

◆ count_nonterminals() [1/2]

template<typename _input_t, typename _output_t, typename... GRAMMAR_TYPES>
constexpr size_t Grammar< _input_t, _output_t, GRAMMAR_TYPES >::count_nonterminals ( ) const
inline

How many nonterminals are there in the grammar.

Returns

◆ count_nonterminals() [2/2]

template<typename _input_t, typename _output_t, typename... GRAMMAR_TYPES>
size_t Grammar< _input_t, _output_t, GRAMMAR_TYPES >::count_nonterminals ( nonterminal_t  nt) const
inline

Count th enumber of non-terminal rules of return type nt

Parameters
nt
Returns

◆ count_rules() [1/2]

template<typename _input_t, typename _output_t, typename... GRAMMAR_TYPES>
size_t Grammar< _input_t, _output_t, GRAMMAR_TYPES >::count_rules ( const nonterminal_t  nt) const
inline

Returns the number of rules of return type nt

Parameters
nt
Returns

◆ count_rules() [2/2]

template<typename _input_t, typename _output_t, typename... GRAMMAR_TYPES>
size_t Grammar< _input_t, _output_t, GRAMMAR_TYPES >::count_rules ( ) const
inline

Total number of rules

Returns

◆ count_terminals()

template<typename _input_t, typename _output_t, typename... GRAMMAR_TYPES>
size_t Grammar< _input_t, _output_t, GRAMMAR_TYPES >::count_terminals ( nonterminal_t  nt) const
inline

Count the number of terminal rules of return type nt

Parameters
nt
Returns

◆ end()

template<typename _input_t, typename _output_t, typename... GRAMMAR_TYPES>
RuleIterator Grammar< _input_t, _output_t, GRAMMAR_TYPES >::end ( ) const
inline

◆ expand_to_neighbor()

template<typename _input_t, typename _output_t, typename... GRAMMAR_TYPES>
void Grammar< _input_t, _output_t, GRAMMAR_TYPES >::expand_to_neighbor ( Node node,
int &  which 
)
inline

◆ find_open_commas_close()

template<typename _input_t, typename _output_t, typename... GRAMMAR_TYPES>
std::tuple<int,std::vector<int>,int> Grammar< _input_t, _output_t, GRAMMAR_TYPES >::find_open_commas_close ( const std::string  s)
inline

◆ from_parseable() [1/3]

template<typename _input_t, typename _output_t, typename... GRAMMAR_TYPES>
Node Grammar< _input_t, _output_t, GRAMMAR_TYPES >::from_parseable ( std::deque< std::string > &  q) const
inline

Fills an entire tree using the string format prefixes – see get_rule(std::string). Here q should contain strings like "3:'a'" which says expand nonterminal type 3 to the rule matching 'a' NOTE: This is not a Serializable interface because the Node needs to know which grammar

Parameters
q
Returns

◆ from_parseable() [2/3]

template<typename _input_t, typename _output_t, typename... GRAMMAR_TYPES>
Node Grammar< _input_t, _output_t, GRAMMAR_TYPES >::from_parseable ( std::string  s) const
inline

Expand from names where s is delimited by ':'

Parameters
s
Returns

◆ from_parseable() [3/3]

template<typename _input_t, typename _output_t, typename... GRAMMAR_TYPES>
Node Grammar< _input_t, _output_t, GRAMMAR_TYPES >::from_parseable ( const char *  c) const
inline

◆ generate()

template<typename _input_t, typename _output_t, typename... GRAMMAR_TYPES>
Node Grammar< _input_t, _output_t, GRAMMAR_TYPES >::generate ( const nonterminal_t  ntfrom = nt<output_t>(),
unsigned long  depth = 0 
) const
inline

Sample an entire tree from this grammar (keeping track of depth in case we recurse too far) of return type nt. This samples a rule, makes them with makeNode, and then recurses.

Parameters
nt
depth
Returns
Returns a Node sampled from the grammar.

NOTE: this may throw a if the grammar recurses too far (usually that means the grammar is improper)

◆ get_counts() [1/3]

template<typename _input_t, typename _output_t, typename... GRAMMAR_TYPES>
std::vector<size_t> Grammar< _input_t, _output_t, GRAMMAR_TYPES >::get_counts ( const Node node) const
inline

Compute a vector of counts of how often each rule was used, in a standard order given by iterating over nts and then iterating over rules

Parameters
node
Returns

◆ get_counts() [2/3]

template<typename _input_t, typename _output_t, typename... GRAMMAR_TYPES>
template<typename T >
std::vector<size_t> Grammar< _input_t, _output_t, GRAMMAR_TYPES >::get_counts ( const std::vector< T > &  v) const
inline

Compute a vector of counts of how often each rule was used, in a standard order given by iterating over nts and then iterating over rules.

Parameters
v
Returns

◆ get_counts() [3/3]

template<typename _input_t, typename _output_t, typename... GRAMMAR_TYPES>
template<typename K , typename T >
std::vector<size_t> Grammar< _input_t, _output_t, GRAMMAR_TYPES >::get_counts ( const std::map< K, T > &  v) const
inline

◆ get_cumulative_indices()

template<typename _input_t, typename _output_t, typename... GRAMMAR_TYPES>
std::vector<size_t> Grammar< _input_t, _output_t, GRAMMAR_TYPES >::get_cumulative_indices ( ) const
inline

◆ get_index_of()

template<typename _input_t, typename _output_t, typename... GRAMMAR_TYPES>
size_t Grammar< _input_t, _output_t, GRAMMAR_TYPES >::get_index_of ( const Rule r) const
inline

Find the index in rules of where r is.

Parameters
r
Returns

◆ get_rule() [1/5]

template<typename _input_t, typename _output_t, typename... GRAMMAR_TYPES>
virtual Rule* Grammar< _input_t, _output_t, GRAMMAR_TYPES >::get_rule ( const nonterminal_t  nt,
size_t  k 
) const
inlinevirtual

Get the k'th rule of type nt

Parameters
nt
k
Returns

◆ get_rule() [2/5]

template<typename _input_t, typename _output_t, typename... GRAMMAR_TYPES>
virtual Rule* Grammar< _input_t, _output_t, GRAMMAR_TYPES >::get_rule ( const nonterminal_t  nt,
const Op  o,
const int  a = 0 
)
inlinevirtual

Get rule of type nt with a given BuiltinOp and argument a

Parameters
nt
o
a
Returns

◆ get_rule() [3/5]

template<typename _input_t, typename _output_t, typename... GRAMMAR_TYPES>
virtual Rule* Grammar< _input_t, _output_t, GRAMMAR_TYPES >::get_rule ( const nonterminal_t  nt,
size_t  i 
)
inlinevirtual

◆ get_rule() [4/5]

template<typename _input_t, typename _output_t, typename... GRAMMAR_TYPES>
virtual Rule* Grammar< _input_t, _output_t, GRAMMAR_TYPES >::get_rule ( const nonterminal_t  nt,
const std::string  s 
) const
inlinevirtual

Return a rule based on s, which must uniquely be a prefix of the rule's format of a given nonterminal type. If s is the empty string, however, it must match exactly.

Parameters
s
Returns

◆ get_rule() [5/5]

template<typename _input_t, typename _output_t, typename... GRAMMAR_TYPES>
virtual Rule* Grammar< _input_t, _output_t, GRAMMAR_TYPES >::get_rule ( const std::string  s) const
inlinevirtual

Return a rule based on s, which must uniquely be a prefix of the rule's format. If s is the empty string, however, it must match exactly.

Parameters
s
Returns

◆ is_in_GRAMMAR_TYPES()

template<typename _input_t, typename _output_t, typename... GRAMMAR_TYPES>
template<typename X >
static constexpr bool Grammar< _input_t, _output_t, GRAMMAR_TYPES >::is_in_GRAMMAR_TYPES ( )
inlinestatic

For a given nt, returns the number of finite trees that nt can expand to if its finite; 0 if its infinite. *.

Parameters
nt- the type of this nonterminal

◆ log_probability()

template<typename _input_t, typename _output_t, typename... GRAMMAR_TYPES>
double Grammar< _input_t, _output_t, GRAMMAR_TYPES >::log_probability ( const Node n) const
inline

Compute the log probability of a tree according to the grammar. NOTE: here we ignore nodes that are Null meaning that we compute the partial probability

Parameters
n
Returns

◆ makeNode()

template<typename _input_t, typename _output_t, typename... GRAMMAR_TYPES>
Node Grammar< _input_t, _output_t, GRAMMAR_TYPES >::makeNode ( const Rule r) const
inline

Helper function to create a node according to this grammar. This is how nodes get their log probabilities.

Parameters
r
Returns

◆ neighbor_prior()

template<typename _input_t, typename _output_t, typename... GRAMMAR_TYPES>
double Grammar< _input_t, _output_t, GRAMMAR_TYPES >::neighbor_prior ( const Node node,
int &  which 
) const
inline

◆ neighbors()

template<typename _input_t, typename _output_t, typename... GRAMMAR_TYPES>
size_t Grammar< _input_t, _output_t, GRAMMAR_TYPES >::neighbors ( const Node node) const
inline

◆ nt()

template<typename _input_t, typename _output_t, typename... GRAMMAR_TYPES>
template<class T >
static constexpr nonterminal_t Grammar< _input_t, _output_t, GRAMMAR_TYPES >::nt ( )
inlinestatic

◆ remove_all()

template<typename _input_t, typename _output_t, typename... GRAMMAR_TYPES>
void Grammar< _input_t, _output_t, GRAMMAR_TYPES >::remove_all ( nonterminal_t  nt)
inline

Remove all the nonterminals of this type from the grammar. NOTE: This is generally a really bad idea unless you know what you are doing.

Parameters
nt

◆ rule_normalizer()

template<typename _input_t, typename _output_t, typename... GRAMMAR_TYPES>
double Grammar< _input_t, _output_t, GRAMMAR_TYPES >::rule_normalizer ( const nonterminal_t  nt) const
inline

Return the normalizing constant (NOT log) for all rules of type nt

Parameters
nt
Returns

◆ sample_rule()

template<typename _input_t, typename _output_t, typename... GRAMMAR_TYPES>
virtual Rule* Grammar< _input_t, _output_t, GRAMMAR_TYPES >::sample_rule ( const nonterminal_t  nt) const
inlinevirtual

Randomly sample a rule of type nt.

Parameters
nt
Returns

◆ simple_parse()

template<typename _input_t, typename _output_t, typename... GRAMMAR_TYPES>
Node Grammar< _input_t, _output_t, GRAMMAR_TYPES >::simple_parse ( std::string  s)
inline

Very simple parsing routine that takes a string like "and(not(or(eq_pos(pos(parent(x)),'NP-POSS'),eq_pos('NP-S',pos(x)))),corefers(x))" (from the Binding example) and parses it into a Node.

NOTE: The resulting Node must then be put into a LOTHypothesis or Lexicon. NOTE: This is not very robust or debugged. Just a quick ipmlementation.

Parameters
s
Returns

◆ start()

template<typename _input_t, typename _output_t, typename... GRAMMAR_TYPES>
constexpr nonterminal_t Grammar< _input_t, _output_t, GRAMMAR_TYPES >::start ( )
inline

The start nonterminal type.

Member Data Documentation

◆ GRAMMAR_MAX_DEPTH

template<typename _input_t, typename _output_t, typename... GRAMMAR_TYPES>
size_t Grammar< _input_t, _output_t, GRAMMAR_TYPES >::GRAMMAR_MAX_DEPTH = 64

◆ N_NTs

template<typename _input_t, typename _output_t, typename... GRAMMAR_TYPES>
constexpr size_t Grammar< _input_t, _output_t, GRAMMAR_TYPES >::N_NTs = std::tuple_size<TypeTuple>::value
static

◆ rules

template<typename _input_t, typename _output_t, typename... GRAMMAR_TYPES>
std::vector<Rule> Grammar< _input_t, _output_t, GRAMMAR_TYPES >::rules[N_NTs]

◆ Z

template<typename _input_t, typename _output_t, typename... GRAMMAR_TYPES>
std::array<double,N_NTs> Grammar< _input_t, _output_t, GRAMMAR_TYPES >::Z

The documentation for this class was generated from the following file: