linkparser 1.1.3 → 2.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,11 +1,11 @@
1
1
  /*
2
2
  * sentence.c - Ruby LinkParser
3
- * $Id: sentence.c,v 21df914cedb1 2010/10/12 15:45:26 ged $
4
- *
3
+ * $Id$
4
+ *
5
5
  * Authors:
6
6
  * * Michael Granger <ged@FaerieMUD.org>
7
- *
8
- * Please see the LICENSE file at the top of the distribution for licensing
7
+ *
8
+ * Please see the LICENSE file at the top of the distribution for licensing
9
9
  * information.
10
10
  */
11
11
 
@@ -30,7 +30,8 @@
30
30
  * Allocation function
31
31
  */
32
32
  static struct rlink_sentence *
33
- rlink_sentence_alloc() {
33
+ rlink_sentence_alloc()
34
+ {
34
35
  struct rlink_sentence *ptr = ALLOC( struct rlink_sentence );
35
36
 
36
37
  ptr->sentence = NULL;
@@ -38,7 +39,7 @@ rlink_sentence_alloc() {
38
39
  ptr->parsed_p = Qfalse;
39
40
  ptr->options = Qnil;
40
41
 
41
- debugMsg(( "Initialized an rlink_sentence <%p>", ptr ));
42
+ rlink_log( "debug", "Initialized an rlink_sentence <%p>", ptr );
42
43
  return ptr;
43
44
  }
44
45
 
@@ -47,17 +48,12 @@ rlink_sentence_alloc() {
47
48
  * GC Mark function
48
49
  */
49
50
  static void
50
- rlink_sentence_gc_mark( struct rlink_sentence *ptr ) {
51
- debugMsg(( "Marking LinkParser::Sentence %p", ptr ));
52
-
51
+ rlink_sentence_gc_mark( struct rlink_sentence *ptr )
52
+ {
53
53
  if ( ptr ) {
54
54
  rb_gc_mark( ptr->dictionary );
55
55
  rb_gc_mark( ptr->options );
56
56
  }
57
-
58
- else {
59
- debugMsg(( "Not marking uninitialized rlink_sentence struct" ));
60
- }
61
57
  }
62
58
 
63
59
 
@@ -65,36 +61,27 @@ rlink_sentence_gc_mark( struct rlink_sentence *ptr ) {
65
61
  * GC Free function
66
62
  */
67
63
  static void
68
- rlink_sentence_gc_free( struct rlink_sentence *ptr ) {
64
+ rlink_sentence_gc_free( struct rlink_sentence *ptr )
65
+ {
69
66
  if ( ptr ) {
70
- debugMsg(( "In free function of Sentence <%p>", ptr ));
71
-
72
67
  if ( ptr->dictionary && TYPE(ptr->dictionary) == T_DATA ) {
73
68
  struct rlink_dictionary *dictionary = rlink_get_dict( ptr->dictionary );
74
- debugMsg(( " sentence's dictionary is: <%p>", dictionary ));
75
69
 
76
70
  /* Freeing the dictionary automatically frees the sentences it belongs to, so
77
71
  don't double-free if the dictionary struct or its pointer is done. */
78
- if ( dictionary->dict ) {
79
- debugMsg(( " deleting Sentence <%p>", ptr->sentence ));
72
+ if ( dictionary->dict && ptr->sentence ) {
73
+ rlink_log( "Deleting sentence %p", "debug", ptr->sentence );
80
74
  sentence_delete( (Sentence)ptr->sentence );
81
75
  }
82
- } else {
83
- debugMsg(( " not deleting a Sentence belonging to an already-freed dictionary." ));
84
76
  }
85
77
 
86
78
  ptr->sentence = NULL;
87
79
  ptr->options = Qnil;
88
80
  ptr->dictionary = Qnil;
89
81
 
90
- debugMsg(( " freeing rlink_sentence <%p>", ptr ));
91
82
  xfree( ptr );
92
83
  ptr = NULL;
93
84
  }
94
-
95
- else {
96
- debugMsg(( "Not freeing an uninitialized rlink_sentence struct" ));
97
- }
98
85
  }
99
86
 
100
87
 
@@ -102,7 +89,8 @@ rlink_sentence_gc_free( struct rlink_sentence *ptr ) {
102
89
  * Object validity checker. Returns the data pointer.
103
90
  */
104
91
  static struct rlink_sentence *
105
- check_sentence( VALUE self ) {
92
+ check_sentence( VALUE self )
93
+ {
106
94
  Check_Type( self, T_DATA );
107
95
 
108
96
  if ( !IsSentence(self) ) {
@@ -118,7 +106,8 @@ check_sentence( VALUE self ) {
118
106
  * Fetch the data pointer and check it for sanity.
119
107
  */
120
108
  static struct rlink_sentence *
121
- get_sentence( VALUE self ) {
109
+ get_sentence( VALUE self )
110
+ {
122
111
  struct rlink_sentence *ptr = check_sentence( self );
123
112
 
124
113
  if ( !ptr )
@@ -132,7 +121,8 @@ get_sentence( VALUE self ) {
132
121
  * Publicly-usable sentence-fetcher
133
122
  */
134
123
  struct rlink_sentence *
135
- rlink_get_sentence( VALUE self ) {
124
+ rlink_get_sentence( VALUE self )
125
+ {
136
126
  return get_sentence( self );
137
127
  }
138
128
 
@@ -150,8 +140,9 @@ rlink_get_sentence( VALUE self ) {
150
140
  *
151
141
  */
152
142
  static VALUE
153
- rlink_sentence_s_alloc( VALUE klass ) {
154
- debugMsg(( "Wrapping an uninitialized Sentence pointer." ));
143
+ rlink_sentence_s_alloc( VALUE klass )
144
+ {
145
+ rlink_log( "debug", "Wrapping an uninitialized Sentence pointer." );
155
146
  return Data_Wrap_Struct( klass, rlink_sentence_gc_mark, rlink_sentence_gc_free, 0 );
156
147
  }
157
148
 
@@ -165,13 +156,14 @@ rlink_sentence_s_alloc( VALUE klass ) {
165
156
  * LinkParser::Sentence.new( str, dict ) -> sentence
166
157
  *
167
158
  * Create a new LinkParser::Sentence object from the given input string
168
- # using the specified LinkParser::Dictionary.
159
+ * using the specified LinkParser::Dictionary.
169
160
  *
170
161
  * dict = LinkParser::Dictionary.new
171
162
  * LinkParser::Sentence.new( "The boy runs", dict ) #=> #<LinkParser::Sentence:0x5481ac>
172
163
  */
173
164
  static VALUE
174
- rlink_sentence_init( VALUE self, VALUE input_string, VALUE dictionary ) {
165
+ rlink_sentence_init( VALUE self, VALUE input_string, VALUE dictionary )
166
+ {
175
167
  if ( !check_sentence(self) ) {
176
168
  struct rlink_sentence *ptr;
177
169
  Sentence sent;
@@ -200,12 +192,13 @@ rlink_sentence_init( VALUE self, VALUE input_string, VALUE dictionary ) {
200
192
  * sentence.parse( options={} ) -> fixnum
201
193
  *
202
194
  * Attach a parse set to this sentence and return the number of linkages
203
- * found. If any +options+ are specified, they override those set in the
195
+ * found. If any +options+ are specified, they override those set in the
204
196
  * sentence's dictionary.
205
- *
197
+ *
206
198
  */
207
199
  static VALUE
208
- rlink_sentence_parse( int argc, VALUE *argv, VALUE self ) {
200
+ rlink_sentence_parse( int argc, VALUE *argv, VALUE self )
201
+ {
209
202
  struct rlink_sentence *ptr = get_sentence( self );
210
203
  Parse_Options opts;
211
204
  VALUE defopts = Qnil;
@@ -216,7 +209,7 @@ rlink_sentence_parse( int argc, VALUE *argv, VALUE self ) {
216
209
  if ( RTEST(ptr->parsed_p) )
217
210
  rb_raise( rlink_eLpError, "Can't reparse a sentence." );
218
211
  */
219
- debugMsg(( "Parsing sentence <%p>", ptr ));
212
+ rlink_log_obj( self, "debug", "Parsing sentence <%p>", ptr );
220
213
 
221
214
  /* Merge the hash from this call with the one from the dict and build
222
215
  Parse_Options from it. */
@@ -250,7 +243,8 @@ rlink_sentence_parse( int argc, VALUE *argv, VALUE self ) {
250
243
  * sentence.parsed? #-> true
251
244
  */
252
245
  static VALUE
253
- rlink_sentence_parsed_p( VALUE self ) {
246
+ rlink_sentence_parsed_p( VALUE self )
247
+ {
254
248
  struct rlink_sentence *ptr = get_sentence( self );
255
249
  return ptr->parsed_p;
256
250
  }
@@ -266,7 +260,8 @@ rlink_sentence_parsed_p( VALUE self ) {
266
260
  * sentence.options.islands_ok? # -> true
267
261
  */
268
262
  static VALUE
269
- rlink_sentence_options( VALUE self ) {
263
+ rlink_sentence_options( VALUE self )
264
+ {
270
265
  struct rlink_sentence *ptr = get_sentence( self );
271
266
  return ptr->options;
272
267
  }
@@ -283,7 +278,8 @@ rlink_sentence_options( VALUE self ) {
283
278
  *
284
279
  */
285
280
  static VALUE
286
- rlink_sentence_linkages( VALUE self ) {
281
+ rlink_sentence_linkages( VALUE self )
282
+ {
287
283
  struct rlink_sentence *ptr = get_sentence( self );
288
284
  int i, count = 0;
289
285
  VALUE rary;
@@ -313,13 +309,13 @@ rlink_sentence_linkages( VALUE self ) {
313
309
  * call-seq:
314
310
  * sentence.length -> fixnum
315
311
  *
316
- * Returns the number of words in the tokenized sentence, including the
312
+ * Returns the number of words in the tokenized sentence, including the
317
313
  * boundary words and punctuation.
318
314
  *
319
315
  */
320
-
321
316
  static VALUE
322
- rlink_sentence_length( VALUE self ) {
317
+ rlink_sentence_length( VALUE self )
318
+ {
323
319
  struct rlink_sentence *ptr = get_sentence( self );
324
320
 
325
321
  if ( !RTEST(ptr->parsed_p) )
@@ -329,86 +325,6 @@ rlink_sentence_length( VALUE self ) {
329
325
  }
330
326
 
331
327
 
332
- /*
333
- * call-seq:
334
- * sentence.word( idx ) -> str
335
- *
336
- * Returns the spelling of the n-th word in the sentence as it appears after
337
- * tokenization.
338
- */
339
- static VALUE
340
- rlink_sentence_word( VALUE self, VALUE n ) {
341
- struct rlink_sentence *ptr = get_sentence( self );
342
- const char *word;
343
-
344
- if ( !RTEST(ptr->parsed_p) )
345
- rlink_sentence_parse( 0, 0, self );
346
-
347
- word = sentence_get_word( (Sentence)ptr->sentence, FIX2INT(n) );
348
- return rb_str_new2( word );
349
- }
350
-
351
-
352
- /*
353
- * call-seq:
354
- * sentence.words -> array
355
- *
356
- * Returns the words of the sentence as they appear after tokenization.
357
- *
358
- * sentence = LinkParser::Dictionary.new.parse( "The dogs barks." )
359
- * sentence.words #->
360
- */
361
- static VALUE
362
- rlink_sentence_words( VALUE self ) {
363
- struct rlink_sentence *ptr = get_sentence( self );
364
- const char *word;
365
- int i, length;
366
- VALUE words = rb_ary_new();
367
-
368
- if ( !RTEST(ptr->parsed_p) )
369
- rlink_sentence_parse( 0, 0, self );
370
-
371
- length = sentence_length( (Sentence)ptr->sentence );
372
- for ( i = 0; i < length; i++ ) {
373
- word = sentence_get_word( (Sentence)ptr->sentence, i );
374
- debugMsg(( "Word %d: <%s>", i, word ));
375
- rb_ary_push( words, rb_str_new2(word) );
376
- }
377
-
378
- return words;
379
- }
380
-
381
-
382
- /*
383
- * call-seq:
384
- * sentence[index] -> str
385
- * sentence[start, length] -> str
386
- * sentence[range] -> str
387
- *
388
- * Element Reference---Returns the element at index, or returns a subarray
389
- * starting at start and continuing for length elements, or returns a subarray
390
- * specified by range. Negative indices count backward from the end of the
391
- * array (-1 is the last element). Returns nil if the index (or starting
392
- * index) are out of range.
393
- *
394
- * sent = dict.parse( "Birds fly south for the winter." )
395
- *
396
- * sent[1] # => "birds"
397
- * sent[0,4] # => ["LEFT-WALL", "birds", "fly", "south"]
398
- * sent[1..3] # => ["birds", "fly", "south"]
399
- *
400
- */
401
- static VALUE
402
- rlink_sentence_aref( argc, argv, self )
403
- int argc;
404
- VALUE *argv;
405
- VALUE self;
406
- {
407
- VALUE words = rlink_sentence_words( self );
408
- return rb_funcall2( words, rb_intern("[]"), argc, argv );
409
- }
410
-
411
-
412
328
  /*
413
329
  * call-seq:
414
330
  * sentence.null_count -> int
@@ -416,7 +332,8 @@ rlink_sentence_aref( argc, argv, self )
416
332
  * Returns the number of null links that were used in parsing the sentence.
417
333
  */
418
334
  static VALUE
419
- rlink_sentence_null_count( VALUE self ) {
335
+ rlink_sentence_null_count( VALUE self )
336
+ {
420
337
  struct rlink_sentence *ptr = get_sentence( self );
421
338
  int count;
422
339
 
@@ -432,11 +349,12 @@ rlink_sentence_null_count( VALUE self ) {
432
349
  * call-seq:
433
350
  * sentence.num_linkages_found -> fixnum
434
351
  *
435
- * Returns the number of linkages found when parsing the sentence. This will
352
+ * Returns the number of linkages found when parsing the sentence. This will
436
353
  * cause the sentence to be parsed if it hasn't been already.
437
354
  */
438
355
  static VALUE
439
- rlink_sentence_num_linkages_found( VALUE self ) {
356
+ rlink_sentence_num_linkages_found( VALUE self )
357
+ {
440
358
  struct rlink_sentence *ptr = get_sentence( self );
441
359
  int i = 0;
442
360
 
@@ -456,7 +374,8 @@ rlink_sentence_num_linkages_found( VALUE self ) {
456
374
  * Return the number of linkages that had no post-processing violations.
457
375
  */
458
376
  static VALUE
459
- rlink_sentence_num_valid_linkages( VALUE self ) {
377
+ rlink_sentence_num_valid_linkages( VALUE self )
378
+ {
460
379
  struct rlink_sentence *ptr = get_sentence( self );
461
380
  int count;
462
381
 
@@ -472,11 +391,12 @@ rlink_sentence_num_valid_linkages( VALUE self ) {
472
391
  * call-seq:
473
392
  * sentence.num_linkages_post_processed -> fixnum
474
393
  *
475
- * Return the number of linkages that were actually post-processed (which may
394
+ * Return the number of linkages that were actually post-processed (which may
476
395
  * be less than the number found because of the linkage_limit parameter).
477
396
  */
478
397
  static VALUE
479
- rlink_sentence_num_linkages_post_processed( VALUE self ) {
398
+ rlink_sentence_num_linkages_post_processed( VALUE self )
399
+ {
480
400
  struct rlink_sentence *ptr = get_sentence( self );
481
401
  int count;
482
402
 
@@ -492,11 +412,12 @@ rlink_sentence_num_linkages_post_processed( VALUE self ) {
492
412
  * call-seq:
493
413
  * sentence.num_violations( i ) -> fixnum
494
414
  *
495
- * The number of post-processing violations that the i-th linkage had during
415
+ * The number of post-processing violations that the i-th linkage had during
496
416
  * the last parse.
497
417
  */
498
418
  static VALUE
499
- rlink_sentence_num_violations( VALUE self, VALUE i ) {
419
+ rlink_sentence_num_violations( VALUE self, VALUE i )
420
+ {
500
421
  struct rlink_sentence *ptr = get_sentence( self );
501
422
  int count;
502
423
 
@@ -515,7 +436,8 @@ rlink_sentence_num_violations( VALUE self, VALUE i ) {
515
436
  * The maximum cost of connectors used in the i-th linkage of the sentence.
516
437
  */
517
438
  static VALUE
518
- rlink_sentence_disjunct_cost( VALUE self, VALUE i ) {
439
+ rlink_sentence_disjunct_cost( VALUE self, VALUE i )
440
+ {
519
441
  struct rlink_sentence *ptr = get_sentence( self );
520
442
  int count;
521
443
 
@@ -529,17 +451,18 @@ rlink_sentence_disjunct_cost( VALUE self, VALUE i ) {
529
451
 
530
452
  /*
531
453
  * Document-class: LinkParser::Sentence
532
- *
454
+ *
533
455
  * A Sentence is the API's representation of an input string,
534
456
  * tokenized and interpreted according to a specific Dictionary. After
535
457
  * a Sentence is created and parsed, various attributes of the
536
458
  * resulting set of linkages can be obtained.
537
- *
459
+ *
538
460
  */
539
461
  void
540
- rlink_init_sentence() {
462
+ rlink_init_sentence()
463
+ {
541
464
  rlink_cSentence = rb_define_class_under( rlink_mLinkParser, "Sentence",
542
- rb_cObject );
465
+ rb_cObject );
543
466
 
544
467
  rb_define_alloc_func( rlink_cSentence, rlink_sentence_s_alloc );
545
468
 
@@ -551,27 +474,18 @@ rlink_init_sentence() {
551
474
  rb_define_method( rlink_cSentence, "options", rlink_sentence_options, 0 );
552
475
 
553
476
  rb_define_method( rlink_cSentence, "length", rlink_sentence_length, 0 );
554
- rb_define_method( rlink_cSentence, "word", rlink_sentence_word, 1 );
555
- rb_define_method( rlink_cSentence, "words", rlink_sentence_words, 0 );
556
- rb_define_method( rlink_cSentence, "[]", rlink_sentence_aref, -1 );
557
477
 
558
- rb_define_method( rlink_cSentence, "null_count",
478
+ rb_define_method( rlink_cSentence, "null_count",
559
479
  rlink_sentence_null_count, 0 );
560
- rb_define_method( rlink_cSentence, "num_linkages_found",
480
+ rb_define_method( rlink_cSentence, "num_linkages_found",
561
481
  rlink_sentence_num_linkages_found, 0 );
562
- rb_define_method( rlink_cSentence, "num_valid_linkages",
482
+ rb_define_method( rlink_cSentence, "num_valid_linkages",
563
483
  rlink_sentence_num_valid_linkages, 0 );
564
- rb_define_method( rlink_cSentence, "num_linkages_post_processed",
484
+ rb_define_method( rlink_cSentence, "num_linkages_post_processed",
565
485
  rlink_sentence_num_linkages_post_processed, 0 );
566
- rb_define_method( rlink_cSentence, "num_violations",
486
+ rb_define_method( rlink_cSentence, "num_violations",
567
487
  rlink_sentence_num_violations, 1 );
568
- rb_define_method( rlink_cSentence, "disjunct_cost",
488
+ rb_define_method( rlink_cSentence, "disjunct_cost",
569
489
  rlink_sentence_disjunct_cost, 1 );
570
-
571
- /*
572
- link_public_api(char *) sentence_get_nth_word(Sentence sent, int i);
573
- link_public_api(int) sentence_nth_word_has_disjunction(Sentence sent, int i);
574
- */
575
-
576
490
  }
577
491
 
@@ -1,14 +1,20 @@
1
- #!/usr/bin/ruby
1
+ # -*- ruby -*-
2
+ # frozen_string_literal: true
3
+
4
+ require 'loggability'
2
5
 
3
6
  # The LinkParser top-level namespace.
4
- #
5
- # @author Michael Granger <ged@FaerieMUD.org>
6
- # @author Martin Chase <stillflame@FaerieMUD.org>
7
- #
8
7
  module LinkParser
8
+ extend Loggability
9
+
9
10
 
10
11
  # Release version
11
- VERSION = '1.1.3'
12
+ VERSION = '2.2.0'
13
+
14
+
15
+ # Loggability API -- set up a logger
16
+ log_as :linkparser
17
+
12
18
 
13
19
  # Load the correct version if it's a Windows binary gem
14
20
  if RUBY_PLATFORM =~/(mswin|mingw)/i
@@ -25,8 +31,10 @@ module LinkParser
25
31
  end
26
32
 
27
33
  require 'linkparser/mixins'
34
+ require 'linkparser/dictionary'
28
35
  require 'linkparser/sentence'
29
36
  require 'linkparser/linkage'
37
+ require 'linkparser/parseoptions'
30
38
 
31
39
 
32
40
  end # class LinkParser