opener-opinion-detector-basic 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/README.md +30 -0
- data/bin/opinion-detector-basic +19 -0
- data/bin/opinion-detector-basic-server +10 -0
- data/config.ru +4 -0
- data/core/opinion_detector_basic_multi.py +499 -0
- data/core/packages/KafNafParser-1.3.tar.gz +0 -0
- data/core/packages/VUA_pylib-1.4.tar.gz +0 -0
- data/core/site-packages/pre_build/VUKafParserPy-1.0-py2.7.egg-info/PKG-INFO +10 -0
- data/core/site-packages/pre_build/VUKafParserPy-1.0-py2.7.egg-info/SOURCES.txt +7 -0
- data/core/site-packages/pre_build/VUKafParserPy-1.0-py2.7.egg-info/dependency_links.txt +1 -0
- data/core/site-packages/pre_build/VUKafParserPy-1.0-py2.7.egg-info/installed-files.txt +11 -0
- data/core/site-packages/pre_build/VUKafParserPy-1.0-py2.7.egg-info/top_level.txt +1 -0
- data/core/site-packages/pre_build/VUKafParserPy/KafDataObjectsMod.py +165 -0
- data/core/site-packages/pre_build/VUKafParserPy/KafDataObjectsMod.pyc +0 -0
- data/core/site-packages/pre_build/VUKafParserPy/KafParserMod.py +439 -0
- data/core/site-packages/pre_build/VUKafParserPy/KafParserMod.pyc +0 -0
- data/core/site-packages/pre_build/VUKafParserPy/__init__.py +7 -0
- data/core/site-packages/pre_build/VUKafParserPy/__init__.pyc +0 -0
- data/core/vendor/src/crfsuite/AUTHORS +1 -0
- data/core/vendor/src/crfsuite/COPYING +27 -0
- data/core/vendor/src/crfsuite/ChangeLog +103 -0
- data/core/vendor/src/crfsuite/INSTALL +236 -0
- data/core/vendor/src/crfsuite/Makefile.am +19 -0
- data/core/vendor/src/crfsuite/Makefile.in +783 -0
- data/core/vendor/src/crfsuite/README +183 -0
- data/core/vendor/src/crfsuite/aclocal.m4 +9018 -0
- data/core/vendor/src/crfsuite/autogen.sh +38 -0
- data/core/vendor/src/crfsuite/compile +143 -0
- data/core/vendor/src/crfsuite/config.guess +1502 -0
- data/core/vendor/src/crfsuite/config.h.in +198 -0
- data/core/vendor/src/crfsuite/config.sub +1714 -0
- data/core/vendor/src/crfsuite/configure +14273 -0
- data/core/vendor/src/crfsuite/configure.in +149 -0
- data/core/vendor/src/crfsuite/crfsuite.sln +42 -0
- data/core/vendor/src/crfsuite/depcomp +630 -0
- data/core/vendor/src/crfsuite/example/chunking.py +49 -0
- data/core/vendor/src/crfsuite/example/crfutils.py +179 -0
- data/core/vendor/src/crfsuite/example/ner.py +270 -0
- data/core/vendor/src/crfsuite/example/pos.py +78 -0
- data/core/vendor/src/crfsuite/example/template.py +88 -0
- data/core/vendor/src/crfsuite/frontend/Makefile.am +29 -0
- data/core/vendor/src/crfsuite/frontend/Makefile.in +640 -0
- data/core/vendor/src/crfsuite/frontend/dump.c +116 -0
- data/core/vendor/src/crfsuite/frontend/frontend.vcxproj +129 -0
- data/core/vendor/src/crfsuite/frontend/iwa.c +273 -0
- data/core/vendor/src/crfsuite/frontend/iwa.h +65 -0
- data/core/vendor/src/crfsuite/frontend/learn.c +439 -0
- data/core/vendor/src/crfsuite/frontend/main.c +137 -0
- data/core/vendor/src/crfsuite/frontend/option.c +93 -0
- data/core/vendor/src/crfsuite/frontend/option.h +86 -0
- data/core/vendor/src/crfsuite/frontend/readdata.h +38 -0
- data/core/vendor/src/crfsuite/frontend/reader.c +136 -0
- data/core/vendor/src/crfsuite/frontend/tag.c +427 -0
- data/core/vendor/src/crfsuite/genbinary.sh.in +15 -0
- data/core/vendor/src/crfsuite/include/Makefile.am +11 -0
- data/core/vendor/src/crfsuite/include/Makefile.in +461 -0
- data/core/vendor/src/crfsuite/include/crfsuite.h +1063 -0
- data/core/vendor/src/crfsuite/include/crfsuite.hpp +555 -0
- data/core/vendor/src/crfsuite/include/crfsuite_api.hpp +400 -0
- data/core/vendor/src/crfsuite/include/os.h +61 -0
- data/core/vendor/src/crfsuite/install-sh +520 -0
- data/core/vendor/src/crfsuite/lib/cqdb/COPYING +28 -0
- data/core/vendor/src/crfsuite/lib/cqdb/Makefile.am +21 -0
- data/core/vendor/src/crfsuite/lib/cqdb/Makefile.in +549 -0
- data/core/vendor/src/crfsuite/lib/cqdb/cqdb.vcxproj +86 -0
- data/core/vendor/src/crfsuite/lib/cqdb/include/cqdb.h +524 -0
- data/core/vendor/src/crfsuite/lib/cqdb/src/cqdb.c +587 -0
- data/core/vendor/src/crfsuite/lib/cqdb/src/lookup3.c +976 -0
- data/core/vendor/src/crfsuite/lib/crf/Makefile.am +46 -0
- data/core/vendor/src/crfsuite/lib/crf/Makefile.in +721 -0
- data/core/vendor/src/crfsuite/lib/crf/crf.vcxproj +216 -0
- data/core/vendor/src/crfsuite/lib/crf/src/crf1d.h +353 -0
- data/core/vendor/src/crfsuite/lib/crf/src/crf1d_context.c +705 -0
- data/core/vendor/src/crfsuite/lib/crf/src/crf1d_encode.c +943 -0
- data/core/vendor/src/crfsuite/lib/crf/src/crf1d_feature.c +352 -0
- data/core/vendor/src/crfsuite/lib/crf/src/crf1d_model.c +994 -0
- data/core/vendor/src/crfsuite/lib/crf/src/crf1d_tag.c +550 -0
- data/core/vendor/src/crfsuite/lib/crf/src/crfsuite.c +492 -0
- data/core/vendor/src/crfsuite/lib/crf/src/crfsuite_internal.h +236 -0
- data/core/vendor/src/crfsuite/lib/crf/src/crfsuite_train.c +272 -0
- data/core/vendor/src/crfsuite/lib/crf/src/dataset.c +106 -0
- data/core/vendor/src/crfsuite/lib/crf/src/dictionary.c +118 -0
- data/core/vendor/src/crfsuite/lib/crf/src/holdout.c +80 -0
- data/core/vendor/src/crfsuite/lib/crf/src/logging.c +91 -0
- data/core/vendor/src/crfsuite/lib/crf/src/logging.h +48 -0
- data/core/vendor/src/crfsuite/lib/crf/src/params.c +335 -0
- data/core/vendor/src/crfsuite/lib/crf/src/params.h +80 -0
- data/core/vendor/src/crfsuite/lib/crf/src/quark.c +172 -0
- data/core/vendor/src/crfsuite/lib/crf/src/quark.h +46 -0
- data/core/vendor/src/crfsuite/lib/crf/src/rumavl.c +1107 -0
- data/core/vendor/src/crfsuite/lib/crf/src/rumavl.h +160 -0
- data/core/vendor/src/crfsuite/lib/crf/src/train_arow.c +408 -0
- data/core/vendor/src/crfsuite/lib/crf/src/train_averaged_perceptron.c +242 -0
- data/core/vendor/src/crfsuite/lib/crf/src/train_l2sgd.c +507 -0
- data/core/vendor/src/crfsuite/lib/crf/src/train_lbfgs.c +338 -0
- data/core/vendor/src/crfsuite/lib/crf/src/train_passive_aggressive.c +435 -0
- data/core/vendor/src/crfsuite/lib/crf/src/vecmath.h +341 -0
- data/core/vendor/src/crfsuite/ltmain.sh +8413 -0
- data/core/vendor/src/crfsuite/missing +376 -0
- data/core/vendor/src/crfsuite/swig/Makefile.am +13 -0
- data/core/vendor/src/crfsuite/swig/Makefile.in +365 -0
- data/core/vendor/src/crfsuite/swig/crfsuite.cpp +2 -0
- data/core/vendor/src/crfsuite/swig/export.i +32 -0
- data/core/vendor/src/crfsuite/swig/python/README +92 -0
- data/core/vendor/src/crfsuite/swig/python/crfsuite.py +329 -0
- data/core/vendor/src/crfsuite/swig/python/export_wrap.cpp +14355 -0
- data/core/vendor/src/crfsuite/swig/python/export_wrap.h +63 -0
- data/core/vendor/src/crfsuite/swig/python/prepare.sh +9 -0
- data/core/vendor/src/crfsuite/swig/python/sample_tag.py +52 -0
- data/core/vendor/src/crfsuite/swig/python/sample_train.py +68 -0
- data/core/vendor/src/crfsuite/swig/python/setup.py +44 -0
- data/core/vendor/src/crfsuite/win32/stdint.h +679 -0
- data/core/vendor/src/liblbfgs/AUTHORS +1 -0
- data/core/vendor/src/liblbfgs/COPYING +22 -0
- data/core/vendor/src/liblbfgs/ChangeLog +120 -0
- data/core/vendor/src/liblbfgs/INSTALL +231 -0
- data/core/vendor/src/liblbfgs/Makefile.am +10 -0
- data/core/vendor/src/liblbfgs/Makefile.in +638 -0
- data/core/vendor/src/liblbfgs/NEWS +0 -0
- data/core/vendor/src/liblbfgs/README +71 -0
- data/core/vendor/src/liblbfgs/aclocal.m4 +6985 -0
- data/core/vendor/src/liblbfgs/autogen.sh +38 -0
- data/core/vendor/src/liblbfgs/config.guess +1411 -0
- data/core/vendor/src/liblbfgs/config.h.in +64 -0
- data/core/vendor/src/liblbfgs/config.sub +1500 -0
- data/core/vendor/src/liblbfgs/configure +21146 -0
- data/core/vendor/src/liblbfgs/configure.in +107 -0
- data/core/vendor/src/liblbfgs/depcomp +522 -0
- data/core/vendor/src/liblbfgs/include/lbfgs.h +745 -0
- data/core/vendor/src/liblbfgs/install-sh +322 -0
- data/core/vendor/src/liblbfgs/lbfgs.sln +26 -0
- data/core/vendor/src/liblbfgs/lib/Makefile.am +24 -0
- data/core/vendor/src/liblbfgs/lib/Makefile.in +499 -0
- data/core/vendor/src/liblbfgs/lib/arithmetic_ansi.h +133 -0
- data/core/vendor/src/liblbfgs/lib/arithmetic_sse_double.h +294 -0
- data/core/vendor/src/liblbfgs/lib/arithmetic_sse_float.h +298 -0
- data/core/vendor/src/liblbfgs/lib/lbfgs.c +1371 -0
- data/core/vendor/src/liblbfgs/lib/lib.vcxproj +95 -0
- data/core/vendor/src/liblbfgs/ltmain.sh +6426 -0
- data/core/vendor/src/liblbfgs/missing +353 -0
- data/core/vendor/src/liblbfgs/sample/Makefile.am +15 -0
- data/core/vendor/src/liblbfgs/sample/Makefile.in +433 -0
- data/core/vendor/src/liblbfgs/sample/sample.c +81 -0
- data/core/vendor/src/liblbfgs/sample/sample.cpp +126 -0
- data/core/vendor/src/liblbfgs/sample/sample.vcxproj +105 -0
- data/core/vendor/src/svm_light/LICENSE.txt +59 -0
- data/core/vendor/src/svm_light/Makefile +105 -0
- data/core/vendor/src/svm_light/kernel.h +40 -0
- data/core/vendor/src/svm_light/svm_classify.c +197 -0
- data/core/vendor/src/svm_light/svm_common.c +985 -0
- data/core/vendor/src/svm_light/svm_common.h +301 -0
- data/core/vendor/src/svm_light/svm_hideo.c +1062 -0
- data/core/vendor/src/svm_light/svm_learn.c +4147 -0
- data/core/vendor/src/svm_light/svm_learn.h +169 -0
- data/core/vendor/src/svm_light/svm_learn_main.c +397 -0
- data/core/vendor/src/svm_light/svm_loqo.c +211 -0
- data/ext/hack/Rakefile +17 -0
- data/ext/hack/support.rb +88 -0
- data/lib/opener/opinion_detector_basic.rb +91 -0
- data/lib/opener/opinion_detector_basic/public/markdown.css +284 -0
- data/lib/opener/opinion_detector_basic/server.rb +16 -0
- data/lib/opener/opinion_detector_basic/version.rb +5 -0
- data/lib/opener/opinion_detector_basic/views/index.erb +97 -0
- data/lib/opener/opinion_detector_basic/views/result.erb +15 -0
- data/opener-opinion-detector-basic.gemspec +36 -0
- data/pre_build_requirements.txt +1 -0
- metadata +309 -0
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* A parser for Item With Attributes (IWA) format.
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2007-2010, Naoaki Okazaki
|
|
5
|
+
* All rights reserved.
|
|
6
|
+
*
|
|
7
|
+
* Redistribution and use in source and binary forms, with or without
|
|
8
|
+
* modification, are permitted provided that the following conditions are met:
|
|
9
|
+
* * Redistributions of source code must retain the above copyright
|
|
10
|
+
* notice, this list of conditions and the following disclaimer.
|
|
11
|
+
* * Redistributions in binary form must reproduce the above copyright
|
|
12
|
+
* notice, this list of conditions and the following disclaimer in the
|
|
13
|
+
* documentation and/or other materials provided with the distribution.
|
|
14
|
+
* * Neither the names of the authors nor the names of its contributors
|
|
15
|
+
* may be used to endorse or promote products derived from this
|
|
16
|
+
* software without specific prior written permission.
|
|
17
|
+
*
|
|
18
|
+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
19
|
+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
20
|
+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
21
|
+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
|
|
22
|
+
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
23
|
+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
24
|
+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
25
|
+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
26
|
+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
27
|
+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
28
|
+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
/* $Id$ */
|
|
32
|
+
|
|
33
|
+
#ifndef __IWA_H__
|
|
34
|
+
#define __IWA_H__
|
|
35
|
+
|
|
36
|
+
#ifdef __cplusplus
|
|
37
|
+
extern "C" {
|
|
38
|
+
#endif/*__cplusplus*/
|
|
39
|
+
|
|
40
|
+
typedef struct tag_iwa iwa_t;
|
|
41
|
+
|
|
42
|
+
enum {
|
|
43
|
+
IWA_NONE,
|
|
44
|
+
IWA_EOF,
|
|
45
|
+
IWA_BOI,
|
|
46
|
+
IWA_EOI,
|
|
47
|
+
IWA_ITEM,
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
struct tag_iwa_token {
|
|
51
|
+
int type;
|
|
52
|
+
const char *attr;
|
|
53
|
+
const char *value;
|
|
54
|
+
};
|
|
55
|
+
typedef struct tag_iwa_token iwa_token_t;
|
|
56
|
+
|
|
57
|
+
iwa_t* iwa_reader(FILE *fp);
|
|
58
|
+
const iwa_token_t* iwa_read(iwa_t* iwa);
|
|
59
|
+
void iwa_delete(iwa_t* iwa);
|
|
60
|
+
|
|
61
|
+
#ifdef __cplusplus
|
|
62
|
+
}
|
|
63
|
+
#endif/*__cplusplus*/
|
|
64
|
+
|
|
65
|
+
#endif/*__IWA_H__*/
|
|
@@ -0,0 +1,439 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* Learn command for CRFsuite frontend.
|
|
3
|
+
*
|
|
4
|
+
* Copyright (c) 2007-2010, Naoaki Okazaki
|
|
5
|
+
* All rights reserved.
|
|
6
|
+
*
|
|
7
|
+
* Redistribution and use in source and binary forms, with or without
|
|
8
|
+
* modification, are permitted provided that the following conditions are met:
|
|
9
|
+
* * Redistributions of source code must retain the above copyright
|
|
10
|
+
* notice, this list of conditions and the following disclaimer.
|
|
11
|
+
* * Redistributions in binary form must reproduce the above copyright
|
|
12
|
+
* notice, this list of conditions and the following disclaimer in the
|
|
13
|
+
* documentation and/or other materials provided with the distribution.
|
|
14
|
+
* * Neither the names of the authors nor the names of its contributors
|
|
15
|
+
* may be used to endorse or promote products derived from this
|
|
16
|
+
* software without specific prior written permission.
|
|
17
|
+
*
|
|
18
|
+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
19
|
+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
20
|
+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
21
|
+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
|
|
22
|
+
* OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
23
|
+
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
24
|
+
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
25
|
+
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
26
|
+
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
27
|
+
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
28
|
+
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
29
|
+
*/
|
|
30
|
+
|
|
31
|
+
/* $Id$ */
|
|
32
|
+
|
|
33
|
+
#include <os.h>
|
|
34
|
+
|
|
35
|
+
#include <stdio.h>
|
|
36
|
+
#include <stdlib.h>
|
|
37
|
+
#include <string.h>
|
|
38
|
+
#include <time.h>
|
|
39
|
+
|
|
40
|
+
#include <crfsuite.h>
|
|
41
|
+
#include "option.h"
|
|
42
|
+
#include "readdata.h"
|
|
43
|
+
|
|
44
|
+
#define SAFE_RELEASE(obj) if ((obj) != NULL) { (obj)->release(obj); (obj) = NULL; }
|
|
45
|
+
#define MAX(a, b) ((a) < (b) ? (b) : (a))
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
typedef struct {
|
|
49
|
+
char *type;
|
|
50
|
+
char *algorithm;
|
|
51
|
+
char *model;
|
|
52
|
+
char *logbase;
|
|
53
|
+
|
|
54
|
+
int split;
|
|
55
|
+
int cross_validation;
|
|
56
|
+
int holdout;
|
|
57
|
+
int logfile;
|
|
58
|
+
|
|
59
|
+
int help;
|
|
60
|
+
int help_params;
|
|
61
|
+
|
|
62
|
+
int num_params;
|
|
63
|
+
char **params;
|
|
64
|
+
} learn_option_t;
|
|
65
|
+
|
|
66
|
+
static char* mystrdup(const char *src)
|
|
67
|
+
{
|
|
68
|
+
char *dst = (char*)malloc(strlen(src)+1);
|
|
69
|
+
if (dst != NULL) {
|
|
70
|
+
strcpy(dst, src);
|
|
71
|
+
}
|
|
72
|
+
return dst;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
static char* mystrcat(char *dst, const char *src)
|
|
76
|
+
{
|
|
77
|
+
int n = (dst != 0 ? strlen(dst) : 0);
|
|
78
|
+
dst = (char*)realloc(dst, n + strlen(src) + 1);
|
|
79
|
+
strcat(dst, src);
|
|
80
|
+
return dst;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
static void learn_option_init(learn_option_t* opt)
|
|
84
|
+
{
|
|
85
|
+
memset(opt, 0, sizeof(*opt));
|
|
86
|
+
opt->num_params = 0;
|
|
87
|
+
opt->holdout = -1;
|
|
88
|
+
opt->type = mystrdup("crf1d");
|
|
89
|
+
opt->algorithm = mystrdup("lbfgs");
|
|
90
|
+
opt->model = mystrdup("");
|
|
91
|
+
opt->logbase = mystrdup("log.crfsuite");
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
static void learn_option_finish(learn_option_t* opt)
|
|
95
|
+
{
|
|
96
|
+
int i;
|
|
97
|
+
|
|
98
|
+
free(opt->model);
|
|
99
|
+
|
|
100
|
+
for (i = 0;i < opt->num_params;++i) {
|
|
101
|
+
free(opt->params[i]);
|
|
102
|
+
}
|
|
103
|
+
free(opt->params);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
BEGIN_OPTION_MAP(parse_learn_options, learn_option_t)
|
|
107
|
+
|
|
108
|
+
ON_OPTION_WITH_ARG(SHORTOPT('t') || LONGOPT("type"))
|
|
109
|
+
if (strcmp(arg, "1d") == 0) {
|
|
110
|
+
free(opt->type);
|
|
111
|
+
opt->type = mystrdup("crf1d");
|
|
112
|
+
} else {
|
|
113
|
+
fprintf(stderr, "ERROR: Unknown graphical model: %s\n", arg);
|
|
114
|
+
return 1;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
ON_OPTION_WITH_ARG(SHORTOPT('a') || LONGOPT("algorithm"))
|
|
118
|
+
if (strcmp(arg, "lbfgs") == 0) {
|
|
119
|
+
free(opt->algorithm);
|
|
120
|
+
opt->algorithm = mystrdup("lbfgs");
|
|
121
|
+
} else if (strcmp(arg, "l2sgd") == 0) {
|
|
122
|
+
free(opt->algorithm);
|
|
123
|
+
opt->algorithm = mystrdup("l2sgd");
|
|
124
|
+
} else if (strcmp(arg, "ap") == 0 || strcmp(arg, "averaged-perceptron") == 0) {
|
|
125
|
+
free(opt->algorithm);
|
|
126
|
+
opt->algorithm = mystrdup("averaged-perceptron");
|
|
127
|
+
} else if (strcmp(arg, "pa") == 0 || strcmp(arg, "passive-aggressive") == 0) {
|
|
128
|
+
free(opt->algorithm);
|
|
129
|
+
opt->algorithm = mystrdup("passive-aggressive");
|
|
130
|
+
} else if (strcmp(arg, "arow") == 0) {
|
|
131
|
+
free(opt->algorithm);
|
|
132
|
+
opt->algorithm = mystrdup("arow");
|
|
133
|
+
} else {
|
|
134
|
+
fprintf(stderr, "ERROR: Unknown algorithm: %s\n", arg);
|
|
135
|
+
return 1;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
ON_OPTION_WITH_ARG(SHORTOPT('p') || LONGOPT("set"))
|
|
139
|
+
opt->params = (char **)realloc(opt->params, sizeof(char*) * (opt->num_params + 1));
|
|
140
|
+
opt->params[opt->num_params] = mystrdup(arg);
|
|
141
|
+
++opt->num_params;
|
|
142
|
+
|
|
143
|
+
ON_OPTION_WITH_ARG(SHORTOPT('m') || LONGOPT("model"))
|
|
144
|
+
free(opt->model);
|
|
145
|
+
opt->model = mystrdup(arg);
|
|
146
|
+
|
|
147
|
+
ON_OPTION_WITH_ARG(SHORTOPT('g') || LONGOPT("split"))
|
|
148
|
+
opt->split = atoi(arg);
|
|
149
|
+
|
|
150
|
+
ON_OPTION_WITH_ARG(SHORTOPT('e') || LONGOPT("holdout"))
|
|
151
|
+
opt->holdout = atoi(arg)-1;
|
|
152
|
+
|
|
153
|
+
ON_OPTION(SHORTOPT('x') || LONGOPT("cross-validate"))
|
|
154
|
+
opt->cross_validation = 1;
|
|
155
|
+
|
|
156
|
+
ON_OPTION(SHORTOPT('l') || LONGOPT("log-to-file"))
|
|
157
|
+
opt->logfile = 1;
|
|
158
|
+
|
|
159
|
+
ON_OPTION_WITH_ARG(SHORTOPT('L') || LONGOPT("logbase"))
|
|
160
|
+
free(opt->logbase);
|
|
161
|
+
opt->logbase = mystrdup(arg);
|
|
162
|
+
|
|
163
|
+
ON_OPTION(SHORTOPT('h') || LONGOPT("help"))
|
|
164
|
+
opt->help = 1;
|
|
165
|
+
|
|
166
|
+
ON_OPTION(SHORTOPT('H') || LONGOPT("help-params"))
|
|
167
|
+
opt->help_params = 1;
|
|
168
|
+
|
|
169
|
+
END_OPTION_MAP()
|
|
170
|
+
|
|
171
|
+
static void show_usage(FILE *fp, const char *argv0, const char *command)
|
|
172
|
+
{
|
|
173
|
+
fprintf(fp, "USAGE: %s %s [OPTIONS] [DATA1] [DATA2] ...\n", argv0, command);
|
|
174
|
+
fprintf(fp, "Trains a model using training data set(s).\n");
|
|
175
|
+
fprintf(fp, "\n");
|
|
176
|
+
fprintf(fp, " DATA file(s) corresponding to data set(s) for training; if multiple N files\n");
|
|
177
|
+
fprintf(fp, " are specified, this utility assigns a group number (1...N) to the\n");
|
|
178
|
+
fprintf(fp, " instances in each file; if a file name is '-', the utility reads a\n");
|
|
179
|
+
fprintf(fp, " data set from STDIN\n");
|
|
180
|
+
fprintf(fp, "\n");
|
|
181
|
+
fprintf(fp, "OPTIONS:\n");
|
|
182
|
+
fprintf(fp, " -t, --type=TYPE specify a graphical model (DEFAULT='1d'):\n");
|
|
183
|
+
fprintf(fp, " (this option is reserved for the future use)\n");
|
|
184
|
+
fprintf(fp, " 1d 1st-order Markov CRF with state and transition\n");
|
|
185
|
+
fprintf(fp, " features; transition features are not conditioned\n");
|
|
186
|
+
fprintf(fp, " on observations\n");
|
|
187
|
+
fprintf(fp, " -a, --algorithm=NAME specify a training algorithm (DEFAULT='lbfgs')\n");
|
|
188
|
+
fprintf(fp, " lbfgs L-BFGS with L1/L2 regularization\n");
|
|
189
|
+
fprintf(fp, " l2sgd SGD with L2-regularization\n");
|
|
190
|
+
fprintf(fp, " ap Averaged Perceptron\n");
|
|
191
|
+
fprintf(fp, " pa Passive Aggressive\n");
|
|
192
|
+
fprintf(fp, " arow Adaptive Regularization of Weights (AROW)\n");
|
|
193
|
+
fprintf(fp, " -p, --set=NAME=VALUE set the algorithm-specific parameter NAME to VALUE;\n");
|
|
194
|
+
fprintf(fp, " use '-H' or '--help-parameters' with the algorithm name\n");
|
|
195
|
+
fprintf(fp, " specified by '-a' or '--algorithm' and the graphical\n");
|
|
196
|
+
fprintf(fp, " model specified by '-t' or '--type' to see the list of\n");
|
|
197
|
+
fprintf(fp, " algorithm-specific parameters\n");
|
|
198
|
+
fprintf(fp, " -m, --model=FILE store the model to FILE (DEFAULT=''); if the value is\n");
|
|
199
|
+
fprintf(fp, " empty, this utility does not store the model\n");
|
|
200
|
+
fprintf(fp, " -g, --split=N split the instances into N groups; this option is\n");
|
|
201
|
+
fprintf(fp, " useful for holdout evaluation and cross validation\n");
|
|
202
|
+
fprintf(fp, " -e, --holdout=M use the M-th data for holdout evaluation and the rest\n");
|
|
203
|
+
fprintf(fp, " for training\n");
|
|
204
|
+
fprintf(fp, " -x, --cross-validate repeat holdout evaluations for #i in {1, ..., N} groups\n");
|
|
205
|
+
fprintf(fp, " (N-fold cross validation)\n");
|
|
206
|
+
fprintf(fp, " -l, --log-to-file write the training log to a file instead of to STDOUT;\n");
|
|
207
|
+
fprintf(fp, " The filename is determined automatically by the training\n");
|
|
208
|
+
fprintf(fp, " algorithm, parameters, and source files\n");
|
|
209
|
+
fprintf(fp, " -L, --logbase=BASE set the base name for a log file (used with -l option)\n");
|
|
210
|
+
fprintf(fp, " -h, --help show the usage of this command and exit\n");
|
|
211
|
+
fprintf(fp, " -H, --help-parameters show the help message of algorithm-specific parameters;\n");
|
|
212
|
+
fprintf(fp, " specify an algorithm with '-a' or '--algorithm' option,\n");
|
|
213
|
+
fprintf(fp, " and specify a graphical model with '-t' or '--type' option\n");
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
|
|
218
|
+
static int message_callback(void *instance, const char *format, va_list args)
|
|
219
|
+
{
|
|
220
|
+
vfprintf(stdout, format, args);
|
|
221
|
+
fflush(stdout);
|
|
222
|
+
return 0;
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
int main_learn(int argc, char *argv[], const char *argv0)
|
|
226
|
+
{
|
|
227
|
+
int i, n, groups = 1, ret = 0, arg_used = 0;
|
|
228
|
+
time_t ts;
|
|
229
|
+
char timestamp[80];
|
|
230
|
+
char trainer_id[128];
|
|
231
|
+
clock_t clk_begin, clk_current;
|
|
232
|
+
learn_option_t opt;
|
|
233
|
+
const char *command = argv[0];
|
|
234
|
+
FILE *fpi = stdin, *fpo = stdout, *fpe = stderr;
|
|
235
|
+
crfsuite_data_t data;
|
|
236
|
+
crfsuite_trainer_t *trainer = NULL;
|
|
237
|
+
crfsuite_dictionary_t *attrs = NULL, *labels = NULL;
|
|
238
|
+
|
|
239
|
+
/* Initializations. */
|
|
240
|
+
learn_option_init(&opt);
|
|
241
|
+
crfsuite_data_init(&data);
|
|
242
|
+
|
|
243
|
+
/* Parse the command-line option. */
|
|
244
|
+
arg_used = option_parse(++argv, --argc, parse_learn_options, &opt);
|
|
245
|
+
if (arg_used < 0) {
|
|
246
|
+
ret = 1;
|
|
247
|
+
goto force_exit;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/* Show the help message for this command if specified. */
|
|
251
|
+
if (opt.help) {
|
|
252
|
+
show_usage(fpo, argv0, command);
|
|
253
|
+
goto force_exit;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/* Open a log file if necessary. */
|
|
257
|
+
if (opt.logfile) {
|
|
258
|
+
/* Generate a filename for the log file. */
|
|
259
|
+
char *fname = NULL;
|
|
260
|
+
fname = mystrcat(fname, opt.logbase);
|
|
261
|
+
fname = mystrcat(fname, "_");
|
|
262
|
+
fname = mystrcat(fname, opt.algorithm);
|
|
263
|
+
for (i = 0;i < opt.num_params;++i) {
|
|
264
|
+
fname = mystrcat(fname, "_");
|
|
265
|
+
fname = mystrcat(fname, opt.params[i]);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
fpo = fopen(fname, "w");
|
|
269
|
+
if (fpo == NULL) {
|
|
270
|
+
fprintf(fpe, "ERROR: Failed to open the log file.\n");
|
|
271
|
+
ret = 1;
|
|
272
|
+
goto force_exit;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
|
|
276
|
+
/* Create dictionaries for attributes and labels. */
|
|
277
|
+
ret = crfsuite_create_instance("dictionary", (void**)&data.attrs);
|
|
278
|
+
if (!ret) {
|
|
279
|
+
fprintf(fpe, "ERROR: Failed to create a dictionary instance.\n");
|
|
280
|
+
ret = 1;
|
|
281
|
+
goto force_exit;
|
|
282
|
+
}
|
|
283
|
+
ret = crfsuite_create_instance("dictionary", (void**)&data.labels);
|
|
284
|
+
if (!ret) {
|
|
285
|
+
fprintf(fpe, "ERROR: Failed to create a dictionary instance.\n");
|
|
286
|
+
ret = 1;
|
|
287
|
+
goto force_exit;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/* Create a trainer instance. */
|
|
291
|
+
sprintf(trainer_id, "train/%s/%s", opt.type, opt.algorithm);
|
|
292
|
+
ret = crfsuite_create_instance(trainer_id, (void**)&trainer);
|
|
293
|
+
if (!ret) {
|
|
294
|
+
fprintf(fpe, "ERROR: Failed to create a trainer instance.\n");
|
|
295
|
+
ret = 1;
|
|
296
|
+
goto force_exit;
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
/* Show the help message for the training algorithm if specified. */
|
|
300
|
+
if (opt.help_params) {
|
|
301
|
+
crfsuite_params_t* params = trainer->params(trainer);
|
|
302
|
+
|
|
303
|
+
fprintf(fpo, "PARAMETERS for %s (%s):\n", opt.algorithm, opt.type);
|
|
304
|
+
fprintf(fpo, "\n");
|
|
305
|
+
|
|
306
|
+
for (i = 0;i < params->num(params);++i) {
|
|
307
|
+
char *name = NULL;
|
|
308
|
+
char *type = NULL;
|
|
309
|
+
char *value = NULL;
|
|
310
|
+
char *help = NULL;
|
|
311
|
+
|
|
312
|
+
params->name(params, i, &name);
|
|
313
|
+
params->get(params, name, &value);
|
|
314
|
+
params->help(params, name, &type, &help);
|
|
315
|
+
|
|
316
|
+
fprintf(fpo, "%s %s = %s;\n", type, name, value);
|
|
317
|
+
fprintf(fpo, "%s\n", help);
|
|
318
|
+
fprintf(fpo, "\n");
|
|
319
|
+
|
|
320
|
+
params->free(params, help);
|
|
321
|
+
params->free(params, type);
|
|
322
|
+
params->free(params, value);
|
|
323
|
+
params->free(params, name);
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
params->release(params);
|
|
327
|
+
goto force_exit;
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
/* Set parameters. */
|
|
331
|
+
for (i = 0;i < opt.num_params;++i) {
|
|
332
|
+
char *value = NULL;
|
|
333
|
+
char *name = opt.params[i];
|
|
334
|
+
crfsuite_params_t* params = trainer->params(trainer);
|
|
335
|
+
|
|
336
|
+
/* Split the parameter argument by the first '=' character. */
|
|
337
|
+
value = strchr(name, '=');
|
|
338
|
+
if (value != NULL) {
|
|
339
|
+
*value++ = 0;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
if (params->set(params, name, value) != 0) {
|
|
343
|
+
fprintf(fpe, "ERROR: paraneter not found: %s\n", name);
|
|
344
|
+
goto force_exit;
|
|
345
|
+
}
|
|
346
|
+
params->release(params);
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
/* Log the start time. */
|
|
350
|
+
time(&ts);
|
|
351
|
+
strftime(timestamp, sizeof(timestamp), "%Y-%m-%dT%H:%M:%SZ", gmtime(&ts));
|
|
352
|
+
fprintf(fpo, "Start time of the training: %s\n", timestamp);
|
|
353
|
+
fprintf(fpo, "\n");
|
|
354
|
+
|
|
355
|
+
/* Read the training data. */
|
|
356
|
+
fprintf(fpo, "Reading the data set(s)\n");
|
|
357
|
+
for (i = arg_used;i < argc;++i) {
|
|
358
|
+
FILE *fp = (strcmp(argv[i], "-") == 0) ? fpi : fopen(argv[i], "r");
|
|
359
|
+
if (fp == NULL) {
|
|
360
|
+
fprintf(fpe, "ERROR: Failed to open the data set: %s\n", argv[i]);
|
|
361
|
+
ret = 1;
|
|
362
|
+
goto force_exit;
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
fprintf(fpo, "[%d] %s\n", i-arg_used+1, argv[i]);
|
|
366
|
+
clk_begin = clock();
|
|
367
|
+
n = read_data(fp, fpo, &data, i-arg_used);
|
|
368
|
+
clk_current = clock();
|
|
369
|
+
fprintf(fpo, "Number of instances: %d\n", n);
|
|
370
|
+
fprintf(fpo, "Seconds required: %.3f\n", (clk_current - clk_begin) / (double)CLOCKS_PER_SEC);
|
|
371
|
+
fclose(fp);
|
|
372
|
+
}
|
|
373
|
+
groups = argc-arg_used;
|
|
374
|
+
fprintf(fpo, "\n");
|
|
375
|
+
|
|
376
|
+
/* Split into data sets if necessary. */
|
|
377
|
+
if (0 < opt.split) {
|
|
378
|
+
/* Shuffle the instances. */
|
|
379
|
+
for (i = 0;i < data.num_instances;++i) {
|
|
380
|
+
int j = rand() % data.num_instances;
|
|
381
|
+
crfsuite_instance_swap(&data.instances[i], &data.instances[j]);
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
/* Assign group numbers. */
|
|
385
|
+
for (i = 0;i < data.num_instances;++i) {
|
|
386
|
+
data.instances[i].group = i % opt.split;
|
|
387
|
+
}
|
|
388
|
+
groups = opt.split;
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
/* Report the statistics of the training data. */
|
|
392
|
+
fprintf(fpo, "Statistics the data set(s)\n");
|
|
393
|
+
fprintf(fpo, "Number of data sets (groups): %d\n", groups);
|
|
394
|
+
fprintf(fpo, "Number of instances: %d\n", data.num_instances);
|
|
395
|
+
fprintf(fpo, "Number of items: %d\n", crfsuite_data_totalitems(&data));
|
|
396
|
+
fprintf(fpo, "Number of attributes: %d\n", data.attrs->num(data.attrs));
|
|
397
|
+
fprintf(fpo, "Number of labels: %d\n", data.labels->num(data.labels));
|
|
398
|
+
fprintf(fpo, "\n");
|
|
399
|
+
fflush(fpo);
|
|
400
|
+
|
|
401
|
+
/* Set callback procedures that receive messages and taggers. */
|
|
402
|
+
trainer->set_message_callback(trainer, NULL, message_callback);
|
|
403
|
+
|
|
404
|
+
/* Start training. */
|
|
405
|
+
if (opt.cross_validation) {
|
|
406
|
+
for (i = 0;i < groups;++i) {
|
|
407
|
+
fprintf(fpo, "===== Cross validation (%d/%d) =====\n", i+1, groups);
|
|
408
|
+
if (ret = trainer->train(trainer, &data, "", i)) {
|
|
409
|
+
goto force_exit;
|
|
410
|
+
}
|
|
411
|
+
fprintf(fpo, "\n");
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
} else {
|
|
415
|
+
if (ret = trainer->train(trainer, &data, opt.model, opt.holdout)) {
|
|
416
|
+
goto force_exit;
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
/* Log the end time. */
|
|
422
|
+
time(&ts);
|
|
423
|
+
strftime(timestamp, sizeof(timestamp), "%Y-%m-%dT%H:%M:%SZ", gmtime(&ts));
|
|
424
|
+
fprintf(fpo, "End time of the training: %s\n", timestamp);
|
|
425
|
+
fprintf(fpo, "\n");
|
|
426
|
+
|
|
427
|
+
force_exit:
|
|
428
|
+
SAFE_RELEASE(trainer);
|
|
429
|
+
SAFE_RELEASE(data.labels);
|
|
430
|
+
SAFE_RELEASE(data.attrs);
|
|
431
|
+
|
|
432
|
+
crfsuite_data_finish(&data);
|
|
433
|
+
learn_option_finish(&opt);
|
|
434
|
+
if (fpo != NULL) {
|
|
435
|
+
fclose(fpo);
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
return ret;
|
|
439
|
+
}
|