Class: Mathpix::Result

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

Overview

OCR Result object

Direct Known Subclasses

DocumentResult

Defined Under Namespace

Classes: Line, Word

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, source_path = nil) ⇒ Result

Returns a new instance of Result.



8
9
10
11
# File 'lib/mathpix/result.rb', line 8

def initialize(data, source_path = nil)
  @data = data
  @source_path = source_path
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



6
7
8
# File 'lib/mathpix/result.rb', line 6

def data
  @data
end

#source_pathObject (readonly)

Returns the value of attribute source_path.



6
7
8
# File 'lib/mathpix/result.rb', line 6

def source_path
  @source_path
end

Instance Method Details

#asciimathString?

AsciiMath output

Returns:

  • (String, nil)


50
51
52
# File 'lib/mathpix/result.rb', line 50

def asciimath
  data['asciimath']
end

#chart?Boolean

Contains chart?

Returns:

  • (Boolean)


130
131
132
# File 'lib/mathpix/result.rb', line 130

def chart?
  data['contains_chart'] == true
end

#chemistry?Boolean

Is this a chemistry result?

Returns:

  • (Boolean)


353
354
355
# File 'lib/mathpix/result.rb', line 353

def chemistry?
  molecular_formula || inchi || smiles&.match?(%r{^[A-Za-z0-9@\[\]()=#+\-\\/.]+$})
end

#confidenceFloat

Confidence score

Returns:

  • (Float)

    0.0-1.0



62
63
64
# File 'lib/mathpix/result.rb', line 62

def confidence
  data['confidence'] || 0.0
end

#confidence_rateFloat

Confidence rate (alternative)

Returns:

  • (Float)

    0.0-1.0



68
69
70
# File 'lib/mathpix/result.rb', line 68

def confidence_rate
  data['confidence_rate'] || confidence
end

#created_atTime?

Created timestamp

Returns:

  • (Time, nil)


74
75
76
77
78
# File 'lib/mathpix/result.rb', line 74

def created_at
  Time.parse(data['created']) if data['created']
rescue ArgumentError
  nil
end

#diagram?Boolean

Contains diagram?

Returns:

  • (Boolean)


136
137
138
# File 'lib/mathpix/result.rb', line 136

def diagram?
  data['contains_diagram'] == true
end

#failure?Boolean

Was capture a failure?

Returns:

  • (Boolean)


367
368
369
# File 'lib/mathpix/result.rb', line 367

def failure?
  !success?
end

#handwritten?Boolean

Is content handwritten?

Returns:

  • (Boolean)


124
125
126
# File 'lib/mathpix/result.rb', line 124

def handwritten?
  data['is_handwritten'] == true
end

#htmlString?

HTML output (may contain SVG)

Returns:

  • (String, nil)


56
57
58
# File 'lib/mathpix/result.rb', line 56

def html
  data['html']
end

#inchiString?

InChI notation (chemistry)

Returns:

  • (String, nil)


317
318
319
# File 'lib/mathpix/result.rb', line 317

def inchi
  data['inchi']
end

#inchikeyString?

InChI Key (chemistry)

Returns:

  • (String, nil)


323
324
325
# File 'lib/mathpix/result.rb', line 323

def inchikey
  data['inchikey']
end

#inspectString

Inspect

Returns:

  • (String)


303
304
305
# File 'lib/mathpix/result.rb', line 303

def inspect
  "#<Mathpix::Result confidence=#{confidence} text=#{text&.[](0..50)}>"
end

#latexString? Also known as: latex_styled

LaTeX styled output

Returns:

  • (String, nil)


30
31
32
# File 'lib/mathpix/result.rb', line 30

def latex
  data['latex_styled'] || data['latex']
end

#latex_simplifiedString?

Simplified LaTeX

Returns:

  • (String, nil)


38
39
40
# File 'lib/mathpix/result.rb', line 38

def latex_simplified
  data['latex_simplified']
end

#line_dataArray<Hash>

Line-level data array (alias for lines_json).

Returns:

  • (Array<Hash>)


106
107
108
# File 'lib/mathpix/result.rb', line 106

def line_data
  lines_json
end

#linesArray<Line>

Get line-by-line data with bounding boxes

Requires snap(..., include_line_data: true)

Returns:

  • (Array<Line>)

    array of line objects



165
166
167
168
169
# File 'lib/mathpix/result.rb', line 165

def lines
  return [] unless data['line_data']

  @lines ||= data['line_data'].map { |line_data| Line.new(line_data) }
end

#lines_jsonArray<Hash>

Get lines as JSON array (raw data)

Returns:

  • (Array<Hash>)


173
174
175
# File 'lib/mathpix/result.rb', line 173

def lines_json
  data['line_data'] || []
end

#mathmlString?

MathML output

Returns:

  • (String, nil)


44
45
46
# File 'lib/mathpix/result.rb', line 44

def mathml
  data['mathml']
end

#metadataHash

Get metadata

Returns:

  • (Hash)


148
149
150
# File 'lib/mathpix/result.rb', line 148

def 
  data['metadata'] || {}
end

#molecular_formulaString?

Molecular formula

Returns:

  • (String, nil)


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

def molecular_formula
  data['molecular_formula']
end

#molecular_nameString?

Molecular name

Returns:

  • (String, nil)


335
336
337
# File 'lib/mathpix/result.rb', line 335

def molecular_name
  data['molecular_name']
end

#molecular_weightFloat?

Molecular weight

Returns:

  • (Float, nil)


341
342
343
# File 'lib/mathpix/result.rb', line 341

def molecular_weight
  data['molecular_weight']
end

#on_failure {|self| ... } ⇒ self

Execute block if failed

Yields:

  • (self)

Returns:

  • (self)


382
383
384
385
# File 'lib/mathpix/result.rb', line 382

def on_failure
  yield self if failure?
  self
end

#on_success {|self| ... } ⇒ self

Execute block if successful

Yields:

  • (self)

Returns:

  • (self)


374
375
376
377
# File 'lib/mathpix/result.rb', line 374

def on_success
  yield self if success?
  self
end

#positionHash?

Bounding box / position of the detected region, if the API returned one.

Returns:

  • (Hash, nil)


94
95
96
# File 'lib/mathpix/result.rb', line 94

def position
  data['position']
end

#printed?Boolean

Is content printed (vs handwritten)?

Returns:

  • (Boolean)


118
119
120
# File 'lib/mathpix/result.rb', line 118

def printed?
  data['is_printed'] == true
end

#processing_time_msInteger?

Processing time in milliseconds

Returns:

  • (Integer, nil)


88
89
90
# File 'lib/mathpix/result.rb', line 88

def processing_time_ms
  data['processing_time_ms']
end

#request_idString?

Request ID

Returns:

  • (String, nil)


82
83
84
# File 'lib/mathpix/result.rb', line 82

def request_id
  data['request_id']
end

#smilesString?

SMILES notation (chemistry)

Returns:

  • (String, nil)


311
312
313
# File 'lib/mathpix/result.rb', line 311

def smiles
  text if chemistry?
end

#source_urlString?

Get source URL if image was processed from URL

Returns:

  • (String, nil)

    URL if source was remote, nil otherwise



16
17
18
19
20
# File 'lib/mathpix/result.rb', line 16

def source_url
  return nil unless @source_path.is_a?(String)

  @source_path.start_with?('http://', 'https://') ? @source_path : nil
end

#stereochemistry?Boolean

Has stereochemistry?

Returns:

  • (Boolean)


347
348
349
# File 'lib/mathpix/result.rb', line 347

def stereochemistry?
  data['has_stereochemistry'] == true || smiles&.include?('@')
end

#success?Boolean

Was capture successful?

Returns:

  • (Boolean)


361
362
363
# File 'lib/mathpix/result.rb', line 361

def success?
  !text.nil? && confidence >= 0.5
end

#table?Boolean

Contains table?

Returns:

  • (Boolean)


142
143
144
# File 'lib/mathpix/result.rb', line 142

def table?
  data['contains_table'] == true
end

#tagsArray<String>

Get tags

Returns:

  • (Array<String>)


154
155
156
# File 'lib/mathpix/result.rb', line 154

def tags
  data['tags'] || []
end

#textString?

Text result

Returns:

  • (String, nil)


24
25
26
# File 'lib/mathpix/result.rb', line 24

def text
  data['text']
end

#timestampString?

Raw capture timestamp (used by recent/search results).

Returns:

  • (String, nil)


100
101
102
# File 'lib/mathpix/result.rb', line 100

def timestamp
  data['timestamp'] || data['created']
end

#to_hHash

Convert to hash

Returns:

  • (Hash)


291
292
293
# File 'lib/mathpix/result.rb', line 291

def to_h
  data
end

#to_jsonString

Convert to JSON

Returns:

  • (String)


297
298
299
# File 'lib/mathpix/result.rb', line 297

def to_json(*)
  data.to_json(*)
end

#word_dataArray<Hash>

Word-level data array, if present.

Returns:

  • (Array<Hash>)


112
113
114
# File 'lib/mathpix/result.rb', line 112

def word_data
  data['word_data'] || []
end