Class: Groonga::Snippet
- Inherits:
-
Object
- Object
- Groonga::Snippet
- Defined in:
- ext/rb-grn-snippet.c,
ext/rb-grn-snippet.c
Overview
スニペット(検索語周辺のテキスト)を生成するためのオブジェクト。
Instance Method Summary collapse
-
#add_keyword(keyword, options = {}) ⇒ Object
keywordを追加する。optionsに指定可能な値は以下の通 り。.
-
#execute(string) ⇒ Object
stringを走査し、スニペットを作成する。.
-
#Groonga::Snippet.new(options = {}) ⇒ Object
constructor
スニペットを作成する。optionsに指定可能な値は以下の通 り。.
Constructor Details
#Groonga::Snippet.new(options = {}) ⇒ Object
スニペットを作成する。optionsに指定可能な値は以下の通 り。
:context
-
スキーマ作成時に使用するGroonga::Contextを指定する。 省略した場合はGroonga::Context.defaultを使用する。
:normalize
-
キーワード文字列・スニペット元の文字列を正規化するかど うか。省略した場合は
false
で正規化しない。 :skip_leading_spaces
-
先頭の空白を無視するかどうか。省略した場合は
false
で無 視しない。 :width
-
スニペット文字列の長さ。省略した場合は100文字。
:max_results
-
生成するスニペットの最大数。省略した場合は3。
:html_escape
-
スニペット内の+<+, >, &, “をHTMLエスケープするか どうか。省略した場合は
false
で、HTMLエスケープしない。 :default_open_tag
-
デフォルトの開始タグ。省略した場合は“”(空文字列)
:default_close_tag
-
デフォルトの終了タグ。省略した場合は“”(空文字列)
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'ext/rb-grn-snippet.c', line 136
static VALUE
rb_grn_snippet_initialize (int argc, VALUE *argv, VALUE self)
{
RbGrnSnippet *rb_grn_snippet;
grn_ctx *context = NULL;
grn_snip *snippet = NULL;
VALUE options;
VALUE rb_context, rb_normalize, rb_skip_leading_spaces;
VALUE rb_width, rb_max_results, rb_default_open_tag, rb_default_close_tag;
VALUE rb_html_escape;
int flags = GRN_SNIP_COPY_TAG;
unsigned int width = 100;
unsigned int max_results = 3;
char *default_open_tag = NULL;
unsigned int default_open_tag_length = 0;
char *default_close_tag = NULL;
unsigned int default_close_tag_length = 0;
grn_snip_mapping *mapping = NULL;
rb_scan_args(argc, argv, "01", &options);
rb_grn_scan_options(options,
"context", &rb_context,
"normalize", &rb_normalize,
"skip_leading_spaces", &rb_skip_leading_spaces,
"width", &rb_width,
"max_results", &rb_max_results,
"default_open_tag", &rb_default_open_tag,
"default_close_tag", &rb_default_close_tag,
"html_escape", &rb_html_escape,
NULL);
context = rb_grn_context_ensure(&rb_context);
if (RVAL2CBOOL(rb_normalize))
flags |= GRN_SNIP_NORMALIZE;
if (RVAL2CBOOL(rb_skip_leading_spaces))
flags |= GRN_SNIP_SKIP_LEADING_SPACES;
if (!NIL_P(rb_width))
width = NUM2UINT(rb_width);
if (!NIL_P(rb_max_results))
max_results = NUM2UINT(rb_max_results);
if (!NIL_P(rb_default_open_tag)) {
default_open_tag = StringValuePtr(rb_default_open_tag);
default_open_tag_length = RSTRING_LEN(rb_default_open_tag);
}
if (!NIL_P(rb_default_close_tag)) {
default_close_tag = StringValuePtr(rb_default_close_tag);
default_close_tag_length = RSTRING_LEN(rb_default_close_tag);
}
if (RVAL2CBOOL(rb_html_escape))
mapping = (grn_snip_mapping *)-1;
snippet = grn_snip_open(context, flags, width, max_results,
default_open_tag, default_open_tag_length,
default_close_tag, default_close_tag_length,
mapping);
rb_grn_context_check(context, rb_ary_new4(argc, argv));
rb_grn_snippet = ALLOC(RbGrnSnippet);
DATA_PTR(self) = rb_grn_snippet;
rb_grn_snippet->context = context;
rb_grn_snippet->snippet = snippet;
rb_grn_snippet->owner = RB_GRN_TRUE;
rb_iv_set(self, "@context", rb_context);
return Qnil;
}
|
Instance Method Details
#add_keyword(keyword, options = {}) ⇒ Object
keywordを追加する。optionsに指定可能な値は以下の通 り。
:open_tag
-
開始タグ。省略した場合はGroonga::Snippet.newで指定し た
:default_open_tag
。 :close_tag
-
終了タグ。省略した場合はGroonga::Snippet.newで指定し た
:default_close_tag
。
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'ext/rb-grn-snippet.c', line 226
static VALUE
rb_grn_snippet_add_keyword (int argc, VALUE *argv, VALUE self)
{
RbGrnSnippet *rb_grn_snippet;
grn_rc rc;
VALUE rb_keyword, options;
VALUE rb_open_tag, rb_close_tag;
char *keyword, *open_tag = NULL, *close_tag = NULL;
unsigned int keyword_length, open_tag_length = 0, close_tag_length = 0;
rb_scan_args(argc, argv, "11", &rb_keyword, &options);
rb_grn_snippet = SELF(self);
keyword = StringValuePtr(rb_keyword);
keyword_length = RSTRING_LEN(rb_keyword);
rb_grn_scan_options(options,
"open_tag", &rb_open_tag,
"close_tag", &rb_close_tag,
NULL);
if (!NIL_P(rb_open_tag)) {
open_tag = StringValuePtr(rb_open_tag);
open_tag_length = RSTRING_LEN(rb_open_tag);
}
if (!NIL_P(rb_close_tag)) {
close_tag = StringValuePtr(rb_close_tag);
close_tag_length = RSTRING_LEN(rb_close_tag);
}
rc = grn_snip_add_cond(rb_grn_snippet->context,
rb_grn_snippet->snippet,
keyword, keyword_length,
open_tag, open_tag_length,
close_tag, close_tag_length);
rb_grn_rc_check(rc, self);
return Qnil;
}
|
#execute(string) ⇒ Object
stringを走査し、スニペットを作成する。
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 |
# File 'ext/rb-grn-snippet.c', line 274
static VALUE
rb_grn_snippet_execute (VALUE self, VALUE rb_string)
{
RbGrnSnippet *rb_grn_snippet;
grn_rc rc;
grn_ctx *context;
grn_snip *snippet;
char *string;
unsigned int string_length;
unsigned int i, n_results, max_tagged_length;
VALUE rb_results;
char *result;
rb_grn_snippet = SELF(self);
context = rb_grn_snippet->context;
snippet = rb_grn_snippet->snippet;
#ifdef HAVE_RUBY_ENCODING_H
rb_string = rb_grn_context_rb_string_encode(context, rb_string);
#endif
string = StringValuePtr(rb_string);
string_length = RSTRING_LEN(rb_string);
rc = grn_snip_exec(context, snippet, string, string_length,
&n_results, &max_tagged_length);
rb_grn_rc_check(rc, self);
rb_results = rb_ary_new2(n_results);
result = ALLOCA_N(char, max_tagged_length);
for (i = 0; i < n_results; i++) {
VALUE rb_result;
unsigned result_length;
rc = grn_snip_get_result(context, snippet, i, result, &result_length);
rb_grn_rc_check(rc, self);
rb_result = rb_grn_context_rb_string_new(context, result, result_length);
rb_ary_push(rb_results, rb_result);
}
return rb_results;
}
|