Class: Mathpix::DocumentResult

Inherits:
Result
  • Object
show all
Defined in:
lib/mathpix/document.rb

Overview

Document Result object

Represents processed document with extracted content

Instance Attribute Summary collapse

Attributes inherited from Result

#data, #source_path

Instance Method Summary collapse

Methods inherited from Result

#asciimath, #chart?, #chemistry?, #confidence, #confidence_rate, #created_at, #diagram?, #failure?, #handwritten?, #inchi, #inchikey, #inspect, #latex_simplified, #line_data, #lines, #lines_json, #mathml, #metadata, #molecular_formula, #molecular_name, #molecular_weight, #on_failure, #on_success, #position, #printed?, #processing_time_ms, #request_id, #smiles, #source_url, #stereochemistry?, #success?, #table?, #tags, #text, #timestamp, #to_h, #to_json, #word_data

Constructor Details

#initialize(data, document_path = nil, document_type = nil) ⇒ DocumentResult

Returns a new instance of DocumentResult.



238
239
240
241
242
# File 'lib/mathpix/document.rb', line 238

def initialize(data, document_path = nil, document_type = nil)
  super(data)
  @document_path = document_path
  @document_type = document_type
end

Instance Attribute Details

#document_pathObject (readonly)

Returns the value of attribute document_path.



236
237
238
# File 'lib/mathpix/document.rb', line 236

def document_path
  @document_path
end

#document_typeObject (readonly)

Returns the value of attribute document_type.



236
237
238
# File 'lib/mathpix/document.rb', line 236

def document_type
  @document_type
end

Instance Method Details

#diagramsArray<Hash>

Get all diagrams across all pages

Returns:

  • (Array<Hash>)


276
277
278
# File 'lib/mathpix/document.rb', line 276

def diagrams
  pages.flat_map { |p| p['diagrams'] || [] }
end

#docx?Boolean

Returns:

  • (Boolean)


333
334
335
# File 'lib/mathpix/document.rb', line 333

def docx?
  document_type == :docx
end

#equationsArray<String>

Get all equations across all pages

Returns:

  • (Array<String>)


264
265
266
# File 'lib/mathpix/document.rb', line 264

def equations
  pages.flat_map { |p| p['equations'] || [] }
end

#htmlString?

Get HTML output

Returns:

  • (String, nil)


294
295
296
# File 'lib/mathpix/document.rb', line 294

def html
  data['html']
end

#latexString?

Get LaTeX output

Returns:

  • (String, nil)


288
289
290
# File 'lib/mathpix/document.rb', line 288

def latex
  data['latex']
end

#markdownString?

Get markdown output

Returns:

  • (String, nil)


282
283
284
# File 'lib/mathpix/document.rb', line 282

def markdown
  data['markdown'] || data['mmd']
end

#page_countInteger

Get page count

Returns:

  • (Integer)


252
253
254
# File 'lib/mathpix/document.rb', line 252

def page_count
  pages.length
end

#pagesArray<Hash>

Get all pages

Returns:

  • (Array<Hash>)

    page data



246
247
248
# File 'lib/mathpix/document.rb', line 246

def pages
  data['pages'] || []
end

#pdf?Boolean

Check if document is a specific type

Returns:

  • (Boolean)


329
330
331
# File 'lib/mathpix/document.rb', line 329

def pdf?
  document_type == :pdf
end

#pptx?Boolean

Returns:

  • (Boolean)


337
338
339
# File 'lib/mathpix/document.rb', line 337

def pptx?
  document_type == :pptx
end

#processing_timeNumeric?

Processing time (seconds if reported by the conversion, else nil)

Returns:

  • (Numeric, nil)


258
259
260
# File 'lib/mathpix/document.rb', line 258

def processing_time
  data['total_processing_time'] || data['processing_time'] || processing_time_ms
end

#save_docx(path) ⇒ Object

Save DOCX output to file

Parameters:

  • path (String)

    output file path



318
319
320
321
322
323
324
325
# File 'lib/mathpix/document.rb', line 318

def save_docx(path)
  if data['docx_url']
    docx_data = client.download(data['docx_url'])
    File.binwrite(path, docx_data)
  elsif data['docx_data']
    File.binwrite(path, data['docx_data'])
  end
end

#save_html(path) ⇒ Object

Save HTML to file

Parameters:

  • path (String)

    output file path



312
313
314
# File 'lib/mathpix/document.rb', line 312

def save_html(path)
  File.write(path, html) if html
end

#save_latex(path) ⇒ Object

Save LaTeX to file

Parameters:

  • path (String)

    output file path



306
307
308
# File 'lib/mathpix/document.rb', line 306

def save_latex(path)
  File.write(path, latex) if latex
end

#save_markdown(path) ⇒ Object

Save markdown to file

Parameters:

  • path (String)

    output file path



300
301
302
# File 'lib/mathpix/document.rb', line 300

def save_markdown(path)
  File.write(path, markdown) if markdown
end

#tablesArray<Hash>

Get all tables across all pages

Returns:

  • (Array<Hash>)


270
271
272
# File 'lib/mathpix/document.rb', line 270

def tables
  pages.flat_map { |p| p['tables'] || [] }
end