Class: AnyStyle::Document

Inherits:
Wapiti::Sequence
  • Object
show all
Extended by:
PdfUtils
Includes:
StringUtils
Defined in:
lib/anystyle/document.rb

Defined Under Namespace

Classes: Page

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from PdfUtils

pdf_info, pdf_meta, pdf_to_text

Methods included from StringUtils

canonize, count, display_chars, display_width, indent, page_break?, scrub, transliterate

Instance Attribute Details

#infoObject

Returns the value of attribute info.



47
48
49
# File 'lib/anystyle/document.rb', line 47

def info
  @info
end

#metaObject

Returns the value of attribute meta.



47
48
49
# File 'lib/anystyle/document.rb', line 47

def meta
  @meta
end

#pagesObject

Returns the value of attribute pages.



47
48
49
# File 'lib/anystyle/document.rb', line 47

def pages
  @pages
end

#pathObject

Returns the value of attribute path.



47
48
49
# File 'lib/anystyle/document.rb', line 47

def path
  @path
end

#tokensObject Also known as: lines

Returns the value of attribute tokens.



47
48
49
# File 'lib/anystyle/document.rb', line 47

def tokens
  @tokens
end

Class Method Details

.open(path, format: File.extname(path), tagged: false, layout: true, **opts) ⇒ Object

Raises:

  • (ArgumentError)


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
# File 'lib/anystyle/document.rb', line 17

def open(path, format: File.extname(path), tagged: false, layout: true, **opts)
  raise ArgumentError,
    "cannot open tainted path: '#{path}'" if path.tainted?
  raise ArgumentError,
    "document not found: '#{path}'" unless File.exist?(path)

  path = File.absolute_path(path)

  case format.downcase
  when '.pdf'
    meta = pdf_meta path if opts[:parse_meta]
    info = pdf_info path if opts[:parse_info]
    input = pdf_to_text path, layout: layout
  when '.ttx'
    tagged = true
    input = File.read(path, encoding: 'utf-8')
  when '.txt'
    input = File.read(path, encoding: 'utf-8')
  end

  doc = parse input, tagged: tagged
  doc.path = path
  doc.meta = meta
  doc.info = info
  doc
end

.parse(string, delimiter: /\r?\n/, tagged: false) ⇒ Object



6
7
8
9
10
11
12
13
14
15
# File 'lib/anystyle/document.rb', line 6

def parse(string, delimiter: /\r?\n/, tagged: false)
  current_label = ''
  new(string.split(delimiter).map { |line|
    if tagged
      label, line = line.split(/\s*\| /, 2)
      current_label = label unless label.empty?
    end
    Wapiti::Token.new line, label: current_label.to_s
  })
end

Instance Method Details

#eachObject



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/anystyle/document.rb', line 54

def each
  if block_given?
    pages.each.with_index do |page, pn|
      page.lines.each.with_index do |line, ln|
        yield line, ln, page, pn
      end
    end
    self
  else
    to_enum
  end
end

#each_sectionObject



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/anystyle/document.rb', line 67

def each_section
  if block_given?
    current = []
    lines.each do |ln|
      case ln.label
      when 'title'
        unless current.empty?
          yield current
          current = []
        end
      when 'ref', 'text'
        current << ln
      else
        # ignore
      end
    end
    unless current.empty?
      yield current
    end
    self
  else
    to_enum
  end
end

#inspectObject



214
215
216
# File 'lib/anystyle/document.rb', line 214

def inspect
  "#<AnyStyle::Document lines={#{size}}>"
end

#join_refs(a, b) ⇒ Object



190
191
192
193
194
195
196
197
198
199
200
# File 'lib/anystyle/document.rb', line 190

def join_refs(a, b)
  if a[-1] == '-'
    if b =~ /^\p{Ll}/
      "#{a[0...-1]}#{b}"
    else
      "#{a}#{b}"
    end
  else
    "#{a} #{b}"
  end
end

#join_refs?(a, b, delta = 0, indent = 0) ⇒ Boolean

Returns:

  • (Boolean)


168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
# File 'lib/anystyle/document.rb', line 168

def join_refs?(a, b, delta = 0, indent = 0)
  pro = [
    indent > 0,
    delta == 0,
    b.length < 50,
    a.length < 65,
    a.match?(/[,;:&\p{Pd}]$/),
    b.match?(/^\p{Ll}/) || a.match?(/\p{L}$/) && b.match?(/^\p{L}/)
  ].count(true)

  con = [
    indent < 0,
    delta > 8,
    a.match?(/\.\]$/),
    a.length > 500,
    (b.length - a.length) > 12,
    b.match?(/^(\p{Pd}\p{Pd}|\p{Lu}\p{Ll}+, \p{Lu}\.|\[\d)/)
  ].count(true)

  (pro - con) > 1
end

#label(other) ⇒ Object



92
93
94
95
96
97
98
99
100
# File 'lib/anystyle/document.rb', line 92

def label(other)
  doc = dup
  doc.tokens = lines.map.with_index { |line, idx|
    Wapiti::Token.new line.value,
      label: other[idx].label.to_s,
      observations: other[idx].observations.dup
  }
  doc
end

#references(**opts) ⇒ Object



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
# File 'lib/anystyle/document.rb', line 129

def references(**opts)
  bib, current, delta, indent = [], nil, 0, 0

  lines.each do |ln|
    case ln.label
    when 'ref'
      val = display_chars(ln.value).rstrip
      idt = val[/^\s*/].length
      val.lstrip!

      if current.nil?
        current, delta, indent = val, 0, idt
      else
        if join_refs?(current, val, delta, idt - indent)
          current = join_refs(current, val)
        else
          bib << current
          current, delta, indent = val, 0, idt
        end
      end
    else
      unless current.nil?
        if delta > 15 || %w{ blank meta }.include?(ln.label)
          delta += 1
        else
          bib << current
          current, delta, indent = nil, 0, idt
        end
      end
    end
  end

  unless current.nil?
    bib << current
  end

  bib
end

#sections(delimiter: "\n", **opts) ⇒ Object



202
203
204
# File 'lib/anystyle/document.rb', line 202

def sections(delimiter: "\n", **opts)
  []
end

#title(delimiter: " ", **opts) ⇒ Object



206
207
208
209
210
211
212
# File 'lib/anystyle/document.rb', line 206

def title(delimiter: " ", **opts)
  lines.drop_while { |ln|
    ln.label != 'title'
  }.take_while { |ln|
    ln.label == 'title'
  }.map(&:value).join(delimiter)
end

#to_a(encode: true, **opts) ⇒ Object



115
116
117
# File 'lib/anystyle/document.rb', line 115

def to_a(encode: true, **opts)
  super(encode: encode, **opts)
end

#to_h(**opts) ⇒ Object



119
120
121
122
123
124
125
126
127
# File 'lib/anystyle/document.rb', line 119

def to_h(**opts)
  {
    info: info,
    meta: meta,
    sections: sections(**opts),
    title: title(**opts),
    references: references(**opts)
  }
end

#to_s(delimiter: "\n", encode: false, tagged: false, **opts) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/anystyle/document.rb', line 102

def to_s(delimiter: "\n", encode: false, tagged: false, **opts)
  if tagged
    prev_label = nil
    lines.map { |ln|
      label = (ln.label == prev_label) ? '' : ln.label
      prev_label = ln.label
      '%.14s| %s' % ["#{label}              ", ln.value]
    }.join(delimiter)
  else
    super(delimiter: delimiter, encode: encode, tagged: tagged, expanded: false, **opts)
  end
end