Class: ELDC::Detector

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/eldc.rb,
ext/eldc/rb_eldc.c

Instance Method Summary collapse

Constructor Details

#initializeObject

:nodoc:



51
52
53
54
55
56
# File 'ext/eldc/rb_eldc.c', line 51

static VALUE
rb_eldc_detector_m_initialize (VALUE self)
{
  eldc_init ();
  return RUBY_Qnil;
}

Instance Method Details

#detectObject

:call-seq:

detect ("TEXT") -> "LANG"


68
69
70
71
72
73
74
# File 'ext/eldc/rb_eldc.c', line 68

static VALUE
rb_eldc_detector_m_detect (VALUE self, VALUE text)
{
  const char *text_cstr = rb_string_value_cstr (&text);
  const char *language = eldc_detect (text_cstr);
  return rb_eldc_make_language_value (language);
}

#detect_detailsObject

:call-seq:

detect_details ("TEXT") -> ELDC::DetectResult


81
82
83
84
85
86
87
88
89
# File 'ext/eldc/rb_eldc.c', line 81

static VALUE
rb_eldc_detector_m_detect_details (VALUE self, VALUE text)
{
  const char *text_cstr = rb_string_value_cstr (&text);
  EldcDetectResult *result = malloc (sizeof (EldcDetectResult));
  eldc_detect_details (text_cstr, result);
  return rb_data_typed_object_wrap (rb_cDetectResult, result,
                                    &rb_eldc_detect_result_type);
}

#scheme=Object

:call-seq:

detector.scheme = "iso639-2t"
detector.scheme = "iso639-1"


148
149
150
151
152
153
154
# File 'ext/eldc/rb_eldc.c', line 148

static VALUE
rb_eldc_detector_m_scheme_set (VALUE self, VALUE scheme)
{
  (void)self;
  eldc_set_scheme (rb_string_value_cstr (&scheme));
  return RUBY_Qnil;
}

#scores=Object

:call-seq:

detector.scores = SCORES

Controls how many top scores will be returned. SCORES is clamped between 1 and ELDC::MAX_SCORES. Default is 3.



163
164
165
166
167
168
# File 'ext/eldc/rb_eldc.c', line 163

static VALUE
rb_eldc_detector_m_scores_set (VALUE self, VALUE scores)
{
  eldc_set_scores (rb_num2int_inline (scores));
  return RUBY_Qnil;
}

#set_languagesObject Also known as: languages=

:call-seq:

set_languages (nil)
set_languages ([])
set_languages ("")
set_languages ("LANG1,LANG2,...") -> ["LANG1", "LANG2", ...]
set_languages (["LANG1", "LANG2", ...]) -> [...]

Empty-ish value resets languages. If one or more language codes are given, it returns an array of actually applied language code String. The codes which are not applied is printed to the standard error.

Please note that if you write…

codes = (detector.languages = "LANG1,LANG2")

… then the codes is always "LANG1,LANG2". For checking the returned value, use #set_languages instead.



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'ext/eldc/rb_eldc.c', line 111

static VALUE
rb_eldc_detector_m_languages_set (VALUE self, VALUE languages)
{
  (void)self;
  VALUE codes_value;
  switch (rb_type (languages))
    {
    case RUBY_T_NIL:
      codes_value = RUBY_Qnil;
      break;
    case RUBY_T_ARRAY:
      {
        const VALUE *ptr = rb_array_const_ptr (languages);
        const long len = rb_array_len (languages);
        codes_value = rb_str_new (NULL, 0);
        for (long index = 0; index < len; index++)
          {
            if (index)
              rb_str_append (codes_value, rb_str_new_lit (","));
            VALUE language = ptr[index];
            rb_str_append (codes_value, language);
          }
      }
      break;
    default: /* including string */
      codes_value = rb_obj_as_string (languages);
    }
  const char *actual = eldc_set_languages (
      RB_NIL_P (codes_value) ? NULL : rb_string_value_cstr (&codes_value));
  return rb_str_split (rb_str_new_cstr (actual), ",");
}