- Main Page
- Namespaces
- Classes
- Files

# /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 #  else
    00027 #    define CRFPP_DLL_EXTERN  __declspec(dllimport)
    00028 #  endif
    00029 #endif
    00030
    00031 #ifndef CRFPP_DLL_EXTERN
    00032 #  define CRFPP_DLL_EXTERN extern
    00033 #endif
    00034
    00035 #ifndef SWIG
    00036   typedef struct crfpp_t crfpp_t;
    00037
    00038   /* C interface */
    00039   CRFPP_DLL_EXTERN crfpp_t* crfpp_new(int,  char**);
    00040   CRFPP_DLL_EXTERN crfpp_t* crfpp_new2(const char*);
    00041   CRFPP_DLL_EXTERN void     crfpp_destroy(crfpp_t*);
    00042   CRFPP_DLL_EXTERN bool     crfpp_add2(crfpp_t*, size_t, const char **);
    00043   CRFPP_DLL_EXTERN bool     crfpp_add(crfpp_t*, const char*);
    00044   CRFPP_DLL_EXTERN size_t   crfpp_size(crfpp_t*);
    00045   CRFPP_DLL_EXTERN size_t   crfpp_xsize(crfpp_t*);
    00046   CRFPP_DLL_EXTERN size_t   crfpp_dsize(crfpp_t*);
    00047   CRFPP_DLL_EXTERN const float* crfpp_weight_vector(crfpp_t*);
    00048   CRFPP_DLL_EXTERN size_t   crfpp_result(crfpp_t*, size_t);
    00049   CRFPP_DLL_EXTERN size_t   crfpp_answer(crfpp_t*, size_t);
    00050   CRFPP_DLL_EXTERN size_t   crfpp_y(crfpp_t*, size_t);
    00051   CRFPP_DLL_EXTERN size_t   crfpp_ysize(crfpp_t*);
    00052   CRFPP_DLL_EXTERN double   crfpp_prob(crfpp_t*, size_t, size_t);
    00053   CRFPP_DLL_EXTERN double   crfpp_prob2(crfpp_t*, size_t);
    00054   CRFPP_DLL_EXTERN double   crfpp_prob3(crfpp_t*);
    00055   CRFPP_DLL_EXTERN double   crfpp_alpha(crfpp_t*, size_t, size_t);
    00056   CRFPP_DLL_EXTERN double   crfpp_beta(crfpp_t*, size_t, size_t);
    00057   CRFPP_DLL_EXTERN double   crfpp_emisstion_cost(crfpp_t*, size_t, size_t);
    00058   CRFPP_DLL_EXTERN double   crfpp_next_transition_cost(crfpp_t*, size_t,
    00059                                                        size_t, size_t);
    00060   CRFPP_DLL_EXTERN double   crfpp_prev_transition_cost(crfpp_t*, size_t,
    00061                                                        size_t, size_t);
    00062   CRFPP_DLL_EXTERN double   crfpp_best_cost(crfpp_t*, size_t, size_t);
    00063   CRFPP_DLL_EXTERN const int* crfpp_emittion_vector(crfpp_t*, size_t, size_t);
    00064   CRFPP_DLL_EXTERN const int* crfpp_next_transition_vector(crfpp_t*, size_t,
    00065                                                            size_t, size_t);
    00066   CRFPP_DLL_EXTERN const int* crfpp_prev_transition_vector(crfpp_t*, size_t,
    00067                                                            size_t, size_t);
    00068   CRFPP_DLL_EXTERN double   crfpp_Z(crfpp_t*);
    00069   CRFPP_DLL_EXTERN bool     crfpp_parse(crfpp_t*);
    00070   CRFPP_DLL_EXTERN bool     crfpp_empty(crfpp_t*);
    00071   CRFPP_DLL_EXTERN bool     crfpp_clear(crfpp_t*);
    00072   CRFPP_DLL_EXTERN bool     crfpp_next(crfpp_t*);
    00073   CRFPP_DLL_EXTERN int      crfpp_test(int, char **);
    00074   CRFPP_DLL_EXTERN int      crfpp_learn(int, char **);
    00075   CRFPP_DLL_EXTERN const char*  crfpp_strerror(crfpp_t*);
    00076   CRFPP_DLL_EXTERN const char*  crfpp_yname(crfpp_t*, size_t);
    00077   CRFPP_DLL_EXTERN const char*  crfpp_y2(crfpp_t*, size_t);
    00078   CRFPP_DLL_EXTERN const char*  crfpp_x(crfpp_t*, size_t, size_t);
    00079   CRFPP_DLL_EXTERN const char** crfpp_x2(crfpp_t*, size_t);
    00080   CRFPP_DLL_EXTERN const char*  crfpp_parse_tostr(crfpp_t*, const char*);
    00081   CRFPP_DLL_EXTERN const char*  crfpp_parse_tostr2(crfpp_t*,
    00082                                                    const char*, size_t);
    00083   CRFPP_DLL_EXTERN const char*  crfpp_parse_tostr3(crfpp_t*, const char*,
    00084                                                    size_t, char *, size_t);
    00085   CRFPP_DLL_EXTERN const char*  crfpp_tostr(crfpp_t*);
    00086   CRFPP_DLL_EXTERN const char*  crfpp_tostr2(crfpp_t*, char *, size_t);
    00087
    00088   CRFPP_DLL_EXTERN void crfpp_set_vlevel(crfpp_t *, unsigned int);
    00089   CRFPP_DLL_EXTERN unsigned int crfpp_vlevel(crfpp_t *);
    00090   CRFPP_DLL_EXTERN void crfpp_set_cost_factor(crfpp_t *, float);
    00091   CRFPP_DLL_EXTERN float crfpp_cost_factor(crfpp_t *);
    00092   CRFPP_DLL_EXTERN void crfpp_set_nbest(crfpp_t *, size_t);
    00093 #endif
    00094
    00095 #ifdef __cplusplus
    00096 }
    00097 #endif
    00098
    00099 /* C++ interface */
    00100 #ifdef __cplusplus
    00101
    00102 namespace CRFPP {
    00103
    00104 class Tagger {
    00105  public:
    00106 #ifndef SWIG
    00107   // open model with parameters in argv[]
    00108   // e.g, argv[] = {"CRF++", "-m", "model", "-v3"};
    00109   virtual bool open(int argc,  char** argv) = 0;
    00110
    00111   // open model with parameter arg, e.g. arg = "-m model -v3";
    00112   virtual bool open(const char* arg) = 0;
    00113
    00114   // add str[] as tokens to the current context
    00115   virtual bool add(size_t size, const char **str) = 0;
    00116
    00117   // close the current model
    00118   virtual void close() = 0;
    00119
    00120   // return parameter vector. the size should be dsize();
    00121   virtual const float *weight_vector() const = 0;
    00122 #endif
    00123
    00124   // set vlevel
    00125   virtual void set_vlevel(unsigned int vlevel) = 0;
    00126
    00127   // get vlevel
    00128   virtual unsigned int vlevel() const = 0;
    00129
    00130   // set cost factor
    00131   virtual void set_cost_factor(float cost_factor) = 0;
    00132
    00133   // get cost factor
    00134   virtual float cost_factor() const = 0;
    00135
    00136   // set nbest
    00137   virtual void set_nbest(size_t nbest) = 0;
    00138
    00139   // get nbest
    00140   virtual size_t nbest() const = 0;
    00141
    00142   // add one line to the current context
    00143   virtual bool add(const char* str) = 0;
    00144
    00145   // return size of tokens(lines)
    00146   virtual size_t size() const = 0;
    00147
    00148   // return size of column
    00149   virtual size_t xsize() const = 0;
    00150
    00151   // return size of features
    00152   virtual size_t dsize() const = 0;
    00153
    00154   // return output tag-id of i-th token
    00155   virtual size_t result(size_t i) const = 0;
    00156
    00157   // return answer tag-id of i-th token if it is available
    00158   virtual size_t answer(size_t i) const = 0;
    00159
    00160   // alias of result(i)
    00161   virtual size_t y(size_t i) const = 0;
    00162
    00163   // return output tag of i-th token as string
    00164   virtual const char*   y2(size_t i) const = 0;
    00165
    00166   // return i-th tag-id as string
    00167   virtual const char*   yname(size_t i) const = 0;
    00168
    00169   // return token at [i,j] as string(i:token j:column)
    00170   virtual const char*   x(size_t i, size_t j) const = 0;
    00171
    00172 #ifndef SWIG
    00173   // return an array of strings at i-th tokens
    00174   virtual const char**  x(size_t) const = 0;
    00175 #endif
    00176
    00177   // return size of output tags
    00178   virtual size_t ysize() const = 0;
    00179
    00180   // return marginal probability of j-th tag id at i-th token
    00181   virtual double prob(size_t i, size_t j) const = 0;
    00182
    00183   // return marginal probability of output tag at i-th token
    00184   // same as prob(i, tagger->y(i));
    00185   virtual double prob(size_t i) const = 0;
    00186
    00187   // return conditional probability of enter output
    00188   virtual double prob() const = 0;
    00189
    00190   // return forward log-prob of the j-th tag at i-th token
    00191   virtual double alpha(size_t i, size_t j) const = 0;
    00192
    00193   // return backward log-prob of the j-th tag at i-th token
    00194   virtual double beta(size_t i, size_t j) const = 0;
    00195
    00196   // return emission cost of the j-th tag at i-th token
    00197   virtual double emission_cost(size_t i, size_t j) const = 0;
    00198
    00199   // return transition cost of [j-th tag at i-th token] to
    00200   // [k-th tag at(i+1)-th token]
    00201   virtual double next_transition_cost(size_t i,
    00202                                       size_t j, size_t k) const = 0;
    00203
    00204   // return transition cost of [j-th tag at i-th token] to
    00205   // [k-th tag at(i-1)-th token]
    00206   virtual double prev_transition_cost(size_t i,
    00207                                       size_t j, size_t k) const = 0;
    00208
    00209   //  return the best accumulative cost to the j-th tag at i-th token
    00210   // used in viterbi search
    00211   virtual double best_cost(size_t i, size_t j) const = 0;
    00212
    00213 #ifndef SWIG
    00214   // return emission feature vector of the j-th tag at i-th token
    00215   virtual const int* emission_vector(size_t i, size_t j) const = 0;
    00216
    00217   // return transition feature vector of [j-th tag at i-th token] to
    00218   // [k-th tag at(i+1)-th token]
    00219   virtual const int* next_transition_vector(size_t i,
    00220                                             size_t j, size_t k) const = 0;
    00221
    00222   // return transition feature vector of [j-th tag at i-th token] to
    00223   // [k-th tag at(i-1)-th token]
    00224   virtual const int* prev_transition_vector(size_t i,
    00225                                             size_t j, size_t k) const = 0;
    00226 #endif
    00227
    00228   // normalizing factor(log-prob)
    00229   virtual double Z() const = 0;
    00230
    00231   // do parse and change the internal status, if failed, returns false
    00232   virtual bool parse() = 0;
    00233
    00234   // return true if the context is empty
    00235   virtual bool empty() const = 0;
    00236
    00237   // clear all context
    00238   virtual bool clear() = 0;
    00239
    00240   // change the internal state to output next-optimal output.
    00241   // calling it n-th times, can get n-best results,
    00242   // Neeed to specify -nN option to use this function, where
    00243   // N>=2
    00244   virtual bool next() = 0;
    00245
    00246   // parse 'str' and return result as string
    00247   // 'str' must be written in CRF++'s input format
    00248   virtual const char* parse(const char* str) = 0;
    00249
    00250 #ifndef SWIG
    00251   // return parsed result as string
    00252   virtual const char* toString() = 0;
    00253
    00254   // return parsed result as string.
    00255   // Result is saved in the buffer 'result', 'size' is the
    00256   // size of the buffer. if failed, return NULL
    00257   virtual const char* toString(char* result , size_t size) = 0;
    00258
    00259   // parse 'str' and return parsed result.
    00260   // You don't need to delete return value, but the buffer
    00261   // is rewritten whenever you call parse method.
    00262   // if failed, return NULL
    00263   virtual const char* parse(const char *str, size_t size) = 0;
    00264
    00265   // parse 'str' and return parsed result.
    00266   // The result is stored in the buffer 'result'.
    00267   // 'size2' is the size of the buffer. if failed, return NULL
    00268   virtual const char* parse(const char *str, size_t size1,
    00269                             char *result, size_t size2) = 0;
    00270 #endif
    00271   // return internal error code as string
    00272   virtual const char* what() = 0;
    00273
    00274   virtual ~Tagger() {}
    00275 };
    00276
    00277 /* factory method */
    00278
    00279 // create CRFPP::Tagger instance with parameters in argv[]
    00280 // e.g, argv[] = {"CRF++", "-m", "model", "-v3"};
    00281
    00282 CRFPP_DLL_EXTERN Tagger *createTagger(int argc, char **argv);
    00283
    00284 // create CRFPP::Tagger instance with parameter in arg
    00285 // e.g. arg = "-m model -v3";
    00286 CRFPP_DLL_EXTERN Tagger *createTagger(const char *arg);
    00287
    00288 // return error code of createTagger();
    00289 CRFPP_DLL_EXTERN const char* getTaggerError();
    00290 }
    00291
    00292 #endif
    00293 #endif