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 Tanl_Corpus_Corpus_H
00025 #define Tanl_Corpus_Corpus_H
00026
00027
00028 #include "Language.h"
00029 #include "SentenceReader.h"
00030
00031
00032 #include <map>
00033 #include <stdexcept>
00034
00035 namespace Tanl {
00036
00042 struct TokenField
00043 {
00045 enum Use {
00046 input,
00047 output,
00048 echo,
00049 ignore
00050 };
00051
00053 enum Value {
00054 string,
00055 integer
00056 };
00057
00058 enum Role {
00059 none,
00060 form,
00061 head,
00062 deprel,
00063 predicate
00064 };
00065
00066 TokenField() :
00067 use(input),
00068 value(string),
00069 role(none),
00070 default_("_")
00071 { }
00072
00073 std::string name;
00074 Use use;
00075 Value value;
00076 Role role;
00077 std::string default_;
00078 std::string link;
00079 std::string label;
00080 };
00081
00082 typedef std::vector<TokenField> TokenFields;
00083
00089 struct CorpusFormat
00090 {
00091 CorpusFormat()
00092 { }
00093
00094 ~CorpusFormat() { }
00095
00096 void clear() { index.clear(); }
00097
00098 std::string name;
00099 TokenFields tokenFields;
00100 AttributeIndex index;
00101 };
00102
00106 class CorpusFormatError : public std::runtime_error
00107 {
00108 public:
00109 CorpusFormatError(std::string const& msg) : std::runtime_error(msg) {}
00110 };
00111
00117 class Corpus
00118 {
00119 public:
00120 Language const& language;
00121 AttributeIndex index;
00122 TokenFields tokenFields;
00123
00127 Corpus(Language const& lang) :
00128 language(lang)
00129 { }
00130
00135 Corpus(Language const& lang, CorpusFormat& format) :
00136 language(lang),
00137 tokenFields(format.tokenFields),
00138 index(format.index)
00139 { }
00140
00145 Corpus(Language const& lang, char const* formatFile);
00146
00154 static Corpus* create(Language const& language, char const* inputFormat);
00155
00156 static Corpus* create(char const* language, char const* inputFormat);
00157
00158 virtual ~Corpus() {}
00159
00164 static CorpusFormat* parseFormat(char const* formatFile);
00165
00168 AttributeId attributeId(const char* name) {
00169 return index.insert(name);
00170 }
00171
00176 virtual SentenceReader* sentenceReader(std::istream* is);
00177
00181 virtual void print(std::ostream& os, Sentence const& sent) const;
00182 virtual std::string toString(Sentence const& sent) const;
00183
00184 protected:
00185 static CorpusFormat* parseFormat(std::istream& is);
00186
00187 };
00188
00190
00191 typedef Corpus* CorpusFactory(Language const& lang, CorpusFormat* format);
00192
00198 struct CorpusMap
00199 {
00200 public:
00201 CorpusMap(char const* type, CorpusFactory* rf)
00202 {
00203 get()[type] = rf;
00204 }
00205
00206 static CorpusFactory* get(char const* type);
00207
00208 private:
00209 static std::map<char const*, CorpusFactory*>& get();
00210 };
00211
00212 #define REGISTER_CORPUS(type, factory) static CorpusMap __dummy ## type(#type, factory)
00213
00214 }
00215
00216 #endif // Tanl_Corpus_Corpus_H