From e70ded36fbf5b78446ac65cd0af2323deebcb895 Mon Sep 17 00:00:00 2001 From: Bartosz Broda <bartosz.broda@gmail.com> Date: Mon, 13 Jun 2011 23:40:48 +0200 Subject: [PATCH] small cleanup, small extensions --- libmwereader/mwe.cpp | 21 +++++++++++++++++---- libmwereader/mwe.h | 28 +++++++++++++++++----------- libmwereader/mweparser.cpp | 11 ++++++----- 3 files changed, 40 insertions(+), 20 deletions(-) diff --git a/libmwereader/mwe.cpp b/libmwereader/mwe.cpp index 897c80e..6bd065a 100644 --- a/libmwereader/mwe.cpp +++ b/libmwereader/mwe.cpp @@ -19,7 +19,7 @@ LexicalUnit::LexicalUnit(const std::string &base, } bool LexicalUnit::IsHere(const Wccl::SentenceContext &sc, - std::set<size_t> &out_position) + std::set<size_t> &out_position) const { boost::shared_ptr<const Wccl::Bool> pResult = condition_->apply(sc); if(pResult->get_value() == false) @@ -39,6 +39,8 @@ bool LexicalUnit::IsHere(const Wccl::SentenceContext &sc, return true; } +//////////////////////////////////////////////////////////////////// +// TODO realy needed? FixedLU::FixedLU(const std::string &base, boost::shared_ptr<Wccl::Operator<Wccl::Bool> > condition, @@ -56,12 +58,14 @@ FlexLU::FlexLU(const std::string &base, { } -MWEIndex::MWEIndex() -{ +//////////////////////////////////////////////////////////////////// +MWEIndex::MWEIndex() : index_(), empty_() +{ + // noop } -void MWEIndex::add_lexicalunit(LexicalUnitPtr lu) +void MWEIndex::add_lexicalunit(LexicalUnit::Ptr lu) { foreach(const std::string& base, lu->get_potential_bases()){ value_type::iterator find = index_.find(base); @@ -75,4 +79,13 @@ void MWEIndex::add_lexicalunit(LexicalUnitPtr lu) } } +const MWEIndex::luvec& MWEIndex::get_potential_lu(const std::string &base){ + value_type::iterator find = index_.find(base); + if(find == index_.end()){ // not found -> return empty + return empty_; + } + + return (find->second); +} + }//ns Corpus2 diff --git a/libmwereader/mwe.h b/libmwereader/mwe.h index 0676ad0..e0ee53c 100644 --- a/libmwereader/mwe.h +++ b/libmwereader/mwe.h @@ -31,14 +31,14 @@ public: * \returns true if this lexical unit was found here */ virtual bool IsHere(const Wccl::SentenceContext& sc, - std::set<size_t> &out_positions) ; + std::set<size_t> &out_positions) const; const std::string & get_base() const{ return base_;} const strmap & get_variables() const{ return variables_;} const strset& get_potential_bases() const{ return potential_bases_;} - + typedef boost::shared_ptr<LexicalUnit> Ptr; protected: boost::shared_ptr<Wccl::Operator<Wccl::Bool> > condition_; @@ -51,7 +51,7 @@ protected: const Wccl::Position nowhere_; }; -typedef boost::shared_ptr<LexicalUnit> LexicalUnitPtr; + // TODO: czy bedzie potrzebny podzial na fix/flex w kodzie? @@ -59,9 +59,9 @@ class FixedLU : public LexicalUnit { public: FixedLU(const std::string &base, - boost::shared_ptr<Wccl::Operator<Wccl::Bool> > condition, - boost::shared_ptr<Wccl::Operator<Wccl::Bool> > head_cond, - std::map<std::string, std::string> variables + LexicalUnit::BoolOpPtr condition, + LexicalUnit::BoolOpPtr head_cond, + LexicalUnit::strmap variables ); }; @@ -69,9 +69,9 @@ class FlexLU : public LexicalUnit { public: FlexLU(const std::string &base, - boost::shared_ptr<Wccl::Operator<Wccl::Bool> > condition, - boost::shared_ptr<Wccl::Operator<Wccl::Bool> > head_cond, - std::map<std::string, std::string> variables + LexicalUnit::BoolOpPtr condition, + LexicalUnit::BoolOpPtr head_cond, + LexicalUnit::strmap variables ); }; @@ -79,16 +79,22 @@ public: class MWEIndex // lub base -> vector<LexicalUnit> { public: + typedef std::vector<LexicalUnit::Ptr> luvec; + MWEIndex(); - void add_lexicalunit(LexicalUnitPtr lu); + void add_lexicalunit(LexicalUnit::Ptr lu); + + const luvec& get_potential_lu(const std::string& base); protected: - typedef std::vector<LexicalUnitPtr> luvec; + typedef boost::unordered_map<std::string,luvec> value_type; value_type index_; + + const luvec empty_; }; }// ns Corpus2 diff --git a/libmwereader/mweparser.cpp b/libmwereader/mweparser.cpp index c8fd91e..cda5674 100644 --- a/libmwereader/mweparser.cpp +++ b/libmwereader/mweparser.cpp @@ -60,17 +60,18 @@ namespace Corpus2 { MWEBuilder::BoolOpPtr head = mwe_builder_->get_head_condition( head_cond_); - LexicalUnitPtr lu; if(group_type_ == "fix"){ // group_name_ -> lower case - lu = LexicalUnitPtr(new FixedLU(mwe_base_, main, head, - variables_)); + mwe_index_.add_lexicalunit( LexicalUnit::Ptr(new FixedLU(mwe_base_, main, head, + variables_))); } else if(group_type_ == "flex"){ - lu = LexicalUnitPtr(new FlexLU(mwe_base_, main, head, - variables_)); + mwe_index_.add_lexicalunit(LexicalUnit::Ptr(new FlexLU(mwe_base_, main, head, + variables_))); } else { throw Wccl::WcclError("Unknown type of lexical unit:" + group_type_); } + + } std::string MWEParser::get_attribute(const AttributeList& attributes, -- GitLab