Sentence

class
Superclass
rb_cObject
Extended With
Loggability
LinkParser::DeprecationUtilities

A Sentence is the API's representation of an input string, tokenized and interpreted according to a specific Dictionary. After a Sentence is created and parsed, various attributes of the resulting set of linkages can be obtained.

A Sentence is the API's representation of an input string, tokenized and interpreted according to a specific Dictionary. After a Sentence is created and parsed, various attributes of the resulting set of linkages can be obtained.

Public Class Methods

anchor
LinkParser::Sentence.new( str, dict ) → sentence
Create a new LinkParser::Sentence object from the given input string

# using the specified LinkParser::Dictionary.

dict = LinkParser::Dictionary.new
LinkParser::Sentence.new( "The boy runs", dict )  #=> #<LinkParser::Sentence:0x5481ac>
static VALUE
rlink_sentence_init( VALUE self, VALUE input_string, VALUE dictionary )
{
        if ( !check_sentence(self) ) {
                struct rlink_sentence *ptr;
                Sentence sent;
                struct rlink_dictionary *dictptr = rlink_get_dict( dictionary );

                if ( !(sent = sentence_create( StringValueCStr(input_string), dictptr->dict )) )
                        rlink_raise_lp_error();

                DATA_PTR( self ) = ptr = rlink_sentence_alloc();

                ptr->sentence = sent;
                ptr->dictionary = dictionary;
                ptr->options = Qnil;

        } else {
                rb_raise( rb_eRuntimeError,
                                  "Cannot re-initialize a sentence once it's been created." );
        }

        return self;
}

Public Instance Methods

anchor
disjunct_cost( i ) → fixnum

The maximum cost of connectors used in the i-th linkage of the sentence.

static VALUE
rlink_sentence_disjunct_cost( VALUE self, VALUE i )
{
        struct rlink_sentence *ptr = get_sentence( self );
        int count;

        if ( !RTEST(ptr->parsed_p) )
                rlink_sentence_parse( 0, 0, self );

        count = sentence_disjunct_cost( (Sentence)ptr->sentence, FIX2INT(i) );
        return INT2FIX( count );
}
anchor
inspect()

Return a human-readable representation of the Sentence object.

# File lib/linkparser/sentence.rb, line 23
def inspect
        contents = ''
        if self.parsed?
                contents = %Q{"%s"/%d linkages/%d nulls} % [
                        self.to_s,
                        self.num_linkages_found,
                        self.null_count,
                ]
        else
                contents = "(unparsed)"
        end

        return "#<%s:%#x %s>" % [
                self.class.name,
                self.object_id / 2,
                contents,
        ]
end
anchor
length → fixnum

Returns the number of words in the tokenized sentence, including the boundary words and punctuation.

static VALUE
rlink_sentence_length( VALUE self )
{
        struct rlink_sentence *ptr = get_sentence( self );

        if ( !RTEST(ptr->parsed_p) )
                rlink_sentence_parse( 0, 0, self );

        return INT2FIX( sentence_length((Sentence)ptr->sentence) );
}
anchor
linkages → array

Returns an Array of LinkParser::Linkage objects which represent the parts parsed from the sentence for the current linkage.

static VALUE
rlink_sentence_linkages( VALUE self )
{
        struct rlink_sentence *ptr = get_sentence( self );
        int i, count = 0;
        VALUE rary;

        if ( !RTEST(ptr->parsed_p) )
                rlink_sentence_parse( 0, 0, self );

        count = sentence_num_valid_linkages( (Sentence)ptr->sentence );
        rary = rb_ary_new2( count );

        for ( i = 0; i < count; i++ ) {
                VALUE linkage;
                VALUE args[2];

                args[0] = INT2FIX( i );
                args[1] = self;

                linkage = rb_class_new_instance( 2, args, rlink_cLinkage );
                rb_ary_store( rary, i, linkage );
        }

        return rary;
}
anchor
null_count → int

Returns the number of null links that were used in parsing the sentence.

static VALUE
rlink_sentence_null_count( VALUE self )
{
        struct rlink_sentence *ptr = get_sentence( self );
        int count;

        if ( !RTEST(ptr->parsed_p) )
                rlink_sentence_parse( 0, 0, self );

        count = sentence_null_count( (Sentence)ptr->sentence );
        return INT2FIX( count );
}
anchor
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( VALUE self )
{
        struct rlink_sentence *ptr = get_sentence( self );
        int i = 0;

        if ( !RTEST(ptr->parsed_p) )
                rlink_sentence_parse( 0, 0, self );

        i = sentence_num_linkages_found( (Sentence)ptr->sentence );

        return INT2FIX( i );
}
anchor
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( VALUE self )
{
        struct rlink_sentence *ptr = get_sentence( self );
        int count;

        if ( !RTEST(ptr->parsed_p) )
                rlink_sentence_parse( 0, 0, self );

        count = sentence_num_linkages_post_processed( (Sentence)ptr->sentence );
        return INT2FIX( count );
}
anchor
num_valid_linkages → fixnum

Return the number of linkages that had no post-processing violations.

static VALUE
rlink_sentence_num_valid_linkages( VALUE self )
{
        struct rlink_sentence *ptr = get_sentence( self );
        int count;

        if ( !RTEST(ptr->parsed_p) )
                rlink_sentence_parse( 0, 0, self );

        count = sentence_num_valid_linkages( (Sentence)ptr->sentence );
        return INT2FIX( count );
}
anchor
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( VALUE self, VALUE i )
{
        struct rlink_sentence *ptr = get_sentence( self );
        int count;

        if ( !RTEST(ptr->parsed_p) )
                rlink_sentence_parse( 0, 0, self );

        count = sentence_num_violations( (Sentence)ptr->sentence, FIX2INT(i) );
        return INT2FIX( count );
}
anchor
options → parseoptions

Returns a ParseOptions object for the receiving sentence.

sentence.options.verbosity = 3
sentence.options.islands_ok?  # -> true
static VALUE
rlink_sentence_options( VALUE self )
{
        struct rlink_sentence *ptr = get_sentence( self );
        return ptr->options;
}
anchor
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 sentence's dictionary.

static VALUE
rlink_sentence_parse( int argc, VALUE *argv, VALUE self )
{
        struct rlink_sentence *ptr = get_sentence( self );
        Parse_Options opts;
        VALUE defopts = Qnil;
        VALUE options = Qnil;
        int link_count = 0;

        /*
        if ( RTEST(ptr->parsed_p) )
                rb_raise( rlink_eLpError, "Can't reparse a sentence." );
        */
        rlink_log_obj( self, "debug", "Parsing sentence <%p>", ptr  );

        /* Merge the hash from this call with the one from the dict and build
           Parse_Options from it. */
        rb_scan_args( argc, argv, "01", &options );
        defopts = rb_funcall( ptr->dictionary, rb_intern("options"), 0 );

        /* Turn the option hash into a ParseOptions object, then extract the
           Parse_Options struct from that  */
        options = rlink_make_parse_options( defopts, options );
        opts = rlink_get_parseopts( options );

        /* Parse the sentence */
        if ( (link_count = sentence_parse( ptr->sentence, opts )) < 0 )
                rlink_raise_lp_error();

        ptr->options = options;
        ptr->parsed_p = Qtrue;

        return INT2FIX( link_count );
}
anchor
parsed? → true or false

Returns true if the sentence has been parsed.

sentence.parsed?   #-> false
sentence.parse     #-> 6
sentence.parsed?   #-> true
static VALUE
rlink_sentence_parsed_p( VALUE self )
{
        struct rlink_sentence *ptr = get_sentence( self );
        return ptr->parsed_p;
}
anchor
to_s()

Print out the sentence

# File lib/linkparser/sentence.rb, line 44
def to_s
        return self.words.join(" ")
end

Protected Instance Methods

anchor
method_missing( sym, *args, &block )

Proxy method – auto-delegate calls to the first linkage.

# File lib/linkparser/sentence.rb, line 54
def method_missing( sym, *args, &block )
        return super unless LinkParser::Linkage.instance_methods.include?( sym )

        linkage_method = LinkParser::Linkage.instance_method( sym )
        meth = lambda do |*args, &block|
                linkage = self.linkages.first or raise LinkParser::Error, "sentence has no linkages"
                linkage_method.bind( linkage ).call( *args, &block )
        end

        self.singleton_class.instance_exec( sym, meth ) do |name, new_method|
                define_method( name, &new_method )
        end

        meth.call( *args, &block )
rescue => err
        raise err, err.message, err.backtrace[ 0..-2 ]
end