Class: Tagger::BrillTagger

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

Instance Method Summary collapse

Instance Method Details

#add_contextual_rule(rule) ⇒ Object



37
38
39
40
41
42
43
44
# File 'ext/rule_tagger/rbtagger.c', line 37

static VALUE
BrillTagger_add_contextual_rule( VALUE self, VALUE rule )
{
  TaggerContext *tc;
  Data_Get_Struct( self, TaggerContext, tc );
  tagger_context_add_contextual_rule( tc, RSTRING_PTR(rule) );
  return Qnil;
}

#add_goodleft(word) ⇒ Object



62
63
64
65
66
67
68
69
# File 'ext/rule_tagger/rbtagger.c', line 62

static VALUE
BrillTagger_add_goodleft( VALUE self, VALUE word )
{
  TaggerContext *tc;
  Data_Get_Struct( self, TaggerContext, tc );
  tagger_context_add_goodleft( tc, RSTRING_PTR(word) );
  return Qnil;
}

#add_goodright(word) ⇒ Object



71
72
73
74
75
76
77
78
# File 'ext/rule_tagger/rbtagger.c', line 71

static VALUE
BrillTagger_add_goodright( VALUE self, VALUE word )
{
  TaggerContext *tc;
  Data_Get_Struct( self, TaggerContext, tc );
  tagger_context_add_goodright( tc, RSTRING_PTR(word) );
  return Qnil;
}

#add_lexical_rule(rule) ⇒ Object



45
46
47
48
49
50
51
52
# File 'ext/rule_tagger/rbtagger.c', line 45

static VALUE
BrillTagger_add_lexical_rule( VALUE self, VALUE rule )
{
  TaggerContext *tc;
  Data_Get_Struct( self, TaggerContext, tc );
  tagger_context_add_lexical_rule( tc, RSTRING_PTR(rule) );
  return Qnil;
}

#add_to_lexicon(word, tag) ⇒ Object



21
22
23
24
25
26
27
28
# File 'ext/rule_tagger/rbtagger.c', line 21

static VALUE
BrillTagger_add_to_lexicon( VALUE self, VALUE word, VALUE tag )
{
  TaggerContext *tc;
  Data_Get_Struct( self, TaggerContext, tc );
  tagger_context_add_to_lexicon( tc, RSTRING_PTR(word), RSTRING_PTR(tag) );
  return Qnil;
}

#add_to_lexicon_tags(bigram) ⇒ Object



29
30
31
32
33
34
35
36
# File 'ext/rule_tagger/rbtagger.c', line 29

static VALUE
BrillTagger_add_to_lexicon_tags( VALUE self, VALUE bigram )
{
  TaggerContext *tc;
  Data_Get_Struct( self, TaggerContext, tc );
  tagger_context_add_to_lexicon_tags( tc, RSTRING_PTR(bigram) );
  return Qnil;
}

#add_word_to_wordlist(word) ⇒ Object



53
54
55
56
57
58
59
60
# File 'ext/rule_tagger/rbtagger.c', line 53

static VALUE
BrillTagger_add_word_to_wordlist( VALUE self, VALUE word )
{
  TaggerContext *tc;
  Data_Get_Struct( self, TaggerContext, tc );
  tagger_context_add_word_to_wordlist( tc, RSTRING_PTR(word) );
  return Qnil;
}

#apply_contextual_rules(tokens, tags, rmove) ⇒ Object



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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'ext/rule_tagger/rbtagger.c', line 181

static VALUE
BrillTagger_apply_contextual_rules( VALUE self, VALUE tokens, VALUE tags, VALUE rmove )
{
  int i;
  int token_length = RARRAY_LEN(tokens);
  int tags_length = RARRAY_LEN(tags);
  int rules_length;
  int restrict_move = NUM2INT( rmove );
  char **text_tags, **text_tokens;
  VALUE fetched;
  TaggerContext *tc;
  Data_Get_Struct( self, TaggerContext, tc );

  if( token_length != tags_length ){
    rb_raise(rb_eArgError, "Error: tags and tokens must be of equal length!");
    return Qnil;
  }
  if( restrict_move && Registry_entry_count( tc->lexicon_hash ) == 0 ){
    rb_raise(rb_eArgError, "Must load a leicon before applying contextual rules");
    return Qnil;
  }

  text_tags = (char**)malloc(sizeof(char*) * tags_length );
  text_tokens = (char**)malloc(sizeof(char*) * token_length );

  // load the tokens and tags into the char * arrays
  for( i = 0; i < token_length; ++i ){
    fetched = rb_ary_entry(tokens,i);
    text_tokens[i] = strdup(RSTRING_PTR(fetched));
    fetched = rb_ary_entry(tags,i);
    text_tags[i] = strdup(RSTRING_PTR(fetched));
  }

  rules_length = Darray_len(tc->contextual_rule_array);
  // Apply the rules
  for( i = 0; i < rules_length; ++i ){
    apply_contextual_rule(Darray_get(tc->contextual_rule_array, i),
                          text_tokens, text_tags, token_length,
                          restrict_move, tc->lexicon_hash, tc->lexicon_tag_hash);
  }

  // load the results back into ruby arrays
  for( i = 0; i < token_length; ++i ){
    rb_ary_store( tags, i, rb_str_new2(text_tags[i]) );
    free(text_tags[i]);
    free(text_tokens[i]);
  }

  free( text_tags );
  free( text_tokens );

  return Qnil;
}

#apply_lexical_rules(tokens, tags, wordlist, extrawds) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
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
142
143
144
145
146
# File 'ext/rule_tagger/rbtagger.c', line 80

static VALUE
BrillTagger_apply_lexical_rules( VALUE self, VALUE tokens, VALUE tags, VALUE wordlist, VALUE extrawds )
{
  TaggerContext *tc;
  int i = 0;
  int token_length = RARRAY_LEN(tokens);
  int tags_length = RARRAY_LEN(tags);
  int rules_length;
  VALUE fetched;
  int EXTRAWDS = NUM2INT( extrawds );
  Data_Get_Struct( self, TaggerContext, tc );

  if( token_length != tags_length ){
    rb_raise(rb_eArgError, "Error: tags and tokens must be of equal length!");
    return Qnil;
  }
      
  Darray text_array = Darray_create();
  Darray tag_array  = Darray_create();

  Darray_hint( text_array, token_length, token_length );
  Darray_hint( tag_array, token_length, token_length );

  for( i = 0; i < token_length; ++i ){
    fetched = rb_ary_entry(tokens,i);
    if( fetched == Qnil ){
      fprintf(stderr, "token missing %d of %d\n", i, token_length );
      rb_raise(rb_eArgError, "Token was missing unexpectedly");
      return Qnil;
    }
    Darray_addh(text_array, (VOIDP)strdup(RSTRING_PTR(fetched)) );
    fetched = rb_ary_entry(tags,i);
    if( fetched == Qnil ){
      fprintf(stderr, "tag missing %d of %d\n", i, token_length );
      rb_raise(rb_eArgError, "Tag was missing unexpectedly");
      return Qnil;
    }
    Darray_addh(tag_array, (VOIDP)strdup(RSTRING_PTR(fetched)) );
  }
  rules_length = Darray_len(tc->rule_array);
  /* Apply the rules */
  for( i = 0; i < rules_length; ++i ) {
    apply_lexical_rule( Darray_get(tc->rule_array, i),
                        text_array, tag_array,
                        tc->lexicon_hash,
                        tc->wordlist_hash,
                        tc->bigram_hash,
                        EXTRAWDS );
  }
  /* Stuff the results back into the ruby arrays */
  for( i = 0; i < token_length; ++i ) {
    char *text_strref = (char*)Darray_get( text_array, i );
    char *tag_strref = (char*)Darray_get( tag_array, i );

    // copy into ruby space
    rb_ary_store( tokens, i, rb_str_new2(text_strref) );
    rb_ary_store( tags, i, rb_str_new2( tag_strref ) );

    free( text_strref );
    free( tag_strref );
  }

  Darray_destroy(text_array);
  Darray_destroy(tag_array);

  return Qnil;
}

#default_tag_finish(tokens, tags) ⇒ Object



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
# File 'ext/rule_tagger/rbtagger.c', line 147

static VALUE
BrillTagger_default_tag_finish( VALUE self, VALUE tokens, VALUE tags )
{
  int i;
  VALUE fetched, word;
  char *tempstr;
  int token_length = RARRAY_LEN(tokens);
  int tags_length = RARRAY_LEN(tags);
  TaggerContext *tc;

  Data_Get_Struct( self, TaggerContext, tc );

  if( token_length != tags_length ){
    rb_raise(rb_eArgError, "Error: tags and tokens must be of equal length!");
    return Qnil;
  }

  for( i = 0; i < token_length; ++i ){
    fetched = rb_ary_entry(tokens,i);
    if( fetched == Qnil ){
      rb_raise(rb_eArgError, "Token was missing unexpectedly");
      return Qnil;
    }
    word = fetched;

    if( (tempstr = Registry_get(tc->lexicon_hash, RSTRING_PTR(word))) != NULL ){
      //fetched = rb_ary_entry(tags,i);
      //printf( "'%s'/%s -> %s\n", RSTRING_PTR(word), RSTRING_PTR(fetched), tempstr );
      rb_ary_store( tags, i, rb_str_new2(tempstr) );
    }
  }
  return Qnil;
}