Class: QDA::Fragment

Inherits:
String
  • Object
show all
Includes:
Coding
Defined in:
lib/weft/document.rb

Direct Known Subclasses

Document

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Coding

#<=>, #end, #exclude, #include?, #intersect, #overlap?, #prepare_args, #touch?, #union

Constructor Details

#initialize(text, doctitle, offset, docid = nil) ⇒ Fragment

Returns a new instance of Fragment.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/weft/document.rb', line 9

def initialize(text, doctitle, offset, docid = nil)
  super(text)
  unless doctitle.kind_of? String
    raise ArgumentError, 
    "Fragment.new expects a doctitle string, got #{doctitle.inspect}"
  end

  unless offset.kind_of?(Fixnum) && offset >= 0
    raise ArgumentError, 
    "Fragment.new expects an integer offset, got #{offset.inspect}"
  end

  unless docid.nil? || docid.kind_of?(Fixnum)
    raise ArgumentError, 
    "Fragment.new expects an integer docid, got #{docid.inspect}"
  end
  @doctitle = doctitle
  @offset   = offset
  # of the document - duplicates role of doctitle - to fix
  @docid    = docid
end

Instance Attribute Details

#docidObject

Returns the value of attribute docid.



7
8
9
# File 'lib/weft/document.rb', line 7

def docid
  @docid
end

#doctitleObject (readonly)

Returns the value of attribute doctitle.



6
7
8
# File 'lib/weft/document.rb', line 6

def doctitle
  @doctitle
end

#offsetObject (readonly)

Returns the value of attribute offset.



6
7
8
# File 'lib/weft/document.rb', line 6

def offset
  @offset
end

Instance Method Details

#==(other) ⇒ Object



39
40
41
42
43
# File 'lib/weft/document.rb', line 39

def ==(other)
  super(other) and
    @offset == other.offset and
    @doctitle == other.doctitle
end

#[](abs, length) ⇒ Object

returns a fragment from abs (relative to the whole document) that is length long



64
65
66
67
68
69
70
# File 'lib/weft/document.rb', line 64

def [](abs, length)
  if abs < self.offset
    raise "Can't get part of non-overlapping string"
  end
  Fragment.new( super(abs - self.offset, length),
                @doctitle, abs, @docid )
end

#coerce(other) ⇒ Object



49
50
51
# File 'lib/weft/document.rb', line 49

def coerce(other)
  self.to_code()
end

#complete?Boolean

does this code completely cover the document

Returns:

  • (Boolean)


54
55
56
57
58
59
60
# File 'lib/weft/document.rb', line 54

def complete?()
  return NotImplementedError # need to fix
  if @doc.fragments.length == @length + 1
    return true
  end
  return false
end

#inspectObject



79
80
81
82
# File 'lib/weft/document.rb', line 79

def inspect()
  str = length < 50 ? self.to_s : self.to_s[0, 50] << '...'
  "<*Fragment #{docid} #{offset}-#{self.end} : '#{str}>"
end

#scan(pattern) ⇒ Object



72
73
74
75
76
77
# File 'lib/weft/document.rb', line 72

def scan(pattern)
  super do | m |
    yield Fragment.new(m, @doctitle, 
                       offset + Regexp.last_match.begin(0), @dbid )
  end
end

#textObject



35
36
37
# File 'lib/weft/document.rb', line 35

def text
  self.to_s()
end

#titleObject



31
32
33
# File 'lib/weft/document.rb', line 31

def title
  @doctitle
end

#to_codeObject



45
46
47
# File 'lib/weft/document.rb', line 45

def to_code()
  Code.new(@docid, offset, length)
end