Commit 82e66ebf authored by Florent Jacquemard's avatar Florent Jacquemard
Browse files

fix CONFLICT merge termirun -> beta

parents 05a1f4f5 8e380b4c
This diff is collapsed.
......@@ -78,7 +78,7 @@ Weight CountingWCFG::eval(const RhythmTree& t) const
assert (this->hasWeightType("CountingWeight"));
Position p = Position();
Weight w = evalCountingVerbose(t, _initial, p);
if (w.zero())
if (w.is_zero())
{
WARN("FAIL in evaluating tree {}", t);
}
......@@ -98,7 +98,7 @@ Weight CountingWCFG::evalCountingVerbose(const RhythmTree& t,
size_t tar = t.arity();
Weight res = this->weight_zero();
assert (! res.unknown());
assert (res.zero());
assert (res.is_zero());
assert (res.hasType("CountingWeight"));
//TRACE("EVAL t={} p={} state={}", t, p, s);
......@@ -134,7 +134,7 @@ Weight CountingWCFG::evalCountingVerbose(const RhythmTree& t,
}
// evaluation failed (no rule appliable)
if (res.zero())
if (res.is_zero())
{
WARN("FAIL in tree evaluation state = {} position = {} subtree = {}",
s, p, t);
......
......@@ -36,15 +36,15 @@ int read_config(const std::string filename)
if (reader.ParseError() < 0)
return -1;
Parsing::OPT_RUN_DUR = reader.GetBoolean("options", "run_dur", true);
Parsing::OPT_RUN_UNIT = reader.GetBoolean("options", "run_unit", true);
Parsing::OPT_RUN_STRICT = reader.GetBoolean("options", "run_strict", false);
if (Parsing::OPT_RUN_DUR == false)
parsing::OPT_RUN_DUR = reader.GetBoolean("options", "run_dur", true);
parsing::OPT_RUN_UNIT = reader.GetBoolean("options", "run_unit", true);
parsing::OPT_RUN_STRICT = reader.GetBoolean("options", "run_strict", false);
if (parsing::OPT_RUN_DUR == false)
{
Parsing::OPT_RUN_UNIT = false;
Parsing::OPT_RUN_STRICT = false;
parsing::OPT_RUN_UNIT = false;
parsing::OPT_RUN_STRICT = false;
}
Parsing::OPT_RUN_STRICT = reader.GetBoolean("options", "norest", false);
parsing::OPT_RUN_STRICT = reader.GetBoolean("options", "norest", false);
// CST_ALPHA = reader.GetReal("params", "alpha", 0.5);
Weight::CST_ALPHA = reader.GetReal("params", "alpha", 1.0);
......
......@@ -130,7 +130,7 @@ void Parser<P>::printobestRun(std::ostream& o,
// QP_WEIGHT == WEIGHT_STOCHASTIC
if ( CST_WEIGHT_TYPE == WeightDom::STOCHASTIC )
{
if (cpty.zero()) // 0
if (cpty.is_zero()) // 0
{
o << "unknown";
}
......@@ -142,7 +142,7 @@ void Parser<P>::printobestRun(std::ostream& o,
// QP_WEIGHT == WEIGHT_PENALTY
else if ( CST_WEIGHT_TYPE == WeightDom::PENALTY )
{
if (cpty.zero()) // +infinity
if (cpty.is_zero()) // +infinity
{
o << "unknown";
}
......
......@@ -177,7 +177,7 @@ void DepthMarking::mark_rec(state_t s, int d, const WCFG& wcfg)
i++)
{
// enumerate the states in the body of the current transition
for (StateMultList::const_iterator j = i->cbegin();
for (Body::const_iterator j = i->cbegin();
j != i->cend();
j++)
{
......
......@@ -192,9 +192,9 @@ bool Krecord<P>::bestFilter(const Run<P>* r)
assert(r->inner() || r->terminal());
// filter acc. to optimization flags
if (Parsing::OPT_RUN_UNIT)
if (parsing::OPT_RUN_UNIT)
{
//assert (Parsing::OPT_RUN_DUR);
//assert (parsing::OPT_RUN_DUR);
if (r->duration.unit() && r->inner())
return false;
}
......
......@@ -27,7 +27,7 @@ Weight Pointer::innerWeight(const Production& tr) const
assert(! tr.weight().unknown());
assert(tr.inner());
return (tr.weight());
// return (tr.weight().get_one());
// return (tr.weight().one());
}
......
......@@ -352,7 +352,7 @@ Weight SIPpointer::terminalWeight(const InputSegment* s,
assert(! tr.weight().unknown());
assert(tr.terminal());
assert(this->compatible(tr.label()));
// return (tr.weight().get_one()); // neutral element for *
// return (tr.weight().one()); // neutral element for *
// tweight = transition weight = complexity
Weight tweight = tr.weight();
......
......@@ -38,7 +38,7 @@ template<class P> class Atable;
/// - NULL ptr to Run
/// - ptr to Run with unknown weight (i.e. weight with NULL letter)
/// that case includes null runs.
/// - ptr to Run with weight == zero (acc. to test zero()).
/// - ptr to Run with weight == zero (acc. to test is_zero()).
/// a Run not in these 3 case is called valid.
///
/// [update] the runs with weight zero (still invalid) can be added to records
......
......@@ -31,7 +31,7 @@ bool Record<P>::valid(Run<P>* r)
WARN("run {} with unknown weight not added to record", *r);
return false;
}
else if (r->weight.zero())
else if (r->weight.is_zero())
{
ERROR("run {} with zero weight added to record", *r);
return true;
......@@ -42,6 +42,6 @@ bool Record<P>::valid(Run<P>* r)
}
// return ((r != NULL) &&
// (! r->weight.unknown()) &&
// (! r->weight.zero()));
// (! r->weight.is_zero()));
}
......@@ -58,7 +58,7 @@ _meta(false)
// segment might be NULL
weight = p.terminalWeight(env->segment, t);
assert(! weight.unknown());
//assert(! weight.zero()); // compatibility is precondition
//assert(! weight.is_zero()); // compatibility is precondition
// at this point, a concrete label must be computable
// i.e. p is enough instanciated (pre value)
......@@ -87,7 +87,7 @@ _meta(false)
assert(a > 1);
weight = p.innerWeight(t);
assert(! weight.unknown());
assert(! weight.zero());
assert(! weight.is_zero());
for (size_t i = 0; i < a; i++)
{
......@@ -368,9 +368,9 @@ bool Run<P>::filter()
// filter acc. to optimization flags
// we do not filter out meta runs
if (! this->meta() && Parsing::OPT_RUN_UNIT)
if (! this->meta() && parsing::OPT_RUN_UNIT)
{
//assert(Parsing::OPT_RUN_DUR);
//assert(parsing::OPT_RUN_DUR);
if (this->duration.unit() && this->inner())
return false;
}
......@@ -440,21 +440,21 @@ void Run<P>::update(const Weight& w, const DurationList& dl) // arg duration
{
assert(this->inner());
assert(! w.unknown());
//assert(! w.zero()); // zero weight
//assert(! w.is_zero()); // zero weight
assert(_first_partialortoupdate < _children.size());
assert(_children[_first_partialortoupdate].complete());
// assert(! weight.zero()); // zero weight
// assert(! weight.is_zero()); // zero weight
weight *= w;
// (flag) concatenation dur
if (Parsing::OPT_RUN_DUR) // dl may be empty in case of manual update
if (parsing::OPT_RUN_DUR) // dl may be empty in case of manual update
{
duration += dl;
}
_first_partialortoupdate++;
// update is terminated
if (this->complete() && Parsing::OPT_RUN_DUR && (! dl.empty()))
if (this->complete() && parsing::OPT_RUN_DUR && (! dl.empty()))
{
assert (duration.summed() == this->arity());
duration.normalize(); // .mult(Rational(1, arity));
......@@ -466,7 +466,7 @@ template<class P>
void Run<P>::_init_duration_list(label_t label)
{
// update duration list
if (Parsing::OPT_RUN_DUR)
if (parsing::OPT_RUN_DUR)
{
if (Label::continuation(label))
{
......
......@@ -456,7 +456,7 @@ size_t Table<P, R, H>::addUpdate(const P& p, Run<P>* r, Record<P>* it)
{
assert(ri->complete());
if (ri->weight.zero())
if (ri->weight.is_zero())
{
WARN("addUpdate: child run {} with zero weight", *ri);
}
......
## for quant target
## only one parser need to be compiled to build a quant target
add_library(parsing STATIC
ParsingEnv.cpp
Ranked.cpp
KeyS.cpp
KeySI.cpp
KeySIP.cpp
KeySIO.cpp
RunFilterMode.cpp
RunFilter.cpp
)
//
// Key.hpp
// squanty
//
// Created by Florent Jacquemard on 04/06/2019.
// Copyright © 2019 Florent Jacquemard. All rights reserved.
//
/// @addtogroup parsing
/// @{
#ifndef Key_hpp
#define Key_hpp
#define KEY_LPAR '<'
#define KEY_RPAR '>'
#include <stdio.h>
#include <assert.h>
#include <queue>
#include <stack>
#include "trace.hpp"
#include "Weight.hpp"
#include "WeightDom.hpp"
#include "Tlist.hpp"
#include "Transition.hpp"
#include "ParsingEnv.hpp"
namespace Parsing{
/// @brief template for keys.
/// a key is an augmented state,
/// acting as index for best runs in a parse table.
///
/// It is made of:
/// - one state of a base SWTA,
/// - extended with attributes taking values in different domains,
/// and of two kinds:
/// - synthesized attributes are propagated bottom-up,
/// - inherited attributes are propagated top-down.
///
/// - A key is partial if only its inherited attributes are valued.
/// - A key is complete if its all inherited and synthesized attributes are
/// valued
/// (keys without synthesized attributes are always complete).
///
/// A augmented transition has the form
/// a(K1, ..., Kn) -> K0
/// where
/// - a is a label of arity n.
/// - (K1, ..., Kn) is a tuple of complete keys called body of the transition.
/// - K0 is a complete key called head of the transition.
/// K0, K1, ..., Kn are abject of the same concrete class K derived
/// from this class.
///
/// concrete transitions are built with the following functions:
/// - initial augmented states can be built with `top`.
/// - from a given a partial augmented state `K0` for the head
/// of an inner augmented transition,
/// one can built a partial first child by calling `firstChild` on `K0`.
/// - then every other child `Ki+1` can be built by calling `nextSibling`
/// on sibling `Ki`, for `i > 0`.
/// - from a given a partial augmented state `K0` for the head
/// of a terminal (leaf) augmented transition,
/// one can built possible complete instances by calling `leaf`.
/// - the (partial) states for siblings are instanciated
/// by repeating from step 3 down to leaves.
/// - once all the children augmented states are complete,
/// the head 'K0' can be instanciated by calling `instanciate` on `K0`
/// (with the last child complete state in argument).
///
/// Some synthesized attribute contain weight values.
/// The weight of a key is computed from the attributes and an input symbol.
template<class K>
class Key
{
public:
~Key();
/// @brief wheter this key has a rank (for k-best computations)
/// @todo TBR
virtual bool ranked() const { return false; }
/// @brief computation of first child key in augmented transition.
/// allocate and return a partial key to be instanciated as the first child
/// of this key in the body of a transition augmenting tr,
/// and for an parsing environment env.
/// i.e. this key is the head key of the augmented transition,
/// it can be partial or complete.
/// @param tr WTA transition, augmented by the transition partially
/// constructed here. must be inner.
/// @param env parsing environment.
inline K* firstChild(const Transition& tr,
const ParsingEnv& env) const;
/// @brief computation of next sibling key in augmented transition.
/// allocate and return a partial key to be instanciated as the
/// next sibling of this key
/// in the body of a transition augmenting tr,
/// and for an parsing environment env.
/// @param i position of the returned new sibling in body.
/// between 1 and arity. position of first sibling is 0.
/// @param parent head key of the augmented transition
/// partial or complete.
/// @param tr SWTA transition, augmented by the transition partially
/// constructed here.
/// @param env parsing environment.
/// @warning this key must be complete.
inline K* nextSibling(size_t i,
const K& parent,
const Transition& tr,
const ParsingEnv& env) const;
/// @brief completion of a partial inner key heading an augmented transition.
/// allocate and return a complete upgrade of this key, by copy,
/// to be the head of an augmented transition,
/// using the parsing environment env.
/// @param a label of the augmented transition. must be inner.
/// @param lastchild last child in the body of the augmented transition.
/// must be complete.
/// @param env parsing environment.
/// @warning this Key must be partial.
inline K* instanciate(label_t a,
const K& lastchild,
const ParsingEnv& env) const;
/// @brief this key can be an inner node, target of tr, in a run.
/// @param tr a SWTA transition, augmented by the transition partially
/// constructed here.
/// @param env parsing environment.
virtual bool inner(const Transition& tr,
const ParsingEnv& env) const = 0;
/// @brief number of leaf instances that can be obtained
/// from this key and the given args.
/// @param tr a SWTA transition, augmented by the transition partially
/// constructed here. must be nullary (leaf, terminal).
/// @param env parsing environment.
/// @return - 0 if there no leaf instance compatible with tr.
/// if this key is complete, it means that it is not compatible with tr, a, and env.
/// - 1 if there is exactly one leaf instance of this key compatible with tr, a and env.
/// if this key is complete, it means that it is compatible with tr, a, and env.
/// if this key is partial, the leaf constructor can be use to built the compatible instance.
/// can be used instead of Key::leaf.
/// - 2 if there are 2 or more leaf instances compatible with tr.
/// it means that this key is partial, the compatible instances can be built with leaves.
virtual size_t leaf(const Transition& tr,
const ParsingEnv& env) const = 0;
/// @brief completion of this partial leaf key to be the head
/// of a nullary (leaf) transition augmenting tr.
/// Build and store in the given queue q
/// all ther new complete instances of this key,
/// using the parsing environment env.
/// @param tr a SWTA transition, augmented by the transition partially
/// constructed here. must be nullary (leaf, terminal).
/// @param env parsing environment.
/// @param q empty queue used to store the complete instances.
/// @warning this Key must be partial.
virtual void leaves(const Transition& tr,
const ParsingEnv& env,
std::queue<K>& q) const;
/// Best label of terminal transition augmenting tr targeted to this key.
/// @param tr a SWTA transition, possibly unlabelled or partialy labelled
/// @param env parsing environment.
/// @return the label maximizing the weight function for this label, this transition, this env.
/// @warning this key must be complete.
virtual label_t label(const Transition& tr,
const ParsingEnv& env) const = 0;
/// @brief The weight of an augmented transition headed by an instance
/// of this Key (augmented state) and augmenting tr.
/// @param a label for the augmented transition.
/// @param tr SWTA transition, augmented by the transition partially
/// constructed here.
/// @param env parsing environment.
/// @return the weight of a transition headed by an instance of this key
/// and augmenting tr.
/// @return a returned weight of zero (absorbing in the semiring)
/// indicates incompatibility between this key and the transition.
/// @warning if tr is nullary (leaf), this key must be complete.
/// @warning if tr is not nullary (inner), this key can be partial or complete.
/// @warning The weight of a run headed by the augmented transition
/// is the semiring product of this value with the product
/// of weights of the subruns.
virtual Weight weight(label_t a,
const Transition& tr,
const ParsingEnv& env) const = 0;
// default to assert(! tr.weight().unknown()); return tr.weight(); ?
/// @brief push in given stack the WTA transitions to be generalized
/// in order to augmented transitions targeted by this key,
/// or an instance.
/// @param s a stack to be filled with WTA transitions to be augmented.
/// @param env parsing environment, containing a wta.
virtual void transitions(std::stack<const Transition*>& s,
const ParsingEnv& env) const = 0;
// /// return an constant iterator to the first base transition in given environment
// /// with head state the state component of this Key.
// /// @param env parsing environment.
// /// @warning the base SWTA and the pool SWTA must be non-NULL in env.
// /// @todo TBR replaced by transitions()
// virtual typename Tlist<Transition>::const_iterator
// cbegin(const ParsingEnv& env) const = 0;
//
//
// /// return an constant iterator to the past-the-end base transition in given environment
// /// with head state the state component of this Key.
// /// @param env parsing environment.
// /// @warning the base SWTA and the pool SWTA must be non-NULL in env.
// /// @todo TBR replaced by transitions()
// virtual typename Tlist<Transition>::const_iterator
// cend(const ParsingEnv& env) const = 0;
/// Key assignement.
virtual K& operator= (const K& p) = 0;
/// for use as key in a unordered_multimap.
virtual bool operator==(const K& p) const = 0;
inline bool operator!=(const K& p) const
{ return (! (*this == p)); }
/// return wether this Key is an instance of p.
/// if p is complete, then it is equality.
virtual bool instance(const K& p) const = 0;
/// inverse of instance
/// return wether p is an instance of this Key.
/// if this Key is complete, then it is equality.
bool subsume(const K& p) const
{ return p.instance(*this); }
/// all attributes of the key are valued
virtual bool complete() const = 0;
/// one atribute at least of the key is not valued
inline bool partial() const
{ return(! this->complete()); }
/// allocate and return a special Run representing a parse failure for this key.
/// @param env parsing environment.
/// @return a pointer to a newly allocated fail run, with fail symbol.
/// This returned run will answer succesfully to Run.fail().
Run<K>* failRun(const ParsingEnv& env) const;
/// @brief time interval represented by this key,
/// or NULL if there is none.
virtual IntervalTree* interval() const { return NULL; }
protected:
/// @brief number of input events represented by this key.
virtual size_t size() const { return 0; }
private:
/// trick of Curiously Recurring Template Pattern
/// to force a derived class K to inherit Key<K>
friend K;
Key();
};
} // end namespace Parsing
// separated definition of template class
#include "Key.tpp"
#endif /* Key_hpp */
/// @}
//
// Key.tpp
// squanty
//
// Created by Florent Jacquemard on 04/06/2019.
// Copyright © 2019 Florent Jacquemard. All rights reserved.
//
namespace Parsing{
template<class K>
Key<K>::Key()
{ }
template<class K>
Key<K>::~Key()
{
// clear queue
// std::queue<int> empty;
// std::swap(leafq, empty );
}
template<class K>
K* Key<K>::firstChild(const Transition& tr,
const ParsingEnv& env) const
{
return new K(this, tr, env);
}
template<class K>
K* Key<K>::nextSibling(size_t i,
const K& parent,
const Transition& tr,
const ParsingEnv& env) const
{
assert(this->complete());
return new K(this, i, parent, tr, env);
}
//KeyS* KeyS::nextSibling(size_t i,
// const KeyS* parent,
// const Transition& tr,
// const ParsingEnv& env)
//{
// assert(tr.inner());
// assert(0 < i);
// assert(i < tr.size());
// assert(this->state() == tr.at(i-1));
// return (new KeyS(tr.at(i)));
//}
// [ALT] define instanciate as pure virtual,
// to be omplemented in every derived
template<class K>
K* Key<K>::instanciate(label_t a,
const K& lastchild,
const ParsingEnv& env) const
{
assert(this->partial());
assert(lastchild.complete());
return new K(this, a, lastchild, env);
}
template<class K>
void Key<K>::leaves(const Transition& tr,
const ParsingEnv& env,
std::queue<K>& q) const
{
assert(this->partial());
}
template<class K>
Run<K>* Key<K>::failRun(const ParsingEnv& env) const
{
assert(env.wta);
const Transition& t1 = env.dummyTransition_one();
assert(t1.weight().one());
label_t a = SymbLabel::make_fail(this->size());
return new Run<K>(t1, a, t1.weight());
}
// template<class K>
// K* Key<K>::leaf(label_t a,
// const Transition& tr,
// const ParsingEnv& env)
// {
// if (all_leaves)
// return NULL;
// else
// return new K(this, a, tr, env);
// }
//template<class K>