Commits

Michael Granger  committed 374b81f

* More documentation cleanup.
* Fixed Sentence#[]
* Added some more Sentence tests.

  • Participants
  • Parent commits d9ed649

Comments (0)

Files changed (5)

File LinkParser Project.tmproj

 <plist version="1.0">
 <dict>
 	<key>currentDocument</key>
-	<string>lib/linkparser/linkage.rb</string>
+	<string>Rakefile</string>
 	<key>documents</key>
 	<array>
 		<dict>
 	<integer>219</integer>
 	<key>metaData</key>
 	<dict>
-		<key>.irbrc</key>
+		<key>Rakefile</key>
 		<dict>
 			<key>caret</key>
 			<dict>
 				<key>column</key>
 				<integer>0</integer>
 				<key>line</key>
-				<integer>29</integer>
+				<integer>4</integer>
 			</dict>
 			<key>firstVisibleColumn</key>
 			<integer>0</integer>
 			<key>firstVisibleLine</key>
 			<integer>0</integer>
 		</dict>
-		<key>docs/link-includes.h</key>
-		<dict>
-			<key>caret</key>
-			<dict>
-				<key>column</key>
-				<integer>25</integer>
-				<key>line</key>
-				<integer>271</integer>
-			</dict>
-			<key>columnSelection</key>
-			<false/>
-			<key>firstVisibleColumn</key>
-			<integer>0</integer>
-			<key>firstVisibleLine</key>
-			<integer>243</integer>
-			<key>selectFrom</key>
-			<dict>
-				<key>column</key>
-				<integer>16</integer>
-				<key>line</key>
-				<integer>271</integer>
-			</dict>
-			<key>selectTo</key>
-			<dict>
-				<key>column</key>
-				<integer>25</integer>
-				<key>line</key>
-				<integer>271</integer>
-			</dict>
-		</dict>
-		<key>docs/public-functions.h</key>
-		<dict>
-			<key>caret</key>
-			<dict>
-				<key>column</key>
-				<integer>0</integer>
-				<key>line</key>
-				<integer>1</integer>
-			</dict>
-			<key>firstVisibleColumn</key>
-			<integer>0</integer>
-			<key>firstVisibleLine</key>
-			<integer>4</integer>
-		</dict>
-		<key>ext/dictionary.c</key>
+		<key>ext/Makefile</key>
 		<dict>
 			<key>caret</key>
 			<dict>
 			<key>firstVisibleColumn</key>
 			<integer>0</integer>
 			<key>firstVisibleLine</key>
-			<integer>105</integer>
+			<integer>79</integer>
+		</dict>
+		<key>ext/dictionary.c</key>
+		<dict>
+			<key>caret</key>
+			<dict>
+				<key>column</key>
+				<integer>0</integer>
+				<key>line</key>
+				<integer>3</integer>
+			</dict>
+			<key>firstVisibleColumn</key>
+			<integer>0</integer>
+			<key>firstVisibleLine</key>
+			<integer>85</integer>
 		</dict>
 		<key>ext/extconf.rb</key>
 		<dict>
 			<key>caret</key>
 			<dict>
 				<key>column</key>
-				<integer>32</integer>
+				<integer>0</integer>
 				<key>line</key>
-				<integer>33</integer>
+				<integer>36</integer>
 			</dict>
 			<key>firstVisibleColumn</key>
 			<integer>0</integer>
 				<key>column</key>
 				<integer>0</integer>
 				<key>line</key>
-				<integer>738</integer>
+				<integer>0</integer>
 			</dict>
 			<key>firstVisibleColumn</key>
 			<integer>0</integer>
 			<key>firstVisibleLine</key>
-			<integer>677</integer>
+			<integer>0</integer>
 		</dict>
 		<key>ext/linkparser.c</key>
 		<dict>
 			<key>caret</key>
 			<dict>
 				<key>column</key>
-				<integer>4</integer>
+				<integer>0</integer>
 				<key>line</key>
-				<integer>92</integer>
+				<integer>33</integer>
 			</dict>
 			<key>firstVisibleColumn</key>
 			<integer>0</integer>
 			<key>firstVisibleLine</key>
-			<integer>48</integer>
+			<integer>4</integer>
 		</dict>
 		<key>ext/linkparser.h</key>
 		<dict>
 			<key>caret</key>
 			<dict>
 				<key>column</key>
-				<integer>0</integer>
+				<integer>7</integer>
 				<key>line</key>
-				<integer>0</integer>
+				<integer>61</integer>
 			</dict>
+			<key>columnSelection</key>
+			<false/>
 			<key>firstVisibleColumn</key>
 			<integer>0</integer>
 			<key>firstVisibleLine</key>
-			<integer>58</integer>
+			<integer>45</integer>
+			<key>selectFrom</key>
+			<dict>
+				<key>column</key>
+				<integer>32</integer>
+				<key>line</key>
+				<integer>61</integer>
+			</dict>
+			<key>selectTo</key>
+			<dict>
+				<key>column</key>
+				<integer>7</integer>
+				<key>line</key>
+				<integer>61</integer>
+			</dict>
 		</dict>
 		<key>ext/parseoptions.c</key>
 		<dict>
 			<key>caret</key>
 			<dict>
 				<key>column</key>
-				<integer>23</integer>
+				<integer>0</integer>
 				<key>line</key>
-				<integer>80</integer>
+				<integer>496</integer>
 			</dict>
 			<key>firstVisibleColumn</key>
 			<integer>0</integer>
 			<key>firstVisibleLine</key>
-			<integer>456</integer>
+			<integer>465</integer>
 		</dict>
-		<key>lib/linkparser.rb</key>
-		<dict>
-			<key>caret</key>
-			<dict>
-				<key>column</key>
-				<integer>28</integer>
-				<key>line</key>
-				<integer>33</integer>
-			</dict>
-			<key>firstVisibleColumn</key>
-			<integer>0</integer>
-			<key>firstVisibleLine</key>
-			<integer>0</integer>
-		</dict>
-		<key>lib/linkparser/linkage.rb</key>
-		<dict>
-			<key>caret</key>
-			<dict>
-				<key>column</key>
-				<integer>72</integer>
-				<key>line</key>
-				<integer>148</integer>
-			</dict>
-			<key>firstVisibleColumn</key>
-			<integer>0</integer>
-			<key>firstVisibleLine</key>
-			<integer>124</integer>
-		</dict>
-		<key>lib/linkparser/sentence.rb</key>
-		<dict>
-			<key>caret</key>
-			<dict>
-				<key>column</key>
-				<integer>7</integer>
-				<key>line</key>
-				<integer>58</integer>
-			</dict>
-			<key>firstVisibleColumn</key>
-			<integer>0</integer>
-			<key>firstVisibleLine</key>
-			<integer>7</integer>
-		</dict>
-		<key>test.rb</key>
-		<dict>
-			<key>caret</key>
-			<dict>
-				<key>column</key>
-				<integer>40</integer>
-				<key>line</key>
-				<integer>31</integer>
-			</dict>
-			<key>firstVisibleColumn</key>
-			<integer>0</integer>
-			<key>firstVisibleLine</key>
-			<integer>0</integer>
-		</dict>
-		<key>tests/dictionary.tests.rb</key>
+		<key>extconf.rb</key>
 		<dict>
 			<key>caret</key>
 			<dict>
 				<key>column</key>
 				<integer>0</integer>
 				<key>line</key>
-				<integer>0</integer>
+				<integer>44</integer>
 			</dict>
 			<key>firstVisibleColumn</key>
 			<integer>0</integer>
 			<key>firstVisibleLine</key>
-			<integer>59</integer>
+			<integer>21</integer>
 		</dict>
-		<key>tests/linkage.tests.rb</key>
+		<key>install.rb</key>
 		<dict>
 			<key>caret</key>
 			<dict>
 			<key>firstVisibleLine</key>
 			<integer>0</integer>
 		</dict>
-		<key>tests/parseoptions.tests.rb</key>
+		<key>lib/extconf.rb</key>
 		<dict>
 			<key>caret</key>
 			<dict>
 				<key>column</key>
 				<integer>0</integer>
 				<key>line</key>
-				<integer>0</integer>
+				<integer>40</integer>
 			</dict>
+			<key>columnSelection</key>
+			<false/>
 			<key>firstVisibleColumn</key>
 			<integer>0</integer>
 			<key>firstVisibleLine</key>
-			<integer>9</integer>
+			<integer>0</integer>
+			<key>selectFrom</key>
+			<dict>
+				<key>column</key>
+				<integer>0</integer>
+				<key>line</key>
+				<integer>20</integer>
+			</dict>
+			<key>selectTo</key>
+			<dict>
+				<key>column</key>
+				<integer>0</integer>
+				<key>line</key>
+				<integer>40</integer>
+			</dict>
 		</dict>
-		<key>tests/sentence.tests.rb</key>
+		<key>lib/linkparser/linkage.rb</key>
 		<dict>
 			<key>caret</key>
 			<dict>
 				<key>column</key>
 				<integer>0</integer>
 				<key>line</key>
-				<integer>28</integer>
+				<integer>210</integer>
 			</dict>
 			<key>firstVisibleColumn</key>
 			<integer>0</integer>
 			<key>firstVisibleLine</key>
-			<integer>8</integer>
+			<integer>170</integer>
+		</dict>
+		<key>tests/linkage.tests.rb</key>
+		<dict>
+			<key>caret</key>
+			<dict>
+				<key>column</key>
+				<integer>0</integer>
+				<key>line</key>
+				<integer>236</integer>
+			</dict>
+			<key>firstVisibleColumn</key>
+			<integer>0</integer>
+			<key>firstVisibleLine</key>
+			<integer>114</integer>
 		</dict>
 	</dict>
 	<key>openDocuments</key>
 	<array>
-		<string>ext/sentence.c</string>
-		<string>lib/linkparser/sentence.rb</string>
-		<string>ext/linkage.c</string>
-		<string>lib/linkparser/linkage.rb</string>
-		<string>lib/linkparser.rb</string>
-		<string>ext/linkparser.c</string>
+		<string>extconf.rb</string>
 		<string>ext/extconf.rb</string>
-		<string>.irbrc</string>
+		<string>lib/extconf.rb</string>
+		<string>Rakefile</string>
+		<string>ext/Makefile</string>
+		<string>install.rb</string>
 	</array>
 	<key>showFileHierarchyDrawer</key>
 	<true/>
 	<key>windowFrame</key>
-	<string>{{47, 0}, {836, 1002}}</string>
+	<string>{{45, 28}, {718, 1000}}</string>
 </dict>
 </plist>

File ext/sentence.c

  * Instance methods
  * -------------------- */
 
-
 /*
  *  call-seq:
  *     LinkParser::Sentence.new( str, dict )   => sentence
 
 /*
  *  call-seq:
- *     sent.parse( options={} )   => fixnum
+ *     sentence.parse( options={} )   => fixnum
  *
  *  Attach a parse set to this sentence and return the number of linkages
  *  found. If any +options+ are specified, they override those set in the 
 
 /*
  *  call-seq:
- *     parsed?   => true or false
+ *     sentence.parsed?   => true or false
  *
  *  Returns +true+ if the sentence has been parsed.
  *
 
 /*
  *  call-seq:
- *     linkages   => array
+ *     sentence.linkages   => array
  *
  *  Returns an Array of LinkParser::Linkage objects which represent the
  *  parts parsed from the sentence for the current linkage.
 }
 	
 
-/* 
- * word( n )
- * --
- * Returns the spelling of the n-th word in the sentence as it appears after 
- * tokenization.
+/*
+ *  call-seq:
+ *     sentence.word( idx )   => str
+ *
+ *  Returns the spelling of the n-th word in the sentence as it appears after 
+ *  tokenization.
  */
 static VALUE
 rlink_sentence_word( self, n )
 
 /*
  *  call-seq:
- *     words   => array
+ *     sentence.words   => array
  *
  *  Returns the words of the sentence as they appear after tokenization.
  *
 }
 
 
-/* 
- * null_count
- * --
- * Returns the number of null links that were used in parsing the sentence.
+/*
+ *  call-seq:
+ *     sentence[index]   		 => str
+ *     sentence[start, length]   => str
+ *     sentence[range]   		 => str
+ *
+ *  Element Reference---Returns the element at index, or returns a subarray 
+ *  starting at start and continuing for length elements, or returns a subarray 
+ *  specified by range. Negative indices count backward from the end of the 
+ *  array (-1 is the last element). Returns nil if the index (or starting 
+ *  index) are out of range.
+ *
+ *     sent = dict.parse( "Birds fly south for the winter." )
+ *     
+ *     sent[1]		# => "birds"
+ *     sent[0,4]	# => ["LEFT-WALL", "birds", "fly", "south"]
+ *     sent[1..3]	# => ["birds", "fly", "south"]
+ *     
+ */
+static VALUE
+rlink_sentence_aref( argc, argv, self )
+	int argc;
+	VALUE *argv;
+	VALUE self;
+{
+	VALUE words = rlink_sentence_words( self );
+	return rb_funcall2( words, rb_intern("[]"), argc, argv );
+}
+
+
+/*
+ *  call-seq:
+ *     sentence.null_count   => int
+ *
+ *  Returns the number of null links that were used in parsing the sentence.
  */
 static VALUE
 rlink_sentence_null_count( self )
 	
 
 /*
- * num_linkages_found => fixnum
- * --
- * Returns the number of linkages found when parsing the sentence. This will 
- * cause the sentence to be parsed if it hasn't been already.
+ *  call-seq:
+ *     sentence.num_linkages_found   => fixnum
+ *
+ *  Returns the number of linkages found when parsing the sentence. This will 
+ *  cause the sentence to be parsed if it hasn't been already.
  */
 static VALUE
 rlink_sentence_num_linkages_found( self )
 }
 
 
-/* 
- * num_valid_linkages => fixnum
- * --
- * Return the number of linkages that had no post-processing violations.
+/*
+ *  call-seq:
+ *     sentence.num_valid_linkages   => fixnum
+ *
+ *  Return the number of linkages that had no post-processing violations.
  */
 static VALUE
 rlink_sentence_num_valid_linkages( self )
 }
 	
 
-/* 
- * num_linkages_post_processed => fixnum
- * --
- * Return the number of linkages that were actually post-processed (which may 
- * be less than the number found because of the linkage_limit parameter).
+/*
+ *  call-seq:
+ *     sentence.num_linkages_post_processed   => fixnum
+ *
+ *  Return the number of linkages that were actually post-processed (which may 
+ *  be less than the number found because of the linkage_limit parameter).
  */
 static VALUE
 rlink_sentence_num_linkages_post_processed( self )
 }
 	
 
-/* 
- * num_violations( i )
- * --
- * The number of post-processing violations that the i-th linkage had during 
- * the last parse.
+/*
+ *  call-seq:
+ *     sentence.num_violations( i )   => fixnum
+ *
+ *  The number of post-processing violations that the i-th linkage had during 
+ *  the last parse.
  */
 static VALUE
 rlink_sentence_num_violations( self, i )
 }
 	
 
-/* 
- * disjunct_cost( i )
- * --
- * The maximum cost of connectors used in the i-th linkage of the sentence.
+/*
+ *  call-seq:
+ *     sentence.disjunct_cost( i )   => fixnum
+ *
+ *  The maximum cost of connectors used in the i-th linkage of the sentence.
  */
 static VALUE
 rlink_sentence_disjunct_cost( self, i )
 	rb_define_method( rlink_cSentence, "length", rlink_sentence_length, 0 );
 	rb_define_method( rlink_cSentence, "word", rlink_sentence_word, 1 );
 	rb_define_method( rlink_cSentence, "words", rlink_sentence_words, 0 );
-	rb_define_alias( rlink_cSentence, "[]", "word" );
+	rb_define_method( rlink_cSentence, "[]", rlink_sentence_aref, -1 );
 
 	rb_define_method( rlink_cSentence, "null_count", 
 		rlink_sentence_null_count, 0 );

File lib/linkparser/sentence.rb

 # 
 # Additional high-level functionality for LinkParser::Sentence objects.
 # 
-# == Synopsis
-# 
-#   
-# 
 # == Authors
 # 
 # * Michael Granger <ged@FaerieMUD.org>
 		end
 	end
 	
+	
+	#########
+	protected
+	#########
+
+	### Return the singleton class for this object
+	def singleton_class
+		class << self; self; end
+	end
+	
+
+	### Proxy method -- auto-delegate calls to the first linkage.
+	def method_missing( sym, *args )
+		linkage = self.linkages.first
+		return super unless linkage.respond_to?( sym )
+		
+		meth = linkage.method( sym )
+		self.singleton_class.send( :define_method, sym, &meth )
+		
+		meth.call( *args )
+	end
 
 end # class Sentence
 

File tests/linkage.tests.rb

 
 class LinkParser::Linkage::TestCase < Test::Unit::TestCase
 
-	@@dict = LinkParser::Dictionary.new( :verbosity => 0 )
+	@dict = LinkParser::Dictionary.new( :verbosity => 0 )
+	class << self; attr_accessor :dict; end
 
 	def setup
-		@simple_sentence = @@dict.parse( "The flag was wet." )
+		@simple_sentence = self.class.dict.parse( "The flag was wet." )
 		@ss_linkage = @simple_sentence.linkages.first
 		@conjunct_sentence = 
-			@@dict.parse( "The ball rolled down the hill and bumped the curb." )
+			self.class.dict.parse( "The ball rolled down the hill and bumped the curb." )
 	end
 
 

File tests/sentence.tests.rb

 
 class LinkParser::Sentence::TestCase < Test::Unit::TestCase
 
+	@dict = LinkParser::Dictionary.new( :verbosity => 0 )
+	class << self; attr_accessor :dict; end
+
+
 	def setup
-		@dict = LinkParser::Dictionary.new( :verbosity => 0 )
+		@dict = self.class.dict
 		@sentence = LinkParser::Sentence.new( "The cat runs.", @dict )
 	end
 	
 	end
 
 
+	def test_sentence_parse_should_accept_parse_options
+		rval = nil
+		
+		assert_nothing_raised do
+			@sentence.parse( :verbosity => 0 )
+		end
+	end
+	
+
 	def test_sentence_length_should_return_word_count
 		rval = nil
 		assert_nothing_raised do
 		assert_equal 6, rval, "'The cat runs.' should have a length of 6"
 	end
 
+
+	def test_sentence_delegates_linkage_methods_to_first_linkage
+		rval = nil
+		
+		assert_nothing_raised do
+			rval = @sentence.num_links
+		end
+		
+		assert_equal 5, rval
+	end
+
+
+	def test_sentence_parsed_p_should_return_false_for_unparsed_sentences
+		rval = nil
+		
+		assert_nothing_raised do
+			rval = @sentence.parsed?
+		end
+		
+		assert_equal false, rval
+	end
 	
-	# sentence_parse(Sentence sent, Parse_Options opts);
-	# sentence_length(Sentence sent);
-	# sentence_get_word(Sentence sent, int wordnum);
-	# sentence_null_count(Sentence sent);
-	# sentence_num_linkages_found(Sentence sent);
-	# sentence_num_valid_linkages(Sentence sent);
-	# sentence_num_linkages_post_processed(Sentence sent);
-	# sentence_num_violations(Sentence sent, int i);
-	# sentence_disjunct_cost(Sentence sent, int i);
-	# 
-	# sentence_get_nth_word(Sentence sent, int i);
-	# sentence_nth_word_has_disjunction(Sentence sent, int i);
+	
+	def test_sentence_parsed_p_should_return_true_for_parsed_sentences
+		rval = nil
+		@sentence.parse
+		
+		assert_nothing_raised do
+			rval = @sentence.parsed?
+		end
+		
+		assert_equal true, rval
+	end
+
+
+	def test_sentence_linkages_should_return_linkage_objects
+		rval = nil
+		
+		assert_nothing_raised do
+			rval = @sentence.linkages
+		end
+		
+		assert_instance_of Array, rval
+		rval.each do |obj|
+			assert_instance_of LinkParser::Linkage, obj
+		end
+	end
+	
+
+	def test_sentence_length_should_return_number_of_words_in_sentence
+		rval = nil
+		
+		assert_nothing_raised do
+			rval = @sentence.length
+		end
+		
+		assert_equal 6, rval
+	end
+
+
+	def test_sentence_word_returns_tokenized_word
+		rval = nil
+		
+		assert_nothing_raised do
+			rval = @sentence.word( 0 )
+		end
+		
+		assert_equal "LEFT-WALL", rval
+	end
+	
+	def test_sentence_bracketbracket_returns_tokenized_word
+		rval = nil
+		
+		assert_nothing_raised do
+			rval = @sentence[ -1 ]
+		end
+		
+		assert_equal "RIGHT-WALL", rval
+	end
+	
+	def test_sentence_null_count_should_return_count_of_null_links
+		sent = @dict.parse( "This sentence contains a gravel superfluous word.")
+		rval = nil
+		
+		assert_nothing_raised do
+			rval = sent.null_count
+		end
+		
+		rval = 1
+	end
 
 end