Class: LangExtract::Core::CharInterval

Inherits:
Object
  • Object
show all
Defined in:
lib/langextract/core/data.rb

Direct Known Subclasses

TokenInterval

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(start_pos:, end_pos:) ⇒ CharInterval

Returns a new instance of CharInterval.

Raises:

  • (ArgumentError)


41
42
43
44
45
46
47
48
# File 'lib/langextract/core/data.rb', line 41

def initialize(start_pos:, end_pos:)
  raise ArgumentError, "start_pos must be non-negative" if start_pos.negative?
  raise ArgumentError, "end_pos must be >= start_pos" if end_pos < start_pos

  @start_pos = start_pos
  @end_pos = end_pos
  freeze
end

Instance Attribute Details

#end_posObject (readonly)

Returns the value of attribute end_pos.



39
40
41
# File 'lib/langextract/core/data.rb', line 39

def end_pos
  @end_pos
end

#start_posObject (readonly)

Returns the value of attribute start_pos.



39
40
41
# File 'lib/langextract/core/data.rb', line 39

def start_pos
  @start_pos
end

Class Method Details

.from_h(hash) ⇒ Object

Raises:

  • (ArgumentError)


70
71
72
73
74
75
76
77
78
79
80
# File 'lib/langextract/core/data.rb', line 70

def self.from_h(hash)
  start_pos = HashCoercion.read(hash, :start_pos)
  end_pos = HashCoercion.read(hash, :end_pos)
  raise ArgumentError, "start_pos is required" if start_pos.nil?
  raise ArgumentError, "end_pos is required" if end_pos.nil?

  new(
    start_pos: start_pos.to_i,
    end_pos: end_pos.to_i
  )
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



82
83
84
# File 'lib/langextract/core/data.rb', line 82

def ==(other)
  other.is_a?(self.class) && start_pos == other.start_pos && end_pos == other.end_pos
end

#contains?(other) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/langextract/core/data.rb', line 58

def contains?(other)
  start_pos <= other.start_pos && end_pos >= other.end_pos
end

#hashObject



87
88
89
# File 'lib/langextract/core/data.rb', line 87

def hash
  [start_pos, end_pos].hash
end

#lengthObject



50
51
52
# File 'lib/langextract/core/data.rb', line 50

def length
  end_pos - start_pos
end

#overlaps?(other) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/langextract/core/data.rb', line 54

def overlaps?(other)
  start_pos < other.end_pos && other.start_pos < end_pos
end

#shift(offset) ⇒ Object



62
63
64
# File 'lib/langextract/core/data.rb', line 62

def shift(offset)
  self.class.new(start_pos: start_pos + offset, end_pos: end_pos + offset)
end

#to_hObject



66
67
68
# File 'lib/langextract/core/data.rb', line 66

def to_h
  { "start_pos" => start_pos, "end_pos" => end_pos }
end

#to_sObject



91
92
93
# File 'lib/langextract/core/data.rb', line 91

def to_s
  "[#{start_pos}, #{end_pos})"
end