00001 #ifndef LM_BUILDER_OUTPUT_H
00002 #define LM_BUILDER_OUTPUT_H
00003
00004 #include "lm/builder/header_info.hh"
00005 #include "lm/common/model_buffer.hh"
00006 #include "util/file.hh"
00007
00008 #include <boost/ptr_container/ptr_vector.hpp>
00009 #include <boost/utility.hpp>
00010
00011 namespace util { namespace stream { class Chains; class ChainPositions; } }
00012
00013
00014 namespace lm { namespace builder {
00015
00016
00017 enum HookType {
00018
00019 PROB_PARALLEL_HOOK,
00020 PROB_SEQUENTIAL_HOOK,
00021 NUMBER_OF_HOOKS
00022 };
00023
00024 class OutputHook {
00025 public:
00026 explicit OutputHook(HookType hook_type) : type_(hook_type) {}
00027
00028 virtual ~OutputHook();
00029
00030 virtual void Sink(const HeaderInfo &info, int vocab_file, util::stream::Chains &chains) = 0;
00031
00032 HookType Type() const { return type_; }
00033
00034 private:
00035 HookType type_;
00036 };
00037
00038 class Output : boost::noncopyable {
00039 public:
00040 Output(StringPiece file_base, bool keep_buffer, bool output_q);
00041
00042
00043 void Add(OutputHook *hook) {
00044 outputs_[hook->Type()].push_back(hook);
00045 }
00046
00047 bool Have(HookType hook_type) const {
00048 return !outputs_[hook_type].empty();
00049 }
00050
00051 int VocabFile() const { return buffer_.VocabFile(); }
00052
00053 void SetHeader(const HeaderInfo &header) { header_ = header; }
00054 const HeaderInfo &GetHeader() const { return header_; }
00055
00056
00057 void SinkProbs(util::stream::Chains &chains);
00058
00059 unsigned int Steps() const { return Have(PROB_SEQUENTIAL_HOOK); }
00060
00061 private:
00062 void Apply(HookType hook_type, util::stream::Chains &chains);
00063
00064 ModelBuffer buffer_;
00065
00066 boost::ptr_vector<OutputHook> outputs_[NUMBER_OF_HOOKS];
00067 HeaderInfo header_;
00068 };
00069
00070 class PrintHook : public OutputHook {
00071 public:
00072
00073 PrintHook(int write_fd, bool verbose_header)
00074 : OutputHook(PROB_SEQUENTIAL_HOOK), file_(write_fd), verbose_header_(verbose_header) {}
00075
00076 void Sink(const HeaderInfo &info, int vocab_file, util::stream::Chains &chains);
00077
00078 private:
00079 util::scoped_fd file_;
00080 bool verbose_header_;
00081 };
00082
00083 }}
00084
00085 #endif // LM_BUILDER_OUTPUT_H