Skip to content
Snippets Groups Projects
Commit 9d71e269 authored by Paweł Kędzia's avatar Paweł Kędzia
Browse files

Merge branch 'master' of nlp.pwr.wroc.pl:wccl

parents 760a1c39 971eb269
Branches
No related merge requests found
Showing
with 171 additions and 29 deletions
......@@ -8,7 +8,7 @@ bool LeftLook::iterate(
Wccl::Position &p,
const Wccl::FunExecContext &context) const
{
for(int i = left; i <= right; ++i) {
for(int i = right; i >= left; --i) {
p.set_value(i);
if(evaluating_expr_->apply(context)->get_value()) {
return true;
......
......@@ -6,8 +6,9 @@
namespace Wccl {
/**
* Iterative operator "llook", which looks for a first
* position from left that makes evaluating expression return true.
* Iterative operator "llook", which looks for the rightmost position
* in a range that makes the evaluating expression return true, i.e.
* it "looks to the left".
*/
class LeftLook : public Iteration
{
......@@ -30,12 +31,12 @@ public:
protected:
/**
* @returns True if, when scanning from left,
* a position within range is found that makes
* @returns True if, when scanning right-to-left,
* a position within the range is found that makes
* the evaluating function return true. False
* otherwise.
* @note Upon success, the iteration variable is
* set to the first position in range from left
* set to the rightmost position in range
* that has made evaluation function return true
* (that is the position that made the stop condition
* true in this case).
......
......@@ -8,7 +8,7 @@ bool RightLook::iterate(
Wccl::Position &p,
const Wccl::FunExecContext &context) const
{
for(int i = right; i >= left; --i) {
for(int i = left; i <= right; ++i) {
p.set_value(i);
if(evaluating_expr_->apply(context)->get_value()) {
return true;
......
......@@ -6,8 +6,9 @@
namespace Wccl {
/**
* Iterative operator "rlook", which looks for a first
* position from right that makes evaluating expression return true.
* Iterative operator "rlook", which looks for the leftmost position
* in a range that makes the evaluating expression return true, i.e.
* it "looks to the right".
*/
class RightLook : public Iteration
{
......@@ -30,12 +31,12 @@ public:
protected:
/**
* @returns True if, when scanning from right,
* a position within range is found that makes
* @returns True if, when scanning left-to-right,
* a position within the range is found that makes
* the evaluating function return true. False
* otherwise.
* @note Upon success, the iteration variable is
* set to the first position in range from right
* set to the leftmost position in range
* that has made evaluation function return true
* (that is the position that made the stop condition
* true in this case).
......
......@@ -19,10 +19,12 @@ public:
PointAgreement(
const PosFunctionPtr& pos1_expr,
const PosFunctionPtr& pos2_expr,
const TSetFunctionPtr& attribs_expr)
const TSetFunctionPtr& attribs_expr,
const Corpus2::Tagset& tagset)
: pos1_expr_(pos1_expr),
pos2_expr_(pos2_expr),
attribs_expr_(attribs_expr)
attribs_expr_(attribs_expr),
tagset_(tagset)
{
BOOST_ASSERT(pos1_expr_);
BOOST_ASSERT(pos2_expr_);
......@@ -45,6 +47,7 @@ protected:
const PosFunctionPtr pos1_expr_;
const PosFunctionPtr pos2_expr_;
const TSetFunctionPtr attribs_expr_;
const Corpus2::Tagset& tagset_;
/**
* Gets two positions from arguments. If either of them
......
......@@ -18,10 +18,12 @@ public:
StrongAgreement(
const PosFunctionPtr& left_pos_expr,
const PosFunctionPtr& right_pos_expr,
const TSetFunctionPtr& attribs_expr)
const TSetFunctionPtr& attribs_expr,
const Corpus2::Tagset& tagset)
: left_pos_expr_(left_pos_expr),
right_pos_expr_(right_pos_expr),
attribs_expr_(attribs_expr)
attribs_expr_(attribs_expr),
tagset_(tagset)
{
BOOST_ASSERT(left_pos_expr_);
BOOST_ASSERT(right_pos_expr_);
......@@ -45,6 +47,7 @@ protected:
const PosFunctionPtr left_pos_expr_;
const PosFunctionPtr right_pos_expr_;
const TSetFunctionPtr attribs_expr_;
const Corpus2::Tagset& tagset_;
/**
* Gets start and end positions for range from arguments.
......
......@@ -18,10 +18,12 @@ public:
WeakAgreement(
const PosFunctionPtr& left_pos_expr,
const PosFunctionPtr& right_pos_expr,
const TSetFunctionPtr& attribs_expr)
const TSetFunctionPtr& attribs_expr,
const Corpus2::Tagset& tagset)
: left_pos_expr_(left_pos_expr),
right_pos_expr_(right_pos_expr),
attribs_expr_(attribs_expr)
attribs_expr_(attribs_expr),
tagset_(tagset)
{
BOOST_ASSERT(left_pos_expr_);
BOOST_ASSERT(right_pos_expr_);
......@@ -45,6 +47,7 @@ protected:
const PosFunctionPtr left_pos_expr_;
const PosFunctionPtr right_pos_expr_;
const TSetFunctionPtr attribs_expr_;
const Corpus2::Tagset& tagset_;
/**
* Gets start and end positions for range from arguments.
......
......@@ -22,11 +22,13 @@ public:
const PosFunctionPtr& left_pos_expr,
const PosFunctionPtr& right_pos_expr,
const TSetFunctionPtr& attribs_expr,
const TSetFunctionPtr& mask_expr)
const TSetFunctionPtr& mask_expr,
const Corpus2::Tagset& tagset)
: left_pos_expr_(left_pos_expr),
right_pos_expr_(right_pos_expr),
attribs_expr_(attribs_expr),
mask_expr_(mask_expr)
mask_expr_(mask_expr),
tagset_(tagset)
{
BOOST_ASSERT(left_pos_expr_);
BOOST_ASSERT(right_pos_expr_);
......@@ -54,7 +56,7 @@ protected:
const TSetFunctionPtr attribs_expr_;
const TSetFunctionPtr mask_expr_;
const Corpus2::Tagset& tagset_;
/**
* Gets range specified via Position expressions. Trims the range
......
......@@ -1098,9 +1098,9 @@ bool_iteration
RPAREN {
ret.reset(new AtLeast(lpos, rpos, *pacc, expr, min_match));
}
| "llook" LPAREN
lpos = position_operator [tagset, vars] COMMA
| "llook" LPAREN //note inverted rpos/lpos order
rpos = position_operator [tagset, vars] COMMA
lpos = position_operator [tagset, vars] COMMA
pacc = position_variable_acc [vars] COMMA
expr = bool_operator [tagset, vars]
RPAREN {
......
......@@ -6,7 +6,10 @@
#define WCCL_VALUE_PREAMBLE \
static const char* type_name; \
const char* get_type_name() const { return type_name; } \
static std::string var_repr(const std::string &var_name);
static std::string var_repr(const std::string &var_name); \
std::string make_var_repr(const std::string &var_name) const { \
return var_repr(var_name); \
}
namespace Wccl {
......@@ -35,6 +38,8 @@ public:
virtual ~Value() {}
virtual std::string make_var_repr(const std::string&) const = 0;
/**
* String representation function, in general, a tagset is required,
* but some classes might not need that, so by default just forward
......
......@@ -4,6 +4,7 @@ After the --- line comes the first test -- the operator string, followed by an e
If the first operator line is "position=XXX", position is parsed as an int and that is used as the current position in the sentence for this and any following tests. Default position is 0.
Tests are separated from one another by a --- line.
This test loads no sentence, but can change the position anyway. Tagset is kipi by default.
Variable state can be checked by a name=value lines after the expected output.
tagset=ikipi
---
equal(["aaa"], "aaa")
......@@ -13,3 +14,10 @@ True
lower(["A"])
["a"]
---
setvar($b:A, setvar($t:B, subst))
True
A=True
B={subst}
tagset=kipi
sentence=t01.xml
---
rlook(begin, end, $Test, True)
True
---
llook(0, end, $V, False)
False
V=nowhere
---
position=0
rlook(0,end,$P,
inter(class[$P], {subst})
)
True
P=3
---
position=3
rlook(0,end,$P,
inter(class[$P], {subst})
)
True
P=0
---
position=4
rlook(0,end,$P,
inter(class[$P], {subst})
)
True
P=8
---
position=19
rlook(0,end,$P,inter(class[$P], {subst}))
False
P=nowhere
---
position=26
llook(0,begin,$NomAcc, equal(cas[$NomAcc],{nom,acc}))
True
NomAcc=-1
---
position=12
llook(0,begin,$NomAcc, equal(cas[$NomAcc],{nom,acc}))
True
NomAcc=-4
---
position=2
llook(0,begin,$NomAcc, equal(cas[$NomAcc],{nom,acc}))
False
NomAcc=nowhere
---
position=0
only(10,12,$Loc, in(loc, cas[$Loc]))
True
Loc=12
---
position=0
only(10,13,$Loc, in(loc, cas[$Loc]))
False
Loc=nowhere
---
position=0
atleast(9,13,$Loc, in(loc, cas[$Loc]), 3)
True
Loc=12
---
position=0
atleast(9,13,$Loc, in(loc, cas[$Loc]), 4)
False
Loc=nowhere
---
position=-1
llook(0,begin,$NomAcc, equal(cas[$NomAcc],{nom,acc}))
False
NomAcc=nowhere
---
......@@ -98,7 +98,24 @@ void test_one_item_actual(const compare_test& c)
expected_output = "";
operator_string = "";
++line_no;
std::getline(ifs_in, line);
while (ifs_in.good() && line != "---" && line != "") {
std::getline(ifs_in, line);
std::vector<std::string> fields;
boost::algorithm::split(fields, line, boost::is_any_of(separators));
if (fields.size() == 2) {
boost::shared_ptr<Wccl::Value> v;
v = fu.variables()->get<Wccl::Value>(fields[0]);
if (!v) {
BOOST_ERROR("Invalid variable name in test: "
<< fields[0] << " on line " << line_no);
} else if (v->to_string(tagset) != fields[1]) {
BOOST_ERROR("Variable " << fields[0]
<< " value mismatch on line "
<< line_no << "\n: expected " << fields[1]
<< " got " << v->to_string(tagset));
}
}
}
BOOST_REQUIRE(line == "---" || line == "");
} else {
if (operator_string.empty() && line.substr(0, 9) == "position=") {
......
......@@ -90,8 +90,17 @@ void libedit_read_loop(boost::function<bool (const std::string&)>& line_cb)
}
#endif
void dumpvariables(const Wccl::Variables& vars, const Corpus2::Tagset& tagset)
{
typedef std::pair<std::string, boost::shared_ptr<Wccl::Value> > v_t;
foreach (const v_t& v, vars.get_all<Wccl::Value>()) {
std::cerr << v.second->make_var_repr(v.first) << "="
<< v.second->to_string(tagset) << "\n";
}
}
bool process_line(const std::string& line, Wccl::Parser& parser,
Wccl::SentenceContext& sc, bool all_positions)
Wccl::SentenceContext& sc, bool all_positions, bool dump_variables)
{
if (line.empty() || line == "exit" || line == "quit") {
return true;
......@@ -123,6 +132,9 @@ bool process_line(const std::string& line, Wccl::Parser& parser,
<< "Parsed expression: "
<< retVal->to_string(parser.tagset())
<< std::endl;
if (dump_variables) {
dumpvariables(*cx.variables(), parser.tagset());
}
} else {
std::cerr << "Problem while parsing -- "
<< "retVal is NULL!" << std::endl;
......@@ -156,6 +168,7 @@ int main(int argc, char** argv)
std::string query_load = "";
std::string position = "0";
bool quiet = false;
bool dump_variables = false;
using boost::program_options::value;
boost::program_options::options_description desc("Allowed options");
......@@ -172,6 +185,8 @@ int main(int argc, char** argv)
"Query to run (disables interactive mode)\n")
("quiet,q", value(&quiet)->zero_tokens(),
"Suppress messages\n")
("variables,V", value(&dump_variables)->zero_tokens(),
"Dump variables after running each query\n")
("help,h", "Show help")
;
boost::program_options::variables_map vm;
......@@ -214,14 +229,14 @@ int main(int argc, char** argv)
sc.set_position(pos);
Wccl::Parser parser(tagset);
if (!query.empty()) {
process_line(query, parser, sc, position == "all");
process_line(query, parser, sc, position == "all", dump_variables);
return 0;
} else if (!query_load.empty()) {
std::ifstream qf(query_load.c_str());
if (qf.good()) {
std::stringstream ss;
ss << qf.rdbuf();
process_line(ss.str(), parser, sc, position == "all");
process_line(ss.str(), parser, sc, position == "all", dump_variables);
return 0;
} else {
throw Wccl::FileNotFound(sentence_load, "", "Query file");
......@@ -229,7 +244,7 @@ int main(int argc, char** argv)
}
boost::function<bool (const std::string&)> f;
f = boost::bind(&process_line, _1, boost::ref(parser), boost::ref(sc),
position == "all");
position == "all", dump_variables);
#ifdef HAVE_LIBEDIT
libedit_read_loop(f);
#else
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment