class Libexttextcat::Classifier
Note that there must be libexttextcat resource files specified in the config file must be in the current directory when this method is invoked.
Public Class Methods
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;
}
conf_file is a file path to the configuration file (e.g. /usr/local/share/libexttextcat/fpdb.conf.dist on OpenBSD).
Public Instance Methods
Source
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);
}
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.
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;
}
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.