00001 #include "SplitPointFileParser.h"
00002
00003 #include <istream>
00004 #include <string>
00005
00006 #include "util/string_piece.hh"
00007 #include "util/tokenize_piece.hh"
00008
00009 #include "syntax-common/exception.h"
00010
00011 namespace MosesTraining
00012 {
00013 namespace Syntax
00014 {
00015 namespace PostprocessEgretForests
00016 {
00017
00018 SplitPointFileParser::SplitPointFileParser()
00019 : m_input(0)
00020 {
00021 }
00022
00023 SplitPointFileParser::SplitPointFileParser(std::istream &input)
00024 : m_input(&input)
00025 {
00026 ++(*this);
00027 }
00028
00029 SplitPointFileParser &SplitPointFileParser::operator++()
00030 {
00031 if (!m_input) {
00032 return *this;
00033 }
00034 m_entry.splitPoints.clear();
00035 if (!std::getline(*m_input, m_tmpLine)) {
00036 m_input = 0;
00037 return *this;
00038 }
00039 ParseLine(m_tmpLine, m_entry.splitPoints);
00040 return *this;
00041 }
00042
00043 void SplitPointFileParser::ParseLine(const std::string &line,
00044 std::vector<SplitPoint> &splitPoints)
00045 {
00046 std::string tmp;
00047 const util::AnyCharacter delimiter(" \t");
00048 for (util::TokenIter<util::AnyCharacter, true> p(line, delimiter); p; ++p) {
00049 splitPoints.resize(splitPoints.size()+1);
00050 SplitPoint &splitPoint = splitPoints.back();
00051 std::size_t pos = p->find(',');
00052
00053 StringPiece sp = p->substr(0, pos);
00054 sp.CopyToString(&tmp);
00055 splitPoint.tokenPos = std::atoi(tmp.c_str());
00056 std::size_t begin = pos+1;
00057 pos = p->find(',', begin);
00058
00059 sp = p->substr(begin, pos-begin);
00060 sp.CopyToString(&tmp);
00061 splitPoint.charPos = std::atoi(tmp.c_str());
00062
00063 sp = p->substr(pos+1);
00064 sp.CopyToString(&splitPoint.connector);
00065 if (splitPoint.connector.size() > 1) {
00066 throw Exception("multi-character connectors not currently supported");
00067 }
00068 }
00069 }
00070
00071 bool operator==(const SplitPointFileParser &lhs,
00072 const SplitPointFileParser &rhs)
00073 {
00074
00075 return lhs.m_input == rhs.m_input;
00076 }
00077
00078 bool operator!=(const SplitPointFileParser &lhs,
00079 const SplitPointFileParser &rhs)
00080 {
00081 return !(lhs == rhs);
00082 }
00083
00084 }
00085 }
00086 }