Class: Boilerpipe::SAX::HTMLContentHandler

Inherits:
Nokogiri::XML::SAX::Document
  • Object
show all
Defined in:
lib/boilerpipe/sax/html_content_handler.rb

Constant Summary collapse

ANCHOR_TEXT_START =
"$\ue00a<"
ANCHOR_TEXT_END =
">\ue00a$"
VALID_WORD_CHARACTER =
/[\p{L}\p{Nd}\p{Nl}\p{No}]/

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeHTMLContentHandler

Returns a new instance of HTMLContentHandler.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/boilerpipe/sax/html_content_handler.rb', line 12

def initialize
  @label_stacks = []
  @tag_actions = ::Boilerpipe::SAX::TagActionMap.tag_actions
  @tag_level = 0
  @sb_last_was_whitespace = false
  @text_buffer = ''
  @token_buffer = ''
  @offset_blocks = 0
  @flush = false
  @block_tag_level = -1

  @in_body = 0
  @in_anchor_tag = 0
  @in_ignorable_element = 0
  @in_anchor_text = false
  @font_size_stack = []
  @last_start_tag = ''
  @title
  @text_blocks = []
end

Instance Attribute Details

#font_size_stackObject

Returns the value of attribute font_size_stack.



8
9
10
# File 'lib/boilerpipe/sax/html_content_handler.rb', line 8

def font_size_stack
  @font_size_stack
end

#in_anchor_tagObject

Returns the value of attribute in_anchor_tag.



8
9
10
# File 'lib/boilerpipe/sax/html_content_handler.rb', line 8

def in_anchor_tag
  @in_anchor_tag
end

#in_ignorable_elementObject (readonly)

Returns the value of attribute in_ignorable_element.



6
7
8
# File 'lib/boilerpipe/sax/html_content_handler.rb', line 6

def in_ignorable_element
  @in_ignorable_element
end

#label_stacksObject (readonly)

Returns the value of attribute label_stacks.



6
7
8
# File 'lib/boilerpipe/sax/html_content_handler.rb', line 6

def label_stacks
  @label_stacks
end

#last_start_tagObject (readonly)

Returns the value of attribute last_start_tag.



6
7
8
# File 'lib/boilerpipe/sax/html_content_handler.rb', line 6

def last_start_tag
  @last_start_tag
end

#token_bufferObject

Returns the value of attribute token_buffer.



8
9
10
# File 'lib/boilerpipe/sax/html_content_handler.rb', line 8

def token_buffer
  @token_buffer
end

Instance Method Details

#add_label_action(label_action) ⇒ Object



258
259
260
261
262
263
264
265
266
# File 'lib/boilerpipe/sax/html_content_handler.rb', line 258

def add_label_action(label_action)
  label_stack = @label_stacks.last
  if label_stack.nil?
    label_stack = []
    @label_stacks.pop
    @label_stacks << label_stack
  end
  label_stack << label_action
end

#add_text_block(text_block) ⇒ Object



228
229
230
231
232
233
234
235
236
237
# File 'lib/boilerpipe/sax/html_content_handler.rb', line 228

def add_text_block(text_block)
  @label_stacks.each do |stack|
    next unless stack

    stack.each do |label_action|
      text_block.add_label(label_action.labels) if label_action
    end
  end
  @text_blocks << text_block
end

#append_spaceObject

append space if last character wasn’t already one



240
241
242
243
244
245
246
# File 'lib/boilerpipe/sax/html_content_handler.rb', line 240

def append_space
  return if @sb_last_was_whitespace
  @sb_last_was_whitespace = true

  @text_buffer << ' '
  @token_buffer << ' '
end

#append_text(text) ⇒ Object



248
249
250
251
252
# File 'lib/boilerpipe/sax/html_content_handler.rb', line 248

def append_text(text)
  @sb_last_was_whitespace = false
  @text_buffer << text
  @token_buffer << text
end

#append_token(token) ⇒ Object



254
255
256
# File 'lib/boilerpipe/sax/html_content_handler.rb', line 254

def append_token(token)
  @token_buffer << token
end

#characters(text) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/boilerpipe/sax/html_content_handler.rb', line 51

def characters(text)
  flush_block if @flush

  return if @in_ignorable_element != 0
  return if text.empty?

  # replace all whitespace with simple space
  text.gsub!(/\s+/, ' ')

  # trim whitespace
  started_with_whitespace = text  =~ /^\s/
  ended_with_whitespace = text  =~ /\s$/
  text.strip!

  #  add a single space if the block was only whitespace
  if text.empty?
    append_space
    @last_event = :WHITESPACE
    return
  end

  # set block levels
  @block_tag_level = @tag_level if @block_tag_level == -1

  append_space if started_with_whitespace
  append_text(text)
  append_space if ended_with_whitespace

  @last_event = :CHARACTERS
end

#decrease_in_body!Object



215
216
217
# File 'lib/boilerpipe/sax/html_content_handler.rb', line 215

def decrease_in_body!
  @in_body -= 1
end

#decrease_in_ignorable_element!Object



207
208
209
# File 'lib/boilerpipe/sax/html_content_handler.rb', line 207

def decrease_in_ignorable_element!
  @in_ignorable_element -= 1
end

#end_element(name) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/boilerpipe/sax/html_content_handler.rb', line 82

def end_element(name)
  tag = name.upcase.intern
  tag_action = @tag_actions[tag]
  if tag_action
    @flush = tag_action.end_tag(self, name) | @flush
  else
    @flush = true
  end

  @tag_level -= 1 if tag_action.nil? || tag_action.changes_tag_level?
  flush_block if @flush

  @last_event = :END_TAG
  @last_end_tag = tag
  @label_stacks.pop
end

#flush_blockObject



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/boilerpipe/sax/html_content_handler.rb', line 99

def flush_block
  @flush = false
  if @in_body == 0
    @title = @token_buffer.strip if :TITLE == @last_start_tag
    clear_buffers
    return
  end

  # clear out if empty or just a space
  length = @token_buffer.size
  case length
  when 0
    return
  when 1
    clear_buffers if @sb_last_was_whitespace
    return
  end

  num_tokens = 0
  num_words = 0
  num_words_current_line = 0
  num_words_in_wrapped_lines = 0
  num_wrapped_lines = 0
  num_linked_words = 0
  current_line_length = 0
  max_line_length = 80

  tokens = ::Boilerpipe::UnicodeTokenizer.tokenize(@token_buffer)
  tokens.each do |token|
    if ANCHOR_TEXT_START == token
      @in_anchor_text = true
    elsif ANCHOR_TEXT_END == token
      @in_anchor_text = false
    elsif is_word?(token)
      num_tokens += 1
      num_words += 1
      num_words_current_line += 1
      num_linked_words += 1 if @in_anchor_text
      token_length = token.size
      current_line_length += token_length + 1

      if current_line_length > max_line_length
        num_wrapped_lines += 1
        current_line_length = token_length
        num_words_current_line = 1
      end
    else
      num_tokens += 1
    end
  end

  return if num_tokens == 0

  num_words_in_wrapped_lines = 0
  if num_wrapped_lines == 0
    num_words_in_wrapped_lines = num_words
    num_wrapped_lines = 1
  else
    num_words_in_wrapped_lines = num_words - num_words_current_line
  end

  text_block = ::Boilerpipe::Document::TextBlock.new(@text_buffer.strip,
                             num_words,
                             num_linked_words,
                             num_words_in_wrapped_lines,
                             num_wrapped_lines, @offset_blocks)

  @offset_blocks += 1
  clear_buffers
  text_block.set_tag_level(@block_tag_level)
  add_text_block(text_block)
  @block_tag_level = -1
end

#in_anchor_tag?Boolean

Returns:

  • (Boolean)


223
224
225
# File 'lib/boilerpipe/sax/html_content_handler.rb', line 223

def in_anchor_tag?
  @in_anchor_tag > 0
end

#in_ignorable_element?Boolean

Returns:

  • (Boolean)


219
220
221
# File 'lib/boilerpipe/sax/html_content_handler.rb', line 219

def in_ignorable_element?
  @in_ignorable_element > 0
end

#increase_in_body!Object



211
212
213
# File 'lib/boilerpipe/sax/html_content_handler.rb', line 211

def increase_in_body!
  @in_body += 1
end

#increase_in_ignorable_element!Object

public void flushBlock()

int numWords = 0;
int numLinkedWords = 0;
int numWrappedLines = 0;
int currentLineLength = -1; // don't count the first space
final int maxLineLength = 80;
int numTokens = 0;
int numWordsCurrentLine = 0;



203
204
205
# File 'lib/boilerpipe/sax/html_content_handler.rb', line 203

def increase_in_ignorable_element!
  @in_ignorable_element += 1
end

#is_word?(word) ⇒ Boolean

unicode regex - categories pL – Letter pNd – a decimal digit pNl – a letterlike numeric character pNo – a numeric character of other type

Returns:

  • (Boolean)


189
190
191
# File 'lib/boilerpipe/sax/html_content_handler.rb', line 189

def is_word?(word)
   word =~ VALID_WORD_CHARACTER
end

#start_element(name, attrs = []) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/boilerpipe/sax/html_content_handler.rb', line 33

def start_element(name, attrs = [])
  @label_stacks << nil
  tag = name.upcase.intern


  tag_action = @tag_actions[tag]
  if tag_action
    @tag_level += 1 if tag_action.changes_tag_level?
    @flush = tag_action.start(self, name, attrs) | @flush
  else
    @tag_level += 1
    @flush = true
  end

  @last_event = :START_TAG
  @last_start_tag = tag
end

#text_documentObject



173
174
175
176
# File 'lib/boilerpipe/sax/html_content_handler.rb', line 173

def text_document
  flush_block
  ::Boilerpipe::Document::TextDocument.new(@title, @text_blocks)
end

#token_buffer_sizeObject



178
179
180
# File 'lib/boilerpipe/sax/html_content_handler.rb', line 178

def token_buffer_size
  @token_buffer.size
end