Class: OTS::Article

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

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ Object



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
63
64
65
66
67
# File 'ext/ots/ots.c', line 37

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_index(text));

    return self;
}

Instance Method Details

#keywordsObject

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



135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'ext/ots/ots.c', line 135

VALUE article_keywords(VALUE self) {
    OtsArticle *article = article_handle(self);
    rb_encoding *encoding = article_encoding(self);

    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



98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'ext/ots/ots.c', line 98

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, article_encoding(self));
}

#topicsObject



119
120
121
122
123
124
125
126
# File 'ext/ots/ots.c', line 119

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

    return
        article->title ?
            rb_str_split(rb_enc_str_new2(article->title, article_encoding(self)), ",") :
            Qnil;
}