- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

# /home/taku/.proj/crfpp/crfpp.h

Go to the documentation of this file.

    00001 /*
    00002   CRF++ -- Yet Another CRF toolkit
    00003
    00004   $Id: crfpp.h 1592 2007-02-12 09:40:53Z taku $;
    00005
    00006   Copyright(C) 2005-2007 Taku Kudo <taku@chasen.org>
    00007 */
    00008 #ifndef CRFPP_CRFPP_H_
    00009 #define CRFPP_CRFPP_H_
    00010
    00011 /* C interface  */
    00012 #ifdef __cplusplus
    00013 #include <cstdio>
    00014 #else
    00015 #include <stdio.h>
    00016 #endif
    00017
    00018 #ifdef __cplusplus
    00019 extern "C" {
    00020 #endif
    00021
    00022 #ifdef _WIN32
    00023 #include <windows.h>
    00024 #  ifdef DLL_EXPORT
    00025 #    define CRFPP_DLL_EXTERN  __declspec(dllexport)
    00026 #    define CRFPP_DLL_CLASS_EXTERN  __declspec(dllexport)
    00027 #  else
    00028 #    define CRFPP_DLL_EXTERN  __declspec(dllimport)
    00029 #  endif
    00030 #endif
    00031
    00032 #ifndef CRFPP_DLL_EXTERN
    00033 #  define CRFPP_DLL_EXTERN extern
    00034 #endif
    00035
    00036 #ifndef CRFPP_DLL_CLASS_EXTERN
    00037 #  define CRFPP_DLL_CLASS_EXTERN
    00038 #endif
    00039
    00040 #ifndef SWIG
    00041   typedef struct crfpp_t crfpp_t;
    00042   typedef struct crfpp_model_t crfpp_model_t;
    00043
    00044   /* C interface */
    00045   CRFPP_DLL_EXTERN crfpp_model_t* crfpp_model_new(int,  char**);
    00046   CRFPP_DLL_EXTERN crfpp_model_t* crfpp_model_new2(const char*);
    00047   CRFPP_DLL_EXTERN const char *   crfpp_model_strerror(crfpp_model_t *);
    00048   CRFPP_DLL_EXTERN crfpp_t*       crfpp_model_new_tagger(crfpp_model_t *);
    00049
    00050   CRFPP_DLL_EXTERN crfpp_t* crfpp_new(int,  char**);
    00051   CRFPP_DLL_EXTERN crfpp_t* crfpp_new2(const char*);
    00052   CRFPP_DLL_EXTERN void     crfpp_destroy(crfpp_t*);
    00053   CRFPP_DLL_EXTERN bool     crfpp_add2(crfpp_t*, size_t, const char **);
    00054   CRFPP_DLL_EXTERN bool     crfpp_add(crfpp_t*, const char*);
    00055   CRFPP_DLL_EXTERN size_t   crfpp_size(crfpp_t*);
    00056   CRFPP_DLL_EXTERN size_t   crfpp_xsize(crfpp_t*);
    00057   CRFPP_DLL_EXTERN size_t   crfpp_dsize(crfpp_t*);
    00058   CRFPP_DLL_EXTERN const float* crfpp_weight_vector(crfpp_t*);
    00059   CRFPP_DLL_EXTERN size_t   crfpp_result(crfpp_t*, size_t);
    00060   CRFPP_DLL_EXTERN size_t   crfpp_answer(crfpp_t*, size_t);
    00061   CRFPP_DLL_EXTERN size_t   crfpp_y(crfpp_t*, size_t);
    00062   CRFPP_DLL_EXTERN size_t   crfpp_ysize(crfpp_t*);
    00063   CRFPP_DLL_EXTERN double   crfpp_prob(crfpp_t*, size_t, size_t);
    00064   CRFPP_DLL_EXTERN double   crfpp_prob2(crfpp_t*, size_t);
    00065   CRFPP_DLL_EXTERN double   crfpp_prob3(crfpp_t*);
    00066   CRFPP_DLL_EXTERN void     crfpp_set_penalty(crfpp_t *, size_t i, size_t j, double penalty);
    00067   CRFPP_DLL_EXTERN double   crfpp_penalty(crfpp_t *, size_t i, size_t j);
    00068   CRFPP_DLL_EXTERN double   crfpp_alpha(crfpp_t*, size_t, size_t);
    00069   CRFPP_DLL_EXTERN double   crfpp_beta(crfpp_t*, size_t, size_t);
    00070   CRFPP_DLL_EXTERN double   crfpp_emisstion_cost(crfpp_t*, size_t, size_t);
    00071   CRFPP_DLL_EXTERN double   crfpp_next_transition_cost(crfpp_t*, size_t,
    00072                                                        size_t, size_t);
    00073   CRFPP_DLL_EXTERN double   crfpp_prev_transition_cost(crfpp_t*, size_t,
    00074                                                        size_t, size_t);
    00075   CRFPP_DLL_EXTERN double   crfpp_best_cost(crfpp_t*, size_t, size_t);
    00076   CRFPP_DLL_EXTERN const int* crfpp_emittion_vector(crfpp_t*, size_t, size_t);
    00077   CRFPP_DLL_EXTERN const int* crfpp_next_transition_vector(crfpp_t*, size_t,
    00078                                                            size_t, size_t);
    00079   CRFPP_DLL_EXTERN const int* crfpp_prev_transition_vector(crfpp_t*, size_t,
    00080                                                            size_t, size_t);
    00081   CRFPP_DLL_EXTERN double   crfpp_Z(crfpp_t*);
    00082   CRFPP_DLL_EXTERN bool     crfpp_parse(crfpp_t*);
    00083   CRFPP_DLL_EXTERN bool     crfpp_empty(crfpp_t*);
    00084   CRFPP_DLL_EXTERN bool     crfpp_clear(crfpp_t*);
    00085   CRFPP_DLL_EXTERN bool     crfpp_next(crfpp_t*);
    00086   CRFPP_DLL_EXTERN int      crfpp_test(int, char **);
    00087   CRFPP_DLL_EXTERN int      crfpp_learn(int, char **);
    00088   CRFPP_DLL_EXTERN const char*  crfpp_strerror(crfpp_t*);
    00089   CRFPP_DLL_EXTERN const char*  crfpp_yname(crfpp_t*, size_t);
    00090   CRFPP_DLL_EXTERN const char*  crfpp_y2(crfpp_t*, size_t);
    00091   CRFPP_DLL_EXTERN const char*  crfpp_x(crfpp_t*, size_t, size_t);
    00092   CRFPP_DLL_EXTERN const char** crfpp_x2(crfpp_t*, size_t);
    00093   CRFPP_DLL_EXTERN const char*  crfpp_parse_tostr(crfpp_t*, const char*);
    00094   CRFPP_DLL_EXTERN const char*  crfpp_parse_tostr2(crfpp_t*,
    00095                                                    const char*, size_t);
    00096   CRFPP_DLL_EXTERN const char*  crfpp_parse_tostr3(crfpp_t*, const char*,
    00097                                                    size_t, char *, size_t);
    00098   CRFPP_DLL_EXTERN const char*  crfpp_tostr(crfpp_t*);
    00099   CRFPP_DLL_EXTERN const char*  crfpp_tostr2(crfpp_t*, char *, size_t);
    00100
    00101   CRFPP_DLL_EXTERN void crfpp_set_vlevel(crfpp_t *, unsigned int);
    00102   CRFPP_DLL_EXTERN unsigned int crfpp_vlevel(crfpp_t *);
    00103   CRFPP_DLL_EXTERN void crfpp_set_cost_factor(crfpp_t *, float);
    00104   CRFPP_DLL_EXTERN float crfpp_cost_factor(crfpp_t *);
    00105   CRFPP_DLL_EXTERN void crfpp_set_nbest(crfpp_t *, size_t);
    00106 #endif
    00107
    00108 #ifdef __cplusplus
    00109 }
    00110 #endif
    00111
    00112 /* C++ interface */
    00113 #ifdef __cplusplus
    00114
    00115 namespace CRFPP {
    00116
    00117 class Tagger;
    00118
    00119 class CRFPP_DLL_CLASS_EXTERN Model {
    00120  public:
    00121 #ifndef SWIG
    00122   // open model with parameters in argv[]
    00123   // e.g, argv[] = {"CRF++", "-m", "model", "-v3"};
    00124   virtual bool open(int argc,  char** argv) = 0;
    00125
    00126   // open model with parameter arg, e.g. arg = "-m model -v3";
    00127   virtual bool open(const char* arg) = 0;
    00128 #endif
    00129
    00130   // create Tagger object. Returned object shared the same
    00131   // model object
    00132   virtual Tagger *createTagger() const = 0;
    00133
    00134   virtual const char* what() = 0;
    00135
    00136   virtual ~Model() {}
    00137 };
    00138
    00139 class CRFPP_DLL_CLASS_EXTERN Tagger {
    00140  public:
    00141 #ifndef SWIG
    00142   // open model with parameters in argv[]
    00143   // e.g, argv[] = {"CRF++", "-m", "model", "-v3"};
    00144   virtual bool open(int argc,  char** argv) = 0;
    00145
    00146   // open model with parameter arg, e.g. arg = "-m model -v3";
    00147   virtual bool open(const char* arg) = 0;
    00148
    00149   // add str[] as tokens to the current context
    00150   virtual bool add(size_t size, const char **str) = 0;
    00151
    00152   // close the current model
    00153   virtual void close() = 0;
    00154
    00155   // return parameter vector. the size should be dsize();
    00156   virtual const float *weight_vector() const = 0;
    00157 #endif
    00158
    00159   // set vlevel
    00160   virtual void set_vlevel(unsigned int vlevel) = 0;
    00161
    00162   // get vlevel
    00163   virtual unsigned int vlevel() const = 0;
    00164
    00165   // set cost factor
    00166   virtual void set_cost_factor(float cost_factor) = 0;
    00167
    00168   // get cost factor
    00169   virtual float cost_factor() const = 0;
    00170
    00171   // set nbest
    00172   virtual void set_nbest(size_t nbest) = 0;
    00173
    00174   // get nbest
    00175   virtual size_t nbest() const = 0;
    00176
    00177   // add one line to the current context
    00178   virtual bool add(const char* str) = 0;
    00179
    00180   // return size of tokens(lines)
    00181   virtual size_t size() const = 0;
    00182
    00183   // return size of column
    00184   virtual size_t xsize() const = 0;
    00185
    00186   // return size of features
    00187   virtual size_t dsize() const = 0;
    00188
    00189   // return output tag-id of i-th token
    00190   virtual size_t result(size_t i) const = 0;
    00191
    00192   // return answer tag-id of i-th token if it is available
    00193   virtual size_t answer(size_t i) const = 0;
    00194
    00195   // alias of result(i)
    00196   virtual size_t y(size_t i) const = 0;
    00197
    00198   // return output tag of i-th token as string
    00199   virtual const char*   y2(size_t i) const = 0;
    00200
    00201   // return i-th tag-id as string
    00202   virtual const char*   yname(size_t i) const = 0;
    00203
    00204   // return token at [i,j] as string(i:token j:column)
    00205   virtual const char*   x(size_t i, size_t j) const = 0;
    00206
    00207 #ifndef SWIG
    00208   // return an array of strings at i-th tokens
    00209   virtual const char**  x(size_t) const = 0;
    00210 #endif
    00211
    00212   // return size of output tags
    00213   virtual size_t ysize() const = 0;
    00214
    00215   // return marginal probability of j-th tag id at i-th token
    00216   virtual double prob(size_t i, size_t j) const = 0;
    00217
    00218   // return marginal probability of output tag at i-th token
    00219   // same as prob(i, tagger->y(i));
    00220   virtual double prob(size_t i) const = 0;
    00221
    00222   // return conditional probability of enter output
    00223   virtual double prob() const = 0;
    00224
    00225   // set token-level penalty. It would be useful for implementing
    00226   // Dual decompositon decoding.
    00227   // e.g.
    00228   // "Dual Decomposition for Parsing with Non-Projective Head Automata"
    00229   // Terry Koo Alexander M. Rush Michael Collins Tommi Jaakkola David Sontag
    00230   virtual void set_penalty(size_t i, size_t j, double penalty) = 0;
    00231   virtual double penalty(size_t i, size_t j) const = 0;
    00232
    00233   // return forward log-prob of the j-th tag at i-th token
    00234   virtual double alpha(size_t i, size_t j) const = 0;
    00235
    00236   // return backward log-prob of the j-th tag at i-th token
    00237   virtual double beta(size_t i, size_t j) const = 0;
    00238
    00239   // return emission cost of the j-th tag at i-th token
    00240   virtual double emission_cost(size_t i, size_t j) const = 0;
    00241
    00242   // return transition cost of [j-th tag at i-th token] to
    00243   // [k-th tag at(i+1)-th token]
    00244   virtual double next_transition_cost(size_t i,
    00245                                       size_t j, size_t k) const = 0;
    00246
    00247   // return transition cost of [j-th tag at i-th token] to
    00248   // [k-th tag at(i-1)-th token]
    00249   virtual double prev_transition_cost(size_t i,
    00250                                       size_t j, size_t k) const = 0;
    00251
    00252   //  return the best accumulative cost to the j-th tag at i-th token
    00253   // used in viterbi search
    00254   virtual double best_cost(size_t i, size_t j) const = 0;
    00255
    00256 #ifndef SWIG
    00257   // return emission feature vector of the j-th tag at i-th token
    00258   virtual const int* emission_vector(size_t i, size_t j) const = 0;
    00259
    00260   // return transition feature vector of [j-th tag at i-th token] to
    00261   // [k-th tag at(i+1)-th token]
    00262   virtual const int* next_transition_vector(size_t i,
    00263                                             size_t j, size_t k) const = 0;
    00264
    00265   // return transition feature vector of [j-th tag at i-th token] to
    00266   // [k-th tag at(i-1)-th token]
    00267   virtual const int* prev_transition_vector(size_t i,
    00268                                             size_t j, size_t k) const = 0;
    00269 #endif
    00270
    00271   // normalizing factor(log-prob)
    00272   virtual double Z() const = 0;
    00273
    00274   // do parse and change the internal status, if failed, returns false
    00275   virtual bool parse() = 0;
    00276
    00277   // return true if the context is empty
    00278   virtual bool empty() const = 0;
    00279
    00280   // clear all context
    00281   virtual bool clear() = 0;
    00282
    00283   // change the internal state to output next-optimal output.
    00284   // calling it n-th times, can get n-best results,
    00285   // Neeed to specify -nN option to use this function, where
    00286   // N>=2
    00287   virtual bool next() = 0;
    00288
    00289   // parse 'str' and return result as string
    00290   // 'str' must be written in CRF++'s input format
    00291   virtual const char* parse(const char* str) = 0;
    00292
    00293 #ifndef SWIG
    00294   // return parsed result as string
    00295   virtual const char* toString() = 0;
    00296
    00297   // return parsed result as string.
    00298   // Result is saved in the buffer 'result', 'size' is the
    00299   // size of the buffer. if failed, return NULL
    00300   virtual const char* toString(char* result , size_t size) = 0;
    00301
    00302   // parse 'str' and return parsed result.
    00303   // You don't need to delete return value, but the buffer
    00304   // is rewritten whenever you call parse method.
    00305   // if failed, return NULL
    00306   virtual const char* parse(const char *str, size_t size) = 0;
    00307
    00308   // parse 'str' and return parsed result.
    00309   // The result is stored in the buffer 'result'.
    00310   // 'size2' is the size of the buffer. if failed, return NULL
    00311   virtual const char* parse(const char *str, size_t size1,
    00312                             char *result, size_t size2) = 0;
    00313 #endif
    00314   // return internal error code as string
    00315   virtual const char* what() = 0;
    00316
    00317   virtual ~Tagger() {}
    00318 };
    00319
    00320 /* factory method */
    00321
    00322 // create CRFPP::Tagger instance with parameters in argv[]
    00323 // e.g, argv[] = {"CRF++", "-m", "model", "-v3"};
    00324 CRFPP_DLL_EXTERN Tagger *createTagger(int argc, char **argv);
    00325
    00326 // create CRFPP::Tagger instance with parameter in arg
    00327 // e.g. arg = "-m model -v3";
    00328 CRFPP_DLL_EXTERN Tagger *createTagger(const char *arg);
    00329
    00330 // create CRFPP::Model instance with parameters in argv[]
    00331 // e.g, argv[] = {"CRF++", "-m", "model", "-v3"};
    00332 CRFPP_DLL_EXTERN Model *createModel(int argc, char **argv);
    00333
    00334 // create CRFPP::Model instance with parameter in arg
    00335 // e.g. arg = "-m model -v3";
    00336 CRFPP_DLL_EXTERN Model *createModel(const char *arg);
    00337
    00338 // return error code of createTagger();
    00339 CRFPP_DLL_EXTERN const char *getTaggerError();
    00340
    00341 // alias of getTaggerError();
    00342 CRFPP_DLL_EXTERN const char *getLastError();
    00343 }
    00344
    00345 #endif
    00346 #endif