Class: OTS::Article

Inherits:
Object
  • Object
show all
Defined in:
ext/ots.c

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'ext/ots.c', line 32

VALUE article_initialize(int argc, VALUE *argv, VALUE self) {
    VALUE text, options, language, dictionary = Qnil;
    OtsArticle *article = article_handle(self);

    rb_scan_args(argc, argv, "11", &text, &options);

    language = rb_str_new2("en");

    if (TYPE(text) != T_STRING)
        rb_raise(rb_eArgError, "invalid +text+");

    if (!NIL_P(options)) {
        if (TYPE(options) != T_HASH)
            rb_raise(rb_eArgError, "invalid +options+ hash");

        dictionary = rb_hash_aref(options, ID2SYM(rb_intern("dictionary")));
        language   = rb_hash_aref(options, ID2SYM(rb_intern("language")));
    }

    if (!NIL_P(dictionary))
        article_load_dictionary(article, CSTRING(dictionary));
    else
        article_load_dictionary(article, CSTRING(language));

    ots_parse_stream(RSTRING_PTR(text), RSTRING_LEN(text), article);
    ots_grade_doc(article);

    rb_iv_set(self, "@encoding", (VALUE)rb_enc_get(text));

    return self;
}

Instance Method Details

#keywordsObject

how many times have we seen this word in the text?



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'ext/ots.c', line 130

VALUE article_keywords(VALUE self) {
    OtsArticle *article = article_handle(self);
    rb_encoding *encoding = (rb_encoding*)rb_iv_get(self, "@encoding");

    VALUE words     = rb_ary_new();
    GList* word_ptr = article->ImpWords;

    while (word_ptr) {
        OtsWordEntry *data = (OtsWordEntry *)word_ptr->data;
        if (data && strlen(data->word) > 0)
            rb_ary_push(words, rb_enc_str_new2(data->word, encoding));
        word_ptr = word_ptr->next;
    }

    return words;
}

#summarize(options) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'ext/ots.c', line 93

VALUE article_summarize(VALUE self, VALUE options) {
    VALUE lines, percent;
    OtsArticle *article = article_handle(self);

    if (TYPE(options) != T_HASH)
        rb_raise(rb_eArgError, "expect an options hash");

    lines   = rb_hash_aref(options, ID2SYM(rb_intern("sentences")));
    percent = rb_hash_aref(options, ID2SYM(rb_intern("percent")));

    if (NIL_P(lines) && NIL_P(percent))
        rb_raise(rb_eArgError, "expect +sentences+ or +percent+");

    if (lines != Qnil)
        ots_highlight_doc_lines(article, NUM2INT(lines));
    else
        ots_highlight_doc(article, NUM2INT(percent));

    return article_summary(article, (rb_encoding *)rb_iv_get(self, "@encoding"));
}

#topicsObject



114
115
116
117
118
119
120
121
# File 'ext/ots.c', line 114

VALUE article_topics(VALUE self) {
    OtsArticle *article = article_handle(self);

    return
        article->title ?
            rb_str_split(rb_enc_str_new2(article->title, (rb_encoding*)rb_iv_get(self, "@encoding")), ",") :
            Qnil;
}