Class: Boilerpipe::Filters::BlockProximityFusion

Inherits:
Object
  • Object
show all
Defined in:
lib/boilerpipe/filters/block_proximity_fusion.rb

Constant Summary collapse

MAX_DISTANCE_1 =
BlockProximityFusion.new(1, false, false)
MAX_DISTANCE_1_SAME_TAGLEVEL =
BlockProximityFusion.new(1, false, true)
MAX_DISTANCE_1_CONTENT_ONLY =
BlockProximityFusion.new(1, true, false)
MAX_DISTANCE_1_CONTENT_ONLY_SAME_TAGLEVEL =
BlockProximityFusion.new(1, true, true)

Instance Method Summary collapse

Constructor Details

#initialize(max_blocks_distance, content_only, same_tag_level_only) ⇒ BlockProximityFusion

Returns a new instance of BlockProximityFusion.



6
7
8
9
10
# File 'lib/boilerpipe/filters/block_proximity_fusion.rb', line 6

def initialize(max_blocks_distance, content_only, same_tag_level_only)
  @max_blocks_distance = max_blocks_distance
  @content_only = content_only
  @same_tag_level_only = same_tag_level_only
end

Instance Method Details

#process(doc) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/boilerpipe/filters/block_proximity_fusion.rb', line 17

def process(doc)
  text_blocks = doc.text_blocks
  return false if text_blocks.size < 2

  prev_block = if @content_only
                 text_blocks.find { |tb| tb.is_content? }
               else
                 text_blocks.first
               end

  return false if prev_block.nil?

  offset = text_blocks.index(prev_block) + 1
  blocks = text_blocks[offset..-1]

  blocks_to_remove = []

  blocks.each do |tb|
    if tb.is_not_content?
      prev_block = tb
      next
    end

    diff_blocks = tb.offset_blocks_start - prev_block.offset_blocks_end - 1
    if diff_blocks <= @max_blocks_distance
      ok = true
      ok = false if (prev_block.is_not_content? || tb.is_not_content?) && @content_only
      ok = false if ok && prev_block.tag_level != tb.tag_level && @same_tag_level_only

      if ok
        prev_block.merge_next(tb)
        blocks_to_remove << tb
      else
        prev_block = tb
      end
    end
  end
  doc.replace_text_blocks!(text_blocks - blocks_to_remove)
  doc
end