Class: Libexttextcat::Classifier
- Inherits:
-
Object
- Object
- Libexttextcat::Classifier
- Defined in:
- lib/libexttextcat.rb,
ext/libexttextcat/libexttextcat.c
Overview
Note that there must be libexttextcat resource files specified in the config file must be in the current directory when this method is invoked.
Instance Method Summary collapse
-
#classify ⇒ Object
:call-seq: classify (source) -> String.
-
#classify_full ⇒ Object
:call-seq: classify_full (source) -> Libexttextcat::Candidates.
-
#initialize ⇒ Object
constructor
:call-seq: new (conf_file) -> Libexttextcat::Classifier.
Constructor Details
#initialize ⇒ Object
:call-seq:
new (conf_file) -> Libexttextcat::Classifier
conf_file is a file path to the configuration file
(e.g. /usr/local/share/libexttextcat/fpdb.conf.dist on
OpenBSD).
51 52 53 54 55 56 57 58 59 60 |
# File 'ext/libexttextcat/libexttextcat.c', line 51
static VALUE
rbe_classifier_m_initialize (VALUE self, VALUE conffile)
{
const char *f = rb_string_value_cstr (&conffile);
void *h = textcat_Init (f);
if (!h)
rb_raise (rbe_eError, "initialization failed");
RTYPEDDATA_DATA (self) = h;
return self;
}
|
Instance Method Details
#classify ⇒ Object
:call-seq:
classify (source) -> String
source is a String. Please see also the class description. So
you might want to do something like this:
Dir.chdir("/usr/local/share/libexttextcat") do
classifier.classify(source)
end
This returns a string containing a list of category id's, each one between square brackets. (e.g. [ga--utf8][hu--utf8]). For more structured data, use #classify_full instead.
This can raise error with "unknown" or "short" message.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'ext/libexttextcat/libexttextcat.c', line 79
static VALUE
rbe_classifier_m_classify (VALUE self, VALUE buffer)
{
void *h = rb_check_typeddata (self, &rbe_classifier_type);
const char *b = rb_string_value_ptr (&buffer);
size_t s = RSTRING_LEN (buffer);
/* Returned category is managed by textcat_t on stack. */
const char *c = textcat_Classify (h, b, s);
if (strcmp (c, TEXTCAT_RESULT_UNKNOWN_STR) == 0)
rb_raise (rbe_eError, "unknown");
if (strcmp (c, TEXTCAT_RESULT_SHORT_STR) == 0)
rb_raise (rbe_eError, "short");
return rb_str_new_cstr (c);
}
|
#classify_full ⇒ Object
:call-seq:
classify_full (source) -> Libexttextcat::Candidates
source is a String. Please see also the class description. So
you might want to do something like this:
Dir.chdir("/usr/local/share/libexttextcat") do
classifier.classify_full(source)
end
You should make sure if the result is correct by invoking
classify with no errors. It is because otherwise the returned
candidates can be silently erroneous; it can be empty or with
garbage data.
138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'ext/libexttextcat/libexttextcat.c', line 138
static VALUE
rbe_classifier_m_classify_full (VALUE self, VALUE buffer)
{
const char *b = rb_string_value_ptr (&buffer);
size_t s = RSTRING_LEN (buffer);
void *h = RTYPEDDATA_DATA (self);
candidate_t *c = textcat_GetClassifyFullOutput (h);
int n = textcat_ClassifyFull (h, b, s, c);
RbeCandidates *cs = ruby_xmalloc (sizeof (RbeCandidates));
*cs = (RbeCandidates){ .handle = h, .candidates = c, .num = n };
VALUE v = rb_obj_alloc (rbe_cCandidates);
RTYPEDDATA_DATA (v) = cs;
return v;
}
|