From a186d08f58f5da727cb5f61c5f5acb991e05207b Mon Sep 17 00:00:00 2001 From: lburdka <lburdka@glados.(none)> Date: Tue, 21 Aug 2012 13:21:02 +0200 Subject: [PATCH] Cleaned relwriter mess --- libcorpus2_whole/io/relwriter.cpp | 84 ++++++++++--------------------- libcorpus2_whole/io/relwriter.h | 25 +-------- swig/relationwriter.i | 13 ++--- 3 files changed, 34 insertions(+), 88 deletions(-) diff --git a/libcorpus2_whole/io/relwriter.cpp b/libcorpus2_whole/io/relwriter.cpp index 6f64c38..ca904a9 100755 --- a/libcorpus2_whole/io/relwriter.cpp +++ b/libcorpus2_whole/io/relwriter.cpp @@ -19,9 +19,11 @@ or FITNESS FOR A PARTICULAR PURPOSE. #include <libcorpus2/exception.h> #include <libcorpus2_whole/io/relwriter.h> -#include <fstream> +#include <iostream> #include <sstream> +#include <fstream> #include <boost/make_shared.hpp> +using namespace std; namespace Corpus2 { namespace whole { @@ -33,72 +35,40 @@ RelationWriter::RelationWriter(const std::string &rela_path) void RelationWriter::write(const std::vector< boost::shared_ptr<Relation> >& relations) -{ - file_.reset(new std::ofstream(rela_path_.c_str())); +{ + ofstream ofs(rela_path_.c_str(), ofstream::out); //header - file_->write("<relations>\n",12); - + ofs<<"<relations>\n"; + + //relations foreach (const boost::shared_ptr<Relation>& r, relations){ - file_->write(" <rel name=\"",14); - std::streamsize size = r->name().size(); - file_->write(r->name().c_str(),size); - //Maybe additional info about set is needed in Relation class - //file_->write("\" set=\"Anaphora\">\n",18); - file_->write("\">\n",3); - file_->write(" <from sent=\"",18); - size = r->from()->sentence_id().size(); - file_->write(r->from()->sentence_id().c_str(),size); - file_->write("\" chan=\"",8); - size = r->from()->channel_name().size(); - file_->write(r->from()->channel_name().c_str(),size); - file_->write("\">",2); - int an_num = r->from()->annotation_number(); - std::ostringstream ss; - ss << an_num; - std::string str = ss.str(); - size=str.size(); - file_->write(str.c_str(),size); - file_->write("</from>\n",8); - file_->write(" <to sent=\"",16); - size = r->to()->sentence_id().size(); - file_->write(r->to()->sentence_id().c_str(),size); - file_->write("\" chan=\"",8); - size = r->to()->channel_name().size(); - file_->write(r->to()->channel_name().c_str(),size); - file_->write("\">",2); - an_num = r->to()->annotation_number(); - std::ostringstream ss1; - ss1 << an_num; - str = ss1.str(); - size=str.size(); - file_->write(str.c_str(),size); - file_->write("</to>\n",6); - //file_->write("\n",1); - file_->write(" </rel>",9); - file_->write("\n",1); + ofs<<" <rel name=\""; + ofs<<r->name().c_str()<<"\">"<<endl; + ofs<<" <from sent=\""; + ofs<<r->from()->sentence_id().c_str(); + ofs<<"\" chan=\""; + ofs<<r->from()->channel_name().c_str(); + ofs<<"\">"; + ofs<<r->from()->annotation_number(); + ofs<<"</from>"<<endl; + ofs<<" <to sent=\""; + ofs<<r->to()->sentence_id().c_str(); + ofs<<"\" chan=\""; + ofs<<r->to()->channel_name().c_str(); + ofs<<"\">"; + ofs<<r->to()->annotation_number(); + ofs<<"</to>"<<endl; + ofs<<" </rel>"<<endl; } //footer - file_->write("</relations>",12); - file_->close(); -} - - -void write_relation(const boost::shared_ptr<Relation>& r) -{ - //std::streamsize size = r->from()->sentence_id().size(); - //f->write(r->from()->sentence_id().c_str(),size); - -} - -void do_header(){ + ofs<<"</relations>"; + ofs.close(); } -void do_footer(){ -} } // whole ns diff --git a/libcorpus2_whole/io/relwriter.h b/libcorpus2_whole/io/relwriter.h index e3b344c..6f82115 100755 --- a/libcorpus2_whole/io/relwriter.h +++ b/libcorpus2_whole/io/relwriter.h @@ -24,8 +24,6 @@ or FITNESS FOR A PARTICULAR PURPOSE. #include <libcorpus2_whole/relation.h> -#include <libcorpus2/io/xmlwriter.h> - #include <iostream> namespace Corpus2 { @@ -33,8 +31,7 @@ namespace whole { /** - * A reader for realtion documents. Note that document is read into memory - * before any processing may take place. + * A writer for realtion documents. */ class RelationWriter { public: @@ -44,6 +41,7 @@ public: */ RelationWriter(const std::string &rela_path); + /** * Lazy relations accessor. * If relations are not readed then read relations and returns list of them. @@ -54,29 +52,10 @@ public: private: - void write_relation(const boost::shared_ptr<Relation>& r); - - void do_header(); - - void do_footer(); - - - // ------------------------------------------------------------------------- - /// List of the relations in given relation file - std::vector< boost::shared_ptr<Relation> > relations_; - /// Path to file with relations std::string rela_path_; - /// File pointer - boost::shared_ptr<std::ofstream> file_; - // ------------------------------------------------------------------------- - // Temporary information of actual parsing relation - std::string rel_name_; - std::string ann_number_; - boost::shared_ptr<DirectionPoint> rel_from_; - boost::shared_ptr<DirectionPoint> rel_to_; }; } // whole ns diff --git a/swig/relationwriter.i b/swig/relationwriter.i index 41f23f4..1cb9f1d 100755 --- a/swig/relationwriter.i +++ b/swig/relationwriter.i @@ -9,14 +9,11 @@ namespace Corpus2 { namespace whole { - class RelationWriter { - public: - RelationWriter(const std::string &rela_path); - void write(const std::vector< boost::shared_ptr<Relation> > relations); - private: - void do_header(); - void do_footer(); - //void write_relation(const boost::shared_ptr<Relation>& r); + class RelationWriter { + public: + RelationWriter(const std::string &rela_path); + void write(const std::vector< boost::shared_ptr<Relation> > relations); + }; } // whole ns } // Corpus2 ns -- GitLab