![]() |
Fleet
0.0.9
Inference in the LOT
|
#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 |
void | change_probability (const std::string &s, const double newp) |
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 Rule * | get_rule (const nonterminal_t nt, size_t k) const |
virtual Rule * | get_rule (const nonterminal_t nt, const Op o, const int a=0) |
virtual Rule * | get_rule (const nonterminal_t nt, size_t i) |
virtual Rule * | get_rule (const nonterminal_t nt, const std::string s) const |
virtual Rule * | get_rule (const std::string s) const |
double | rule_normalizer (const nonterminal_t nt) const |
virtual Rule * | sample_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 | generate (const nonterminal_t ntfrom=nt< output_t >(), unsigned long depth=0) const |
A wrapper to catch DepthExcpetions and retry. This means that defaultly we try to generate GENERATE_DEPTH_EXCEPTION_RETRIES times and if ALL of them fail, we throw an assert error. Presumably, unless the grammar is terrible one of them will work. This makes our DepthExceptions generally silent to the outside since they won't call __generate typically. More... | |
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 |
This computes the expected length of productions from this grammar, counting terminals and nonterminals as 1 each NOTE: We can do this smarter with a matrix eigenvalues, but its a bit less flexible/extensible. More... | |
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< Rule > | rules [N_NTs] |
std::array< double, N_NTs > | Z |
size_t | GRAMMAR_MAX_DEPTH = 64 |
Static Public Attributes | |
static constexpr size_t | N_NTs = std::tuple_size<TypeTuple>::value |
static const size_t | GENERATE_DEPTH_EXCEPTION_RETRIES = 1000 |
using Grammar< _input_t, _output_t, GRAMMAR_TYPES >::FT = typename VirtualMachineState_t::FT |
using Grammar< _input_t, _output_t, GRAMMAR_TYPES >::input_t = _input_t |
using Grammar< _input_t, _output_t, GRAMMAR_TYPES >::output_t = _output_t |
using Grammar< _input_t, _output_t, GRAMMAR_TYPES >::this_t = Grammar<input_t, output_t, GRAMMAR_TYPES...> |
using Grammar< _input_t, _output_t, GRAMMAR_TYPES >::TypeTuple = std::tuple<GRAMMAR_TYPES...> |
using Grammar< _input_t, _output_t, GRAMMAR_TYPES >::VirtualMachineState_t = VirtualMachineState<input_t, output_t, GRAMMAR_TYPES...> |
|
inline |
|
delete |
|
delete |
|
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.
nt | |
depth |
NOTE: this may throw a if the grammar recurses too far (usually that means the grammar is improper)
|
inline |
fmt | |
b | |
p |
|
inline |
fmt | |
f | |
p | |
o |
|
inline |
fmt | |
p | |
o |
|
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.
fmt | |
x | |
p | |
o | |
a |
|
inline |
fmt | |
f | |
p | |
o |
|
inline |
|
inline |
|
inline |
|
inline |
Make a copy of node where all nodes satisfying f are regenerated from the grammar.
node | |
f | - a function saying what we should resample |
|
inline |
How many nonterminals are there in the grammar.
|
inline |
Count th enumber of non-terminal rules of return type nt
nt |
|
inline |
Returns the number of rules of return type nt
nt |
|
inline |
Total number of rules
|
inline |
Count the number of terminal rules of return type nt
nt |
|
inline |
|
inline |
|
inline |
|
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
q |
|
inline |
Expand from names where s is delimited by ':'
s |
|
inline |
|
inline |
A wrapper to catch DepthExcpetions and retry. This means that defaultly we try to generate GENERATE_DEPTH_EXCEPTION_RETRIES times and if ALL of them fail, we throw an assert error. Presumably, unless the grammar is terrible one of them will work. This makes our DepthExceptions generally silent to the outside since they won't call __generate typically.
ntfrom | |
depth |
|
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
node |
|
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.
v |
|
inline |
|
inline |
|
inline |
Find the index in rules of where r is.
r |
|
inlinevirtual |
Get the k'th rule of type nt
nt | |
k |
|
inlinevirtual |
Get rule of type nt with a given BuiltinOp and argument a
nt | |
o | |
a |
|
inlinevirtual |
|
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.
s |
|
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.
s |
|
inlinestatic |
For a given nt, returns the number of finite trees that nt can expand to if its finite; 0 if its infinite. *.
nt | - the type of this nonterminal |
|
inline |
This computes the expected length of productions from this grammar, counting terminals and nonterminals as 1 each NOTE: We can do this smarter with a matrix eigenvalues, but its a bit less flexible/extensible.
max_depth |
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
n |
|
inline |
Helper function to create a node according to this grammar. This is how nodes get their log probabilities.
r |
|
inline |
|
inline |
|
inlinestatic |
|
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.
nt |
|
inline |
Return the normalizing constant (NOT log) for all rules of type nt
nt |
|
inlinevirtual |
Randomly sample a rule of type nt.
nt |
|
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.
s |
|
inline |
The start nonterminal type.
|
static |
size_t Grammar< _input_t, _output_t, GRAMMAR_TYPES >::GRAMMAR_MAX_DEPTH = 64 |
|
static |
std::vector<Rule> Grammar< _input_t, _output_t, GRAMMAR_TYPES >::rules[N_NTs] |
std::array<double,N_NTs> Grammar< _input_t, _output_t, GRAMMAR_TYPES >::Z |