Class: ExplanatoryNotesParser

Inherits:
Object
  • Object
show all
Defined in:
app/models/explanatory_notes_parser.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.parse(pdf_file, options = {}) ⇒ Object


8
9
10
# File 'app/models/explanatory_notes_parser.rb', line 8

def parse pdf_file, options={}
  ExplanatoryNotesParser.new.parse pdf_file, options
end

Instance Method Details

#add(text) ⇒ Object


513
514
515
516
517
518
519
# File 'app/models/explanatory_notes_parser.rb', line 513

def add text
  if text.nil?
    raise 'text should not be null'
  else
    @xml << text
  end
end

#add_bill_infoObject


553
554
555
556
557
558
# File 'app/models/explanatory_notes_parser.rb', line 553

def add_bill_info
  unless @doc_started
    add "<BillInfo><Title>#{@bill_title}</Title><Version>#{@bill_version}</Version></BillInfo>"
    @doc_started = true
  end
end

#add_section_start(tag, serial, number = "") ⇒ Object


543
544
545
546
547
548
549
550
551
# File 'app/models/explanatory_notes_parser.rb', line 543

def add_section_start tag, serial, number = ""
  add_bill_info unless @doc_started
  if number == ""
    num_attr = ""
  else
    num_attr = %Q| Number="#{number}"|
  end
  add %Q|<#{tag}#{num_attr} SerialNumber="#{serial}">|
end

#check_for_text_section(line) ⇒ Object


560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
# File 'app/models/explanatory_notes_parser.rb', line 560

def check_for_text_section line
  if (line.strip == line.strip.upcase && line =~ /[a-zA-Z]$/ && 
      line.strip.upcase &&
      line.strip.upcase != "EXPLANATORY NOTES" &&
      line.strip.upcase != "LONDON: THE STATIONERY OFFICE" &&
      line.strip.upcase != @full_bill_title.upcase &&
      @page_number !=1) ||
      (line.strip.upcase == @full_bill_title.upcase && @page_number > 2)
    
    last_line = @xml.pop
    @xml << last_line
    
    if last_line.strip == ""
      if @in_section
        add "</TextSection>"
      end
      if @in_clause
        add "</Clause>"
        @in_clause = false
      end
      if @in_schedule
        add "</Schedule>"
        @in_schedule = false
      end
      if @in_clause_range
        add "</ClauseRange>"
        @in_clause_range = false
      end
      if @in_chapter
        add "</Chapter>"
        @in_chapter = false
      end
      if @in_part
        add "</Part>"
        @in_part = false
      end
      add_section_start('TextSection', @serial_number)
      @serial_number += 1
      @in_section = true
    end
  end
end

#do_cleanupObject


521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
# File 'app/models/explanatory_notes_parser.rb', line 521

def do_cleanup
  if @in_section
    add "</TextSection>"
  end
  if @in_clause
    add "</Clause>"
  end
  if @in_clause_range
    add "</ClauseRange>"
    @in_clause_range = false
  end
  if @in_chapter
    add "</Chapter>"
  end
  if @in_part
    add "</Part>"
  end
  if @in_schedule
    add "</Schedule>"
  end
end

#handle_chapter(number) ⇒ Object


447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
# File 'app/models/explanatory_notes_parser.rb', line 447

def handle_chapter number
  if @in_section
    add "</TextSection>"
    @in_section = false
  end
  if @in_clause
    add "</Clause>"
    @in_clause = false
  end
  if @in_clause_range
    add "</ClauseRange>"
    @in_clause_range = false
  end
  if @in_schedule
    add "</Schedule>"
    @in_schedule = false
  end
  if @in_chapter
    add "</Chapter>"
  end

  if number =~ /([^:]*):*/
    number = $1
  end

  add_section_start('Chapter', @serial_number, number)
  @serial_number += 1
  @in_chapter = true
end

#handle_clause(number) ⇒ Object


346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
# File 'app/models/explanatory_notes_parser.rb', line 346

def handle_clause number
  insert_heading = false
  
  if @in_section
    add "</TextSection>"
    @in_section = false
  end
  if @in_clause
    last_line = @xml.pop
    if is_subheading(last_line)
      insert_heading = true
    else
      @xml << last_line
    end
    add "</Clause>"
  end
  if @in_schedule
    add "</Schedule>"
    @in_schedule = false
  end
  if @in_clause_range
    add "</ClauseRange>"
    @in_clause_range = false
  end
  
  if number =~ /([^:]*):*/
    number = $1
  end

  add_section_start('Clause', @serial_number, number)
  @serial_number += 1
  @xml << last_line if insert_heading
  @in_clause = true
end

#handle_clause_range(first_clause, last_clause) ⇒ Object


381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
# File 'app/models/explanatory_notes_parser.rb', line 381

def handle_clause_range first_clause, last_clause
  insert_heading = false
  
  if @in_section
    add "</TextSection>"
    @in_section = false
  end
  if @in_clause
    last_line = @xml.pop
    if is_subheading(last_line)
      insert_heading = true
    else
      @xml << last_line
    end
    add "</Clause>"
    @in_clause = false
  end
  if @in_clause_range
    add "</ClauseRange>"
    @in_clause_range = false
  end
  
  @in_clause_range = true
  @range_end = last_clause
      
  add (%Q|<ClauseRange start="#{first_clause}" end="#{last_clause}">|)
  
  @xml << last_line if insert_heading
end

#handle_page_footers(line) ⇒ Object


104
105
106
107
108
109
110
111
112
113
114
# File 'app/models/explanatory_notes_parser.rb', line 104

def handle_page_footers line
  if line.strip =~ /^(\d+)$/
    @in_footer = true
  elsif line.strip =~ /Bill-#{@bill_version}.*EN .* (\d*\/\d*)/
    @in_footer = true
  elsif line.strip =~ /HL Bill-#{@bill_version}.*EN .* (\d*\/\d*)/
    @in_footer = true
  else
    @in_footer = false
  end
end

#handle_page_headers(line) ⇒ Object


81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/models/explanatory_notes_parser.rb', line 81

def handle_page_headers line
  if line.strip[0..23] == 'These notes refer to the' || line.strip[0..24] == 'These notes relate to the'
    @in_header = true
    last_line = @xml.pop
    @xml << last_line
    add %Q|<PageStart PageNumber="#{@page_number}"/>| unless last_line =~ /<PageStart PageNumber/
    @page_number += 1
    set_bill_title(line.strip) if @bill_title == ""
  end

  if @in_header
    if line.strip == ""
      @in_header = false
      last_line = @xml.pop
      @xml << last_line unless last_line.strip == ""
      @page_line_count = 0
      @blank_rows_after_header = 0
    else
      set_bill_version(line) if @bill_version == ""
    end
  end
end

#handle_part(number) ⇒ Object


477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
# File 'app/models/explanatory_notes_parser.rb', line 477

def handle_part number
  if @in_schedule
    return
  end
  if @in_section
    add "</TextSection>"
    @in_section = false
  end
  if @in_clause
    add "</Clause>"
    @in_clause = false
  end
  if @in_clause_range
    add "</ClauseRange>"
    @in_clause_range = false
  end
  if @in_chapter
    add "</Chapter>"
    @in_chapter = false
  end
  if @in_part
    add "</Part>"
    @in_part = false
  end
  
  if number =~ /([^:]*):*/
    number = $1
  end
  
  unless @in_schedule
    add_section_start('Part', @serial_number, number)
    @serial_number += 1
    @in_part = true
  end
end

#handle_schedule(number) ⇒ Object


411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
# File 'app/models/explanatory_notes_parser.rb', line 411

def handle_schedule number
  if @in_section
    add "</TextSection>"
    @in_section = false
  end
  
  if @in_clause
    add "</Clause>"
    @in_clause = false
    if @in_chapter
      add "</Chapter>"
      @in_chapter = false
    end
    if @in_part
      add "</Part>"
      @in_part = false
    end
  end
  if @in_clause_range
    add "</ClauseRange>"
    @in_clause_range = false
  end

  if @in_schedule
    add "</Schedule>"
  end
  
  if number =~ /([^:]*):*/
    number = $1
  end

  add_section_start('Schedule', @serial_number, number)
  @serial_number += 1
  @in_schedule = true
end

#handle_toc(line) ⇒ Object


116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'app/models/explanatory_notes_parser.rb', line 116

def handle_toc line
  if line.strip == "TABLE OF CONTENTS"
    @in_toc = true
  end

  if @in_toc && line.strip == ""
    if @blank_rows_after_header < 4
      @blank_rows_after_header += 1
    else
      @in_toc = false
      @was_toc = true
    end
  end
end

#handle_txt_line(line) ⇒ Object


603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
# File 'app/models/explanatory_notes_parser.rb', line 603

def handle_txt_line line
  handle_page_headers(line)
  handle_page_footers(line)
  unless @in_header || @in_footer
    handle_toc(line)
  end

  unless @in_header || @in_toc || @in_footer
    if line.strip == ""
      @blank_row_count += 1
    else
      @blank_row_count = 0
    end
    
    case line.strip
      when /^Clause (.*)/
        handle_clause($1) if is_clause_start(line.strip)
      when /^Schedule (.*)/
        handle_schedule($1) if is_schedule_start(line.strip)
      when /^Part (.*)/
        handle_part($1) if is_part_start(line.strip)
      when /^Chapter (.*)/
        handle_chapter($1) if is_chapter_start(line.strip)
      when /^Clauses (.*) to (.*):/
        handle_clause_range($1, $2)
      when /^Topic (.*):/
        @in_topic = true
    end
    
    check_for_text_section(line)
    
    unless @in_clause || @in_schedule || @in_part || @in_chapter || @in_section || @in_clause_range
      add_section_start('TextSection', @serial_number)
      @serial_number += 1
      @in_section = true
    end

    text = HTMLEntities.new.encode(line, :decimal)
    text = strip_control_chars(text)
    
    add "#{text}\n" unless @blank_row_count > 1
    @page_line_count += 1
  end
end

#initialize_parserObject


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
# File 'app/models/explanatory_notes_parser.rb', line 55

def initialize_parser
  @doc_started = false

  @bill_title = ""
  @full_bill_title = ""
  @bill_version = ""

  @in_section = false
  @in_clause = false
  @in_clause_range = false
  @in_part = false
  @in_chapter = false
  @in_schedule = false
  @in_topic = false

  @in_header = false
  @in_footer = false
  @in_toc = false
  
  @blank_row_count = 0
  @page_line_count = 0
  @blank_rows_after_header = 0
  @serial_number = 1
  @page_number = 1
end

#is_chapter_start(line) ⇒ Object


249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# File 'app/models/explanatory_notes_parser.rb', line 249

def is_chapter_start line
  if line =~ /^Chapter \d+\S*(: .*)?$/
    if @page_line_count == 1
      @xml << "\n \n"
      return true
    end
    last_line = @xml.pop
    if last_line.strip == "" || last_line =~ /<PageStart/
      @xml << last_line
      return true
    end
    if last_line.strip =~ /\<Chapter /
      @xml << last_line
      return true
    end
    if is_part_start(last_line)
      @xml << last_line
      return true
    end
    if is_en_header(last_line)
      @xml << last_line
      return true
    end
    @xml << last_line
  end
  false
end

#is_clause_start(line) ⇒ Object


163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
# File 'app/models/explanatory_notes_parser.rb', line 163

def is_clause_start line
  if line =~ /^Clause \d+\S*\ {0,1}(: .*)?$/
    if @page_line_count == 1
      @xml << "\n \n"
    end
    if @in_schedule && !@in_topic
      return false
    else
      return true
    end
    last_line = @xml.pop
    if last_line.strip == "" || last_line =~ /<PageStart/
      @xml << last_line
      return true
    end
    if is_part_start(last_line)
      @xml << last_line
      return true
    end
    if is_chapter_start(last_line)
      @xml << last_line
      return true
    end
    if is_subheading(last_line)
      @xml << last_line
      return true
    end
    if is_en_header(last_line)
      @xml << last_line
      return true
    end
    @xml << last_line
  end
  if line =~ /^Clause \d+\ {0,1}[^\s\.*]$/
    if @page_line_count == 1
      @xml << "\n \n"
    end
    if @in_schedule && !@in_topic
      return false
    else
      return true
    end
  end
  false
end

#is_en_header(line) ⇒ Object


152
153
154
155
156
157
158
159
160
161
# File 'app/models/explanatory_notes_parser.rb', line 152

def is_en_header line
  if line.strip =~ /^\(.*\)$/ && line.strip == line.strip.upcase
    last_line = @xml.pop
    @xml << last_line
    if last_line.strip =~ /^EXPLANATORY NOTES/
      return true
    end
  end
  false
end

#is_part_start(line) ⇒ Object


277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# File 'app/models/explanatory_notes_parser.rb', line 277

def is_part_start line
  if line =~ /^Part \d+\S*(: .*)?$/
    if @page_line_count == 1
      @xml << "\n \n"
      return true
    end
    last_line = @xml.pop
    if last_line.strip == "" || last_line =~ /<PageStart/
      prev_line = @xml.pop
      @xml << prev_line
      @xml << last_line
      unless prev_line.strip[-1..-1] == ":"
        return true
      end
    end
    if last_line.strip =~ /^<Part/
      prev_line = @xml.pop
      @xml << prev_line
      if prev_line == "" || prev_line == "</Part>"
        @xml << last_line
        return true
      end
    end
    if is_en_header(last_line)
      @xml << last_line
      return true
    end
    @xml << last_line
  end
  false
end

#is_schedule_start(line) ⇒ Object


209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# File 'app/models/explanatory_notes_parser.rb', line 209

def is_schedule_start line
  if line =~ /^Schedule \d+\S*(: .*)?$/
    if @page_line_count == 1
      @xml << "\n \n"
      return true
    end
    last_line = @xml.pop
    if last_line.strip == "" || last_line =~ /<PageStart/
      prev_line = @xml.pop
      @xml << prev_line
      @xml << last_line
      unless prev_line.strip[-1..-1] == ":"
        return true
      end
      if last_line =~ /^Schedule/
        return false
      end
    end
    if is_part_start(last_line)
      @xml << last_line
      return true
    end
    if is_chapter_start(last_line)
      @xml << last_line
      return true
    end
    if is_subheading(last_line)
      @xml << last_line
      return true
    end
    if is_en_header(last_line)
      @xml << last_line
      return true
    end
    @xml << last_line
  end
  false
end

#is_subheading(line) ⇒ Object


309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
# File 'app/models/explanatory_notes_parser.rb', line 309

def is_subheading line
  if line.strip == ""
    return false
  end
  if line.strip =~ /^\d*\./
    return false
  end
  if line.strip =~ /^Part \d+\S*(: .*)?$/
    return false
  end
  if line.strip =~ /^Clause \d+\S*(: .*)?$/
    return false
  end
  if line.strip =~ /^Schedule \d+\S*(: .*)?$/
    return false
  end
  if line.strip =~ /^Chapter \d+\S*(: .*)?$/
    return false
  end
  
  last_line = @xml.pop
  if last_line.strip == ""
    prev_line = @xml.pop
    @xml << prev_line
    @xml << last_line
    unless prev_line.strip[-1..-1] == ":"
      return true
    end
  end
  if is_en_header(last_line)
    @xml << last_line
    return true
  end
  @xml << last_line
  false
end

#make_xml(doc) ⇒ Object


42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/models/explanatory_notes_parser.rb', line 42

def make_xml doc
  initialize_parser()
  en_text = File.open(doc)

  @xml = ['<Document><ENData>']
  en_text.each do |line|
    handle_txt_line(line)
  end
  do_cleanup
  @xml << ['</ENData></Document>']
  @xml.join('')
end

#parse(pdf_file, options = {}) ⇒ Object


13
14
15
16
17
18
19
20
21
22
23
# File 'app/models/explanatory_notes_parser.rb', line 13

def parse pdf_file, options={}
  pdf_txt_file = Tempfile.new("#{pdf_file.gsub('/','_')}.txt", "#{RAILS_ROOT}/tmp")
  pdf_txt_file.close # was open

  Kernel.system %Q|pdftotext -layout -enc UTF-8 "#{pdf_file}" "#{pdf_txt_file.path}"|

  result = parse_txt_file(pdf_txt_file.path, options)
  pdf_txt_file.delete
      
  result
end

#parse_txt_file(doc, options = {}) ⇒ Object


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'app/models/explanatory_notes_parser.rb', line 25

def parse_txt_file doc, options={}
  xml = make_xml(doc)
  begin
    doc = REXML::Document.new(xml)
  rescue Exception => e
    puts e.to_s
    # raise e
  end
  if options[:indent]
    indented = ''
    doc.write(indented,2)
    indented
  else
    xml
  end
end

#set_bill_title(line) ⇒ Object


131
132
133
134
135
136
137
138
139
140
141
142
# File 'app/models/explanatory_notes_parser.rb', line 131

def set_bill_title line
  title = line.gsub('These notes refer to the ', '')
  title = title.gsub('These notes relate to the ', '')
  if title =~ /(.* Bill)/
    @bill_title = $1
  end
  if title =~ /(.* Bill .*\])/
    @full_bill_title = $1
  else
    @full_bill_title = @bill_title
  end
end

#set_bill_version(line) ⇒ Object


144
145
146
147
148
149
150
# File 'app/models/explanatory_notes_parser.rb', line 144

def set_bill_version line
  if line =~ /\[Bill (.*)\]/
    @bill_version = $1
  elsif line =~ /\[HL Bill (.*)\]/
    @bill_version = $1
  end
end