diff options
author | Phil Williams <philip.williams@mac.com> | 2015-06-04 18:54:31 +0300 |
---|---|---|
committer | Phil Williams <philip.williams@mac.com> | 2015-06-04 18:54:31 +0300 |
commit | c6a3d8e54aa84933875160873d7bf837a6210b25 (patch) | |
tree | 7c60524b79d2b17e11bbbe30715e66b525354621 /phrase-extract | |
parent | 721bfe823bd4142f73499be371036c775887f9d5 (diff) |
Ongoing moses/phrase-extract refactoring
Diffstat (limited to 'phrase-extract')
-rw-r--r-- | phrase-extract/extract-ghkm/ExtractGHKM.cpp | 46 | ||||
-rw-r--r-- | phrase-extract/extract-ghkm/ExtractGHKM.h | 18 | ||||
-rw-r--r-- | phrase-extract/filter-rule-table/FilterRuleTable.cpp | 18 | ||||
-rw-r--r-- | phrase-extract/filter-rule-table/FilterRuleTable.h | 18 | ||||
-rw-r--r-- | phrase-extract/pcfg-extract/pcfg_extract.cc | 2 | ||||
-rw-r--r-- | phrase-extract/pcfg-score/pcfg_score.cc | 4 | ||||
-rw-r--r-- | phrase-extract/postprocess-egret-forests/Main.cpp | 6 | ||||
-rw-r--r-- | phrase-extract/postprocess-egret-forests/PostprocessEgretForests.cpp | 52 | ||||
-rw-r--r-- | phrase-extract/postprocess-egret-forests/PostprocessEgretForests.h | 18 | ||||
-rw-r--r-- | phrase-extract/score-stsg/ScoreStsg.cpp | 26 | ||||
-rw-r--r-- | phrase-extract/score-stsg/ScoreStsg.h | 15 | ||||
-rw-r--r-- | phrase-extract/syntax-common/tool.cc | 81 | ||||
-rw-r--r-- | phrase-extract/syntax-common/tool.h | 82 |
13 files changed, 97 insertions, 289 deletions
diff --git a/phrase-extract/extract-ghkm/ExtractGHKM.cpp b/phrase-extract/extract-ghkm/ExtractGHKM.cpp index 777e56f52..c2ee43767 100644 --- a/phrase-extract/extract-ghkm/ExtractGHKM.cpp +++ b/phrase-extract/extract-ghkm/ExtractGHKM.cpp @@ -359,39 +359,6 @@ int ExtractGHKM::Main(int argc, char *argv[]) return 0; } -void ExtractGHKM::OpenInputFileOrDie(const std::string &filename, - std::ifstream &stream) -{ - stream.open(filename.c_str()); - if (!stream) { - std::ostringstream msg; - msg << "failed to open input file: " << filename; - Error(msg.str()); - } -} - -void ExtractGHKM::OpenOutputFileOrDie(const std::string &filename, - std::ofstream &stream) -{ - stream.open(filename.c_str()); - if (!stream) { - std::ostringstream msg; - msg << "failed to open output file: " << filename; - Error(msg.str()); - } -} - -void ExtractGHKM::OpenOutputFileOrDie(const std::string &filename, - Moses::OutputFileStream &stream) -{ - bool ret = stream.Open(filename); - if (!ret) { - std::ostringstream msg; - msg << "failed to open output file: " << filename; - Error(msg.str()); - } -} - void ExtractGHKM::ProcessOptions(int argc, char *argv[], Options &options) const { @@ -401,7 +368,7 @@ void ExtractGHKM::ProcessOptions(int argc, char *argv[], // Construct the 'top' of the usage message: the bit that comes before the // options list. std::ostringstream usageTop; - usageTop << "Usage: " << GetName() + usageTop << "Usage: " << name() << " [OPTION]... TARGET SOURCE ALIGNMENT EXTRACT\n\n" << "SCFG rule extractor based on the GHKM algorithm described in\n" << "Galley et al. (2004).\n\n" @@ -547,11 +514,8 @@ void ExtractGHKM::ProcessOptions(int argc, char *argv[], // Process the command-line. po::variables_map vm; - const int optionStyle = cls::allow_long - | cls::long_allow_adjacent - | cls::long_allow_next; try { - po::store(po::command_line_parser(argc, argv).style(optionStyle). + po::store(po::command_line_parser(argc, argv).style(MosesOptionStyle()). options(cmdLineOptions).positional(p).run(), vm); po::notify(vm); } catch (const std::exception &e) { @@ -635,12 +599,6 @@ void ExtractGHKM::ProcessOptions(int argc, char *argv[], } } -void ExtractGHKM::Error(const std::string &msg) const -{ - std::cerr << GetName() << ": " << msg << std::endl; - std::exit(1); -} - std::vector<std::string> ExtractGHKM::ReadTokens(const std::string &s) const { std::vector<std::string> tokens; diff --git a/phrase-extract/extract-ghkm/ExtractGHKM.h b/phrase-extract/extract-ghkm/ExtractGHKM.h index 66c4c55f8..0d0fa8bf1 100644 --- a/phrase-extract/extract-ghkm/ExtractGHKM.h +++ b/phrase-extract/extract-ghkm/ExtractGHKM.h @@ -28,6 +28,8 @@ #include "OutputFileStream.h" #include "SyntaxTree.h" +#include "syntax-common/tool.h" + namespace MosesTraining { namespace GHKM @@ -35,22 +37,14 @@ namespace GHKM struct Options; -class ExtractGHKM +class ExtractGHKM : public Syntax::Tool { public: + ExtractGHKM() : Tool("extract-ghkm") {} - ExtractGHKM() : m_name("extract-ghkm") {} - const std::string &GetName() const { - return m_name; - } - int Main(int argc, char *argv[]); + virtual int Main(int argc, char *argv[]); private: - - void Error(const std::string &) const; - void OpenInputFileOrDie(const std::string &, std::ifstream &); - void OpenOutputFileOrDie(const std::string &, std::ofstream &); - void OpenOutputFileOrDie(const std::string &, Moses::OutputFileStream &); void RecordTreeLabels(const SyntaxTree &, std::set<std::string> &); void CollectWordLabelCounts(SyntaxTree &, const Options &, @@ -79,8 +73,6 @@ private: std::vector<std::string> ReadTokens(const SyntaxTree &root) const; void ProcessOptions(int, char *[], Options &) const; - - std::string m_name; }; } // namespace GHKM diff --git a/phrase-extract/filter-rule-table/FilterRuleTable.cpp b/phrase-extract/filter-rule-table/FilterRuleTable.cpp index 89e59b3e9..24c2803a7 100644 --- a/phrase-extract/filter-rule-table/FilterRuleTable.cpp +++ b/phrase-extract/filter-rule-table/FilterRuleTable.cpp @@ -167,7 +167,7 @@ void FilterRuleTable::ProcessOptions(int argc, char *argv[], // Construct the 'top' of the usage message: the bit that comes before the // options list. std::ostringstream usageTop; - usageTop << "Usage: " << GetName() + usageTop << "Usage: " << name() << " [OPTION]... MODEL TEST\n\n" << "Filter for SCFG/STSG rule tables.\n\n" << "Options"; @@ -203,11 +203,8 @@ void FilterRuleTable::ProcessOptions(int argc, char *argv[], // Process the command-line. po::variables_map vm; - const int optionStyle = cls::allow_long - | cls::long_allow_adjacent - | cls::long_allow_next; try { - po::store(po::command_line_parser(argc, argv).style(optionStyle). + po::store(po::command_line_parser(argc, argv).style(MosesOptionStyle()). options(cmdLineOptions).positional(p).run(), vm); po::notify(vm); } catch (const std::exception &e) { @@ -229,17 +226,6 @@ void FilterRuleTable::ProcessOptions(int argc, char *argv[], } } -void FilterRuleTable::Error(const std::string &msg) const -{ - std::cerr << GetName() << ": error: " << msg << std::endl; - std::exit(1); -} - -void FilterRuleTable::Warn(const std::string &msg) const -{ - std::cerr << GetName() << ": warning: " << msg << std::endl; -} - } // namespace FilterRuleTable } // namespace Syntax } // namespace MosesTraining diff --git a/phrase-extract/filter-rule-table/FilterRuleTable.h b/phrase-extract/filter-rule-table/FilterRuleTable.h index 3077e690d..7b51bb8fa 100644 --- a/phrase-extract/filter-rule-table/FilterRuleTable.h +++ b/phrase-extract/filter-rule-table/FilterRuleTable.h @@ -7,6 +7,8 @@ #include "SyntaxTree.h" +#include "syntax-common/tool.h" + #include "StringForest.h" namespace MosesTraining @@ -18,20 +20,14 @@ namespace FilterRuleTable struct Options; -class FilterRuleTable +class FilterRuleTable : public Tool { public: - FilterRuleTable() : m_name("filter-rule-table") {} - - const std::string &GetName() const { - return m_name; - } + FilterRuleTable() : Tool("filter-rule-table") {} - int Main(int argc, char *argv[]); + virtual int Main(int argc, char *argv[]); private: - void Error(const std::string &) const; - // Filter rule table (on std::cin) for test set (string version). void Filter(const std::vector<std::vector<std::string> > &); @@ -51,10 +47,6 @@ private: // Read test set (forest version) void ReadTestSet(std::istream &, std::vector<boost::shared_ptr<StringForest> > &); - - void Warn(const std::string &) const; - - std::string m_name; }; } // namespace FilterRuleTable diff --git a/phrase-extract/pcfg-extract/pcfg_extract.cc b/phrase-extract/pcfg-extract/pcfg_extract.cc index 45eb9ff3d..0e89e26be 100644 --- a/phrase-extract/pcfg-extract/pcfg_extract.cc +++ b/phrase-extract/pcfg-extract/pcfg_extract.cc @@ -118,7 +118,7 @@ void PcfgExtract::ProcessOptions(int argc, char *argv[], // Process the command-line. po::variables_map vm; try { - po::store(po::command_line_parser(argc, argv).style(CommonOptionStyle()). + po::store(po::command_line_parser(argc, argv).style(MosesOptionStyle()). options(cmd_line_options).positional(p).run(), vm); po::notify(vm); } catch (const std::exception &e) { diff --git a/phrase-extract/pcfg-score/pcfg_score.cc b/phrase-extract/pcfg-score/pcfg_score.cc index cec84211a..bdbb761f9 100644 --- a/phrase-extract/pcfg-score/pcfg_score.cc +++ b/phrase-extract/pcfg-score/pcfg_score.cc @@ -56,7 +56,7 @@ int PcfgScore::Main(int argc, char *argv[]) // Open PCFG stream. std::ifstream pcfg_stream; - OpenNamedInputOrDie(options.pcfg_file, pcfg_stream); + OpenInputFileOrDie(options.pcfg_file, pcfg_stream); // Read PCFG. Pcfg pcfg; @@ -131,7 +131,7 @@ void PcfgScore::ProcessOptions(int argc, char *argv[], Options &options) const // Process the command-line. po::variables_map vm; try { - po::store(po::command_line_parser(argc, argv).style(CommonOptionStyle()). + po::store(po::command_line_parser(argc, argv).style(MosesOptionStyle()). options(cmd_line_options).positional(p).run(), vm); po::notify(vm); } catch (const std::exception &e) { diff --git a/phrase-extract/postprocess-egret-forests/Main.cpp b/phrase-extract/postprocess-egret-forests/Main.cpp index ec2bab185..fead94652 100644 --- a/phrase-extract/postprocess-egret-forests/Main.cpp +++ b/phrase-extract/postprocess-egret-forests/Main.cpp @@ -5,9 +5,5 @@ int main(int argc, char *argv[]) { MosesTraining::Syntax::PostprocessEgretForests::PostprocessEgretForests tool; - try { - return tool.Main(argc, argv); - } catch (const MosesTraining::Syntax::Exception &e) { - tool.Error(e.msg()); - } + return tool.Main(argc, argv); } diff --git a/phrase-extract/postprocess-egret-forests/PostprocessEgretForests.cpp b/phrase-extract/postprocess-egret-forests/PostprocessEgretForests.cpp index d87e082dc..4911d4913 100644 --- a/phrase-extract/postprocess-egret-forests/PostprocessEgretForests.cpp +++ b/phrase-extract/postprocess-egret-forests/PostprocessEgretForests.cpp @@ -30,19 +30,23 @@ namespace PostprocessEgretForests int PostprocessEgretForests::Main(int argc, char *argv[]) { - // Process command-line options. - Options options; - ProcessOptions(argc, argv, options); - - // Open input files. - boost::scoped_ptr<SplitPointFileParser> splitPointParser; - std::ifstream splitPointFileStream; - if (!options.splitPointsFile.empty()) { - OpenInputFileOrDie(options.splitPointsFile, splitPointFileStream); - splitPointParser.reset(new SplitPointFileParser(splitPointFileStream)); - } + try { + // Process command-line options. + Options options; + ProcessOptions(argc, argv, options); + + // Open input files. + boost::scoped_ptr<SplitPointFileParser> splitPointParser; + std::ifstream splitPointFileStream; + if (!options.splitPointsFile.empty()) { + OpenInputFileOrDie(options.splitPointsFile, splitPointFileStream); + splitPointParser.reset(new SplitPointFileParser(splitPointFileStream)); + } - ProcessForest(std::cin, std::cout, splitPointParser.get(), options); + ProcessForest(std::cin, std::cout, splitPointParser.get(), options); + } catch (const MosesTraining::Syntax::Exception &e) { + Error(e.msg()); + } return 0; } @@ -76,17 +80,6 @@ void PostprocessEgretForests::ProcessForest( } } -void PostprocessEgretForests::OpenInputFileOrDie(const std::string &filename, - std::ifstream &stream) -{ - stream.open(filename.c_str()); - if (!stream) { - std::ostringstream msg; - msg << "failed to open input file: " << filename; - Error(msg.str()); - } -} - void PostprocessEgretForests::ProcessOptions(int argc, char *argv[], Options &options) const { @@ -96,7 +89,7 @@ void PostprocessEgretForests::ProcessOptions(int argc, char *argv[], // Construct the 'top' of the usage message: the bit that comes before the // options list. std::ostringstream usageTop; - usageTop << "Usage: " << GetName() + usageTop << "Usage: " << name() << " [OPTION]...\n\n" << "TODO\n\n" << "Options"; @@ -132,11 +125,8 @@ void PostprocessEgretForests::ProcessOptions(int argc, char *argv[], // Process the command-line. po::variables_map vm; - const int optionStyle = cls::allow_long - | cls::long_allow_adjacent - | cls::long_allow_next; try { - po::store(po::command_line_parser(argc, argv).style(optionStyle). + po::store(po::command_line_parser(argc, argv).style(MosesOptionStyle()). options(cmdLineOptions).positional(p).run(), vm); po::notify(vm); } catch (const std::exception &e) { @@ -156,12 +146,6 @@ void PostprocessEgretForests::ProcessOptions(int argc, char *argv[], } } -void PostprocessEgretForests::Error(const std::string &msg) const -{ - std::cerr << GetName() << ": " << msg << std::endl; - std::exit(1); -} - } // namespace PostprocessEgretForests } // namespace Syntax } // namespace MosesTraining diff --git a/phrase-extract/postprocess-egret-forests/PostprocessEgretForests.h b/phrase-extract/postprocess-egret-forests/PostprocessEgretForests.h index 95da24c71..51970084e 100644 --- a/phrase-extract/postprocess-egret-forests/PostprocessEgretForests.h +++ b/phrase-extract/postprocess-egret-forests/PostprocessEgretForests.h @@ -4,6 +4,8 @@ #include <ostream> #include <string> +#include "syntax-common/tool.h" + namespace MosesTraining { namespace Syntax @@ -14,31 +16,21 @@ namespace PostprocessEgretForests struct Options; class SplitPointFileParser; -class PostprocessEgretForests +class PostprocessEgretForests : public Tool { public: - PostprocessEgretForests() : m_name("postprocess-egret-forests") {} - - void Error(const std::string &) const; - - const std::string &GetName() const { - return m_name; - } + PostprocessEgretForests() : Tool("postprocess-egret-forests") {} - int Main(int argc, char *argv[]); + virtual int Main(int argc, char *argv[]); private: void OneBestTree(std::istream &, std::ostream &, SplitPointFileParser *, const Options &); - void OpenInputFileOrDie(const std::string &, std::ifstream &); - void ProcessForest(std::istream &, std::ostream &, SplitPointFileParser *, const Options &); void ProcessOptions(int, char *[], Options &) const; - - std::string m_name; }; } // namespace PostprocessEgretForests diff --git a/phrase-extract/score-stsg/ScoreStsg.cpp b/phrase-extract/score-stsg/ScoreStsg.cpp index 09395e21e..f6df0d0da 100644 --- a/phrase-extract/score-stsg/ScoreStsg.cpp +++ b/phrase-extract/score-stsg/ScoreStsg.cpp @@ -35,7 +35,7 @@ namespace ScoreStsg const int ScoreStsg::kCountOfCountsMax = 10; ScoreStsg::ScoreStsg() - : m_name("score-stsg") + : Tool("score-stsg") , m_lexTable(m_srcVocab, m_tgtVocab) , m_countOfCounts(kCountOfCountsMax, 0) , m_totalDistinct(0) @@ -300,17 +300,6 @@ double ScoreStsg::ComputeLexProb(const std::vector<RuleSymbol> &sourceFrontier, return lexScore; } -void ScoreStsg::OpenOutputFileOrDie(const std::string &filename, - Moses::OutputFileStream &stream) -{ - bool ret = stream.Open(filename); - if (!ret) { - std::ostringstream msg; - msg << "failed to open output file: " << filename; - Error(msg.str()); - } -} - void ScoreStsg::ProcessOptions(int argc, char *argv[], Options &options) const { namespace po = boost::program_options; @@ -319,7 +308,7 @@ void ScoreStsg::ProcessOptions(int argc, char *argv[], Options &options) const // Construct the 'top' of the usage message: the bit that comes before the // options list. std::ostringstream usageTop; - usageTop << "Usage: " << GetName() + usageTop << "Usage: " << name() << " [OPTION]... EXTRACT LEX TABLE\n\n" << "STSG rule scorer\n\n" << "Options"; @@ -386,11 +375,8 @@ void ScoreStsg::ProcessOptions(int argc, char *argv[], Options &options) const // Process the command-line. po::variables_map vm; - const int optionStyle = cls::allow_long - | cls::long_allow_adjacent - | cls::long_allow_next; try { - po::store(po::command_line_parser(argc, argv).style(optionStyle). + po::store(po::command_line_parser(argc, argv).style(MosesOptionStyle()). options(cmdLineOptions).positional(p).run(), vm); po::notify(vm); } catch (const std::exception &e) { @@ -440,12 +426,6 @@ void ScoreStsg::ProcessOptions(int argc, char *argv[], Options &options) const } } -void ScoreStsg::Error(const std::string &msg) const -{ - std::cerr << GetName() << ": " << msg << std::endl; - std::exit(1); -} - } // namespace ScoreStsg } // namespace Syntax } // namespace MosesTraining diff --git a/phrase-extract/score-stsg/ScoreStsg.h b/phrase-extract/score-stsg/ScoreStsg.h index 628c0080e..1757e181b 100644 --- a/phrase-extract/score-stsg/ScoreStsg.h +++ b/phrase-extract/score-stsg/ScoreStsg.h @@ -9,6 +9,8 @@ #include "ExtractionPhrasePair.h" #include "OutputFileStream.h" +#include "syntax-common/tool.h" + #include "LexicalTable.h" #include "Options.h" #include "RuleSymbol.h" @@ -25,16 +27,12 @@ namespace ScoreStsg class RuleGroup; class RuleTableWriter; -class ScoreStsg +class ScoreStsg : public Tool { public: ScoreStsg(); - const std::string &GetName() const { - return m_name; - } - - int Main(int argc, char *argv[]); + virtual int Main(int argc, char *argv[]); private: static const int kCountOfCountsMax; @@ -43,10 +41,6 @@ private: const std::vector<RuleSymbol> &, const ALIGNMENT &); - void Error(const std::string &) const; - - void OpenOutputFileOrDie(const std::string &, Moses::OutputFileStream &); - void ParseAlignmentString(const std::string &, int, ALIGNMENT &); @@ -59,7 +53,6 @@ private: void TokenizeRuleHalf(const std::string &, TokenizedRuleHalf &); - std::string m_name; Options m_options; Vocabulary m_srcVocab; Vocabulary m_tgtVocab; diff --git a/phrase-extract/syntax-common/tool.cc b/phrase-extract/syntax-common/tool.cc index c41eaf9bd..e145b78be 100644 --- a/phrase-extract/syntax-common/tool.cc +++ b/phrase-extract/syntax-common/tool.cc @@ -1,64 +1,30 @@ -/*********************************************************************** - Moses - statistical machine translation system - Copyright (C) 2006-2012 University of Edinburgh - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -***********************************************************************/ - #include "tool.h" +#include <cstdlib> +#include <iostream> #include <sstream> +#include <boost/program_options/cmdline.hpp> + namespace MosesTraining { namespace Syntax { -namespace PCFG { -std::istream &Tool::OpenInputOrDie(const std::string &filename) { - // TODO Check that function is only called once? - if (filename.empty() || filename == "-") { - input_ptr_ = &(std::cin); - } else { - input_file_stream_.open(filename.c_str()); - if (!input_file_stream_) { - std::ostringstream msg; - msg << "failed to open input file: " << filename; - Error(msg.str()); - } - input_ptr_ = &input_file_stream_; - } - return *input_ptr_; +int Tool::MosesOptionStyle() { + namespace cls = boost::program_options::command_line_style; + return cls::allow_long | cls::long_allow_adjacent | cls::long_allow_next; } -std::ostream &Tool::OpenOutputOrDie(const std::string &filename) { - // TODO Check that function is only called once? - if (filename.empty() || filename == "-") { - output_ptr_ = &(std::cout); - } else { - output_file_stream_.open(filename.c_str()); - if (!output_file_stream_) { - std::ostringstream msg; - msg << "failed to open output file: " << filename; - Error(msg.str()); - } - output_ptr_ = &output_file_stream_; - } - return *output_ptr_; +void Tool::Warn(const std::string &msg) const { + std::cerr << name_ << ": warning: " << msg << std::endl; } -void Tool::OpenNamedInputOrDie(const std::string &filename, - std::ifstream &stream) { +void Tool::Error(const std::string &msg) const { + std::cerr << name_ << ": error: " << msg << std::endl; + std::exit(1); +} + +void Tool::OpenInputFileOrDie(const std::string &filename, + std::ifstream &stream) { stream.open(filename.c_str()); if (!stream) { std::ostringstream msg; @@ -67,8 +33,8 @@ void Tool::OpenNamedInputOrDie(const std::string &filename, } } -void Tool::OpenNamedOutputOrDie(const std::string &filename, - std::ofstream &stream) { +void Tool::OpenOutputFileOrDie(const std::string &filename, + std::ofstream &stream) { stream.open(filename.c_str()); if (!stream) { std::ostringstream msg; @@ -77,6 +43,15 @@ void Tool::OpenNamedOutputOrDie(const std::string &filename, } } -} // namespace PCFG +void Tool::OpenOutputFileOrDie(const std::string &filename, + Moses::OutputFileStream &stream) { + bool ret = stream.Open(filename); + if (!ret) { + std::ostringstream msg; + msg << "failed to open output file: " << filename; + Error(msg.str()); + } +} + } // namespace Syntax } // namespace MosesTraining diff --git a/phrase-extract/syntax-common/tool.h b/phrase-extract/syntax-common/tool.h index 2c903a11e..e1df8025f 100644 --- a/phrase-extract/syntax-common/tool.h +++ b/phrase-extract/syntax-common/tool.h @@ -1,93 +1,53 @@ -/*********************************************************************** - Moses - statistical machine translation system - Copyright (C) 2006-2012 University of Edinburgh - - This library is free software; you can redistribute it and/or - modify it under the terms of the GNU Lesser General Public - License as published by the Free Software Foundation; either - version 2.1 of the License, or (at your option) any later version. - - This library is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - Lesser General Public License for more details. - - You should have received a copy of the GNU Lesser General Public - License along with this library; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA -***********************************************************************/ - #pragma once -#ifndef PCFG_TOOL_H_ -#define PCFG_TOOL_H_ -#include <cstdlib> #include <fstream> -#include <iostream> #include <string> -#include <boost/program_options/cmdline.hpp> +#include "OutputFileStream.h" namespace MosesTraining { namespace Syntax { -namespace PCFG { +/*! Base class for command-line based tools. + */ class Tool { public: virtual ~Tool() {} + //! Get the name of the tool. const std::string &name() const { return name_; } + //! Virtual main function to be provided by subclass. virtual int Main(int argc, char *argv[]) = 0; protected: Tool(const std::string &name) : name_(name) {} - // Returns the boost::program_options style that should be used by all tools. - static int CommonOptionStyle() { - namespace cls = boost::program_options::command_line_style; - return cls::default_style & (~cls::allow_guessing); - } - - void Warn(const std::string &msg) const { - std::cerr << name_ << ": warning: " << msg << std::endl; - } + //! Returns a boost::program_options style that is consistent with other + //! Moses tools (extract-rules, score, etc.). + static int MosesOptionStyle(); - void Error(const std::string &msg) const { - std::cerr << name_ << ": error: " << msg << std::endl; - std::exit(1); - } + //! Write a formatted warning message to standard error. + void Warn(const std::string &) const; - // Initialises the tool's main input stream and returns a reference that is - // valid for the remainder of the tool's lifetime. If filename is empty or - // "-" then input is standard input; otherwise it is the named file. Calls - // Error() if the file cannot be opened for reading. - std::istream &OpenInputOrDie(const std::string &filename); + //! Write a formatted error message to standard error and call exit(1). + void Error(const std::string &msg) const; - // Initialises the tool's main output stream and returns a reference that is - // valid for the remainder of the tool's lifetime. If filename is empty or - // "-" then output is standard output; otherwise it is the named file. Calls - // Error() if the file cannot be opened for writing. - std::ostream &OpenOutputOrDie(const std::string &filename); + //! Opens the named input file using the supplied ifstream. Calls Error() if + //! the file cannot be opened for reading. + void OpenInputFileOrDie(const std::string &, std::ifstream &); - // Opens the named input file using the supplied ifstream. Calls Error() if - // the file cannot be opened for reading. - void OpenNamedInputOrDie(const std::string &, std::ifstream &); + //! Opens the named output file using the supplied ofstream. Calls Error() if + //! the file cannot be opened for writing. + void OpenOutputFileOrDie(const std::string &, std::ofstream &); - // Opens the named output file using the supplied ofstream. Calls Error() if - // the file cannot be opened for writing. - void OpenNamedOutputOrDie(const std::string &, std::ofstream &); + //! Opens the named output file using the supplied OutputFileStream. Calls + //! Error() if the file cannot be opened for writing. + void OpenOutputFileOrDie(const std::string &, Moses::OutputFileStream &); private: std::string name_; - std::istream *input_ptr_; - std::ifstream input_file_stream_; - std::ostream *output_ptr_; - std::ofstream output_file_stream_; }; -} // namespace PCFG } // namespace Syntax } // namespace MosesTraining - -#endif |