00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef DeSR_Parser_H
00025 #define DeSR_Parser_H
00026
00027 #include "IPipe.h"
00028 #include "SentenceReader.h"
00029 #include "ReviseEventStream.h"
00030 #include "WordCounts.h"
00031
00032
00033 #include "conf/conf_bool.h"
00034 #include "conf/conf_int.h"
00035 #include "conf/conf_string.h"
00036
00037 namespace Parser {
00038
00039 extern IXE::conf<std::string> algorithm;
00040 extern IXE::conf<std::string> lang;
00041 extern IXE::conf<int> beam;
00042
00043 struct GlobalInfo
00044 {
00045 WordCounts timeLemmas;
00046 WordCounts locLemmas;
00047
00048 static float const freqRatio;
00049
00053 virtual void extract(Sentence const& sentence);
00054
00058 void clearRareEntities();
00059
00060 void save(std::ofstream& ofs);
00061
00062 void load(std::ifstream& ifs);
00063
00064 };
00065
00066 struct ParserPipe;
00067
00071
00072 class Parser : public IPipe<Sentence*, Sentence*>
00073 {
00074 public:
00075
00076 Parser(WordIndex& predIndex) :
00077 predIndex(predIndex)
00078 { }
00079
00080 ~Parser() { freeLanguageTable(); }
00081
00085 static Parser* create(char const* modelFile = 0);
00086
00091 virtual void train(SentenceReader* sentenceReader, char const* modelFile) = 0;
00096 virtual Sentence* parse(Sentence* sentence) = 0;
00097
00102 virtual void parse(SentenceReader* sentenceReader, std::ostream& os = std::cout);
00103
00111 virtual void revise(SentenceReader* sentenceReader, char const* actionFile = 0) { }
00112
00114 virtual void showEval(int tokenCount, int las, int uas, int sentCount);
00115
00117 void writeHeader(std::ostream& os);
00118
00122 static bool readHeader(std::istream& is);
00123
00128 Enumerator<Sentence*>* pipe(Enumerator<std::vector<Token*>*>& tce);
00129
00134 Enumerator<Sentence*>* pipe(Enumerator<Sentence*>& tce);
00135
00136 WordIndex& predIndex;
00137 GlobalInfo info;
00138
00140 static IXE::conf<int> featureCutoff;
00142 static IXE::conf<bool> verbose;
00143 };
00144
00145 struct ParserPipe : public Enumerator<Sentence*>
00146 {
00147 public:
00148 ParserPipe(Parser& parser, Enumerator<std::vector<Token*>*>& tve);
00149
00150 bool MoveNext();
00151
00152 Sentence* Current();
00153
00154 private:
00155 Parser& parser;
00156 Enumerator<std::vector<Token*>*>& tve;
00157 Language const* language;
00158 };
00159
00160 struct ParserSentPipe : public Enumerator<Sentence*>
00161 {
00162 public:
00163 ParserSentPipe(Parser& parser, Enumerator<Sentence*>& tve);
00164
00165 bool MoveNext();
00166
00167 Sentence* Current();
00168
00169 private:
00170 Parser& parser;
00171 Enumerator<Sentence*>& tve;
00172 };
00173
00175
00176 typedef Parser* ParserFactory(char const* = 0);
00177
00181 struct ParserMap
00182 {
00183 public:
00184 ParserMap(char const* type, ParserFactory* pf)
00185 {
00186 get()[type] = pf;
00187 }
00188
00189 static ParserFactory* get(char const* type);
00190
00191 private:
00192 static std::map<char const*, ParserFactory*>& get();
00193 };
00194
00195 #define REGISTER_PARSER(type, factory) static ParserMap __dummy ## type(#type, factory)
00196
00197 }
00198
00199 #endif // DeSR_Parser_H