Class: QDA::FragmentTable

Inherits:
CodingTable show all
Defined in:
lib/weft/coding.rb

Overview

a FragmentTable holds a collection of fragments. It contains a number of CodeSets of Fragments. Each CodeSet can be retrieved either by document title or by document dbid.

tbl = FragmentTable.new()
f   = Fragment.new('Weft QDA', 'the title', 6, 1)
tbl.add(f)
tbl['the title'] # => QDA::CodeSet[ <Fragment 1 6-14: 'Weft QDA'> ]
tbl[1] # => QDA::CodeSet[ <Fragment 1 6-14: 'Weft QDA'> ]

Instance Method Summary collapse

Methods inherited from CodingTable

#codes?, #join, #join!, #merge, #merge!, #num_of_chars, #num_of_codes, #num_of_docs, #remove, #remove!, #sort, #subtract

Constructor Details

#initializeFragmentTable

Returns a new instance of FragmentTable.



355
356
357
358
# File 'lib/weft/coding.rb', line 355

def initialize
  @titles = Hash.new() { | h, k | h[k] = CodeSet }
  super()
end

Instance Method Details

#[](k) ⇒ Object

Assumes this is a document title if a string, or an dbid if an integer



361
362
363
# File 'lib/weft/coding.rb', line 361

def [](k)
  k.kind_of?(String) ? super(@titles[k]) : super(k)
end

#add(fragment) ⇒ Object

Always use this method to add fragments to the collection



373
374
375
376
377
378
379
# File 'lib/weft/coding.rb', line 373

def add(fragment)
  unless fragment.is_a?(Fragment)
    raise ArgumentError, "Fragment expected, got #{fragment.inspect}"
  end
  self[fragment.docid].add(fragment)
  @titles[fragment.doctitle] = fragment.docid
end

#each_setObject



394
395
396
# File 'lib/weft/coding.rb', line 394

def each_set
  titles.each { | title | yield self[ @titles[title] ] }
end

#each_titleObject



385
386
387
# File 'lib/weft/coding.rb', line 385

def each_title()
  titles.each { | title | yield title, self[ @titles[title] ] }
end

#set(docid, fragset) ⇒ Object



365
366
367
368
369
370
# File 'lib/weft/coding.rb', line 365

def set(docid, fragset)
  super(docid, fragset)
  if fragset[0] and fragset[0].respond_to?(:doctitle)
    @titles[fragset[0].doctitle] = fragset[0].docid
  end
end

#setsObject



389
390
391
392
# File 'lib/weft/coding.rb', line 389

def sets
  docids = titles.map { | t | @titles[t] }
  values_at( *docids )
end

#titlesObject



381
382
383
# File 'lib/weft/coding.rb', line 381

def titles()
  @titles.keys.sort
end

#to_codingtableObject



398
399
400
401
402
403
404
# File 'lib/weft/coding.rb', line 398

def to_codingtable()
  ct = CodingTable.new 
  each do | docid, codeset |
    ct[docid] = QDA::CodeSet[ *codeset.map { | frag | frag.to_code } ]
  end
  return ct
end