Sha256: 480ee352ce93269f732c3c9457e4f7f185e1523ec8af19db05e860c8f8ed1c70

Contents?: true

Size: 1.65 KB

Versions: 1

Compression:

Stored size: 1.65 KB

Contents

# encoding: utf-8
require "jldrill/model/items/edict/GrammarType"

module JLDrill

    # Holds a definition for an Edict entry.  Each definition
    # may have one or more type indicating its type of speach
    # (verb, noun, etc)
    class Definition

        DEFINITION_RE = /^(\(\S*\))\s?(.*)/
        SEPARATOR_RE = /\)|,/
        COMMA_RE = /[,]/

        attr_reader :types, :value
        attr_writer :types, :value
        	
        def initialize()
            @value = ""
            @types = []
        end
        
        def Definition.create(string)
            definition = Definition.new
            definition.parse(string)
            definition
        end

        def parse(string)
            @types = []
            while string =~ DEFINITION_RE
        	    types = GrammarType.create($1)
        	    if(types.empty?)
        	        # There were no types in the parenthesis, so it must
        	        # be part of the definition.
        	        @value += $1 + " "
        	    else
        	        @types += types
        	    end
                string = $2
            end
            @value += string.gsub(COMMA_RE, "\\,")
        end
        
        def eql?(definition)
            @value.eql?(definition.value) && (@types.size == definition.types.size) &&
                @types.all? do |x|
                    definition.types.find do |y|
                        x.eql?(y)
                    end
                end
        end        

        def to_s
          	retVal = ""
          	if @types.size > 0
          	    retVal += "(" + @types.join(",") + ")"
          	end
          	retVal += @value
          	retVal
        end
    end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
jldrill-0.6.0.1 lib/jldrill/model/items/edict/Definition.rb