Masafumi Yokoyama
null+****@clear*****
Sat Nov 1 16:27:52 JST 2014
Masafumi Yokoyama 2014-11-01 16:27:52 +0900 (Sat, 01 Nov 2014) New Revision: 03ff7abf8648b711a3ca318d34e624250b1cc8c6 https://github.com/ranguba/rroonga/commit/03ff7abf8648b711a3ca318d34e624250b1cc8c6 Message: Add Groonga::Expression#keywords GitHub: fix #30 Modified files: ext/groonga/rb-grn-expression.c test/test-expression.rb Modified: ext/groonga/rb-grn-expression.c (+45 -0) =================================================================== --- ext/groonga/rb-grn-expression.c 2014-10-31 16:02:01 +0900 (5613565) +++ ext/groonga/rb-grn-expression.c 2014-11-01 16:27:52 +0900 (21f94b4) @@ -1,6 +1,7 @@ /* -*- coding: utf-8; mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* Copyright (C) 2009-2014 Kouhei Sutou <kou �� clear-code.com> + Copyright (C) 2014 Masafumi Yokoyama <myokoym �� gmail.com> This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -717,6 +718,47 @@ rb_grn_expression_snippet (int argc, VALUE *argv, VALUE self) return GRNOBJECT2RVAL(Qnil, context, snippet, GRN_TRUE); } +/* + * Extracts keywords from _expression_. The keywords order isn't + * guaranteed. + * + * @example + * expression.parse("Ruby OR Groonga") + * expression.keywords #=> ["Groonga", "Ruby"] + * + * @overload keywords + * @return [::Array<String>] the extracted keywords + * + * @since 4.0.6 + */ +static VALUE +rb_grn_expression_get_keywords (VALUE self) +{ + grn_ctx *context = NULL; + grn_obj *expression; + grn_obj keywords; + VALUE rb_keywords = rb_ary_new(); + + rb_grn_expression_deconstruct(SELF(self), &expression, &context, + NULL, NULL, + NULL, NULL, NULL); + + GRN_PTR_INIT(&keywords, GRN_OBJ_VECTOR, GRN_ID_NIL); + grn_expr_get_keywords(context, expression, &keywords); + { + int i, n_keywords; + n_keywords = GRN_BULK_VSIZE(&keywords) / sizeof(grn_obj *); + for (i = 0; i < n_keywords; i++) { + grn_obj *keyword = GRN_PTR_VALUE_AT(&keywords, i); + rb_ary_push(rb_keywords, + GRNBULK2RVAL(context, keyword, NULL, self)); + } + } + GRN_OBJ_FIN(context, &keywords); + + return rb_keywords; +} + void rb_grn_init_expression (VALUE mGrn) { @@ -748,6 +790,9 @@ rb_grn_init_expression (VALUE mGrn) rb_define_method(rb_cGrnExpression, "snippet", rb_grn_expression_snippet, -1); + rb_define_method(rb_cGrnExpression, "keywords", + rb_grn_expression_get_keywords, 0); + rb_define_method(rb_cGrnExpression, "inspect", rb_grn_expression_inspect, 0); } Modified: test/test-expression.rb (+19 -0) =================================================================== --- test/test-expression.rb 2014-10-31 16:02:01 +0900 (14ae16e) +++ test/test-expression.rb 2014-11-01 16:27:52 +0900 (c8f91f7) @@ -1,6 +1,7 @@ # -*- coding: utf-8 -*- # # Copyright (C) 2009-2013 Kouhei Sutou <kou �� clear-code.com> +# Copyright (C) 2014 Masafumi Yokoyama <myokoym �� gmail.com> # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public @@ -132,6 +133,24 @@ class ExpressionTest < Test::Unit::TestCase snippet.close end + def test_keywords + users = Groonga::Array.create(:name => "Users") + users.define_column("name", "ShortText") + + expression = Groonga::Expression.new + variable = expression.define_variable(:domain => users) + expression.append_object(variable) + expression.parse("ラングバ OR Ruby OR groonga", :default_column => name) + expression.compile + + assert_equal([ + "Ruby", + "groonga", + "ラングバ", + ], + expression.keywords.sort) + end + class AppendObjectTest < self setup def setup_expression -------------- next part -------------- HTML����������������������������...Download