Class: Mathpix::Result
- Inherits:
-
Object
- Object
- Mathpix::Result
- Defined in:
- lib/mathpix/result.rb
Overview
OCR Result object
Direct Known Subclasses
Defined Under Namespace
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#source_path ⇒ Object
readonly
Returns the value of attribute source_path.
Instance Method Summary collapse
-
#asciimath ⇒ String?
AsciiMath output.
-
#chart? ⇒ Boolean
Contains chart?.
-
#chemistry? ⇒ Boolean
Is this a chemistry result?.
-
#confidence ⇒ Float
Confidence score.
-
#confidence_rate ⇒ Float
Confidence rate (alternative).
-
#created_at ⇒ Time?
Created timestamp.
-
#diagram? ⇒ Boolean
Contains diagram?.
-
#failure? ⇒ Boolean
Was capture a failure?.
-
#handwritten? ⇒ Boolean
Is content handwritten?.
-
#html ⇒ String?
HTML output (may contain SVG).
-
#inchi ⇒ String?
InChI notation (chemistry).
-
#inchikey ⇒ String?
InChI Key (chemistry).
-
#initialize(data, source_path = nil) ⇒ Result
constructor
A new instance of Result.
-
#inspect ⇒ String
Inspect.
-
#latex ⇒ String?
(also: #latex_styled)
LaTeX styled output.
-
#latex_simplified ⇒ String?
Simplified LaTeX.
-
#line_data ⇒ Array<Hash>
Line-level data array (alias for lines_json).
-
#lines ⇒ Array<Line>
Get line-by-line data with bounding boxes.
-
#lines_json ⇒ Array<Hash>
Get lines as JSON array (raw data).
-
#mathml ⇒ String?
MathML output.
-
#metadata ⇒ Hash
Get metadata.
-
#molecular_formula ⇒ String?
Molecular formula.
-
#molecular_name ⇒ String?
Molecular name.
-
#molecular_weight ⇒ Float?
Molecular weight.
-
#on_failure {|self| ... } ⇒ self
Execute block if failed.
-
#on_success {|self| ... } ⇒ self
Execute block if successful.
-
#position ⇒ Hash?
Bounding box / position of the detected region, if the API returned one.
-
#printed? ⇒ Boolean
Is content printed (vs handwritten)?.
-
#processing_time_ms ⇒ Integer?
Processing time in milliseconds.
-
#request_id ⇒ String?
Request ID.
-
#smiles ⇒ String?
SMILES notation (chemistry).
-
#source_url ⇒ String?
Get source URL if image was processed from URL.
-
#stereochemistry? ⇒ Boolean
Has stereochemistry?.
-
#success? ⇒ Boolean
Was capture successful?.
-
#table? ⇒ Boolean
Contains table?.
-
#tags ⇒ Array<String>
Get tags.
-
#text ⇒ String?
Text result.
-
#timestamp ⇒ String?
Raw capture timestamp (used by recent/search results).
-
#to_h ⇒ Hash
Convert to hash.
-
#to_json ⇒ String
Convert to JSON.
-
#word_data ⇒ Array<Hash>
Word-level data array, if present.
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
#data ⇒ Object (readonly)
Returns the value of attribute data.
6 7 8 |
# File 'lib/mathpix/result.rb', line 6 def data @data end |
#source_path ⇒ Object (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
#asciimath ⇒ String?
AsciiMath output
50 51 52 |
# File 'lib/mathpix/result.rb', line 50 def asciimath data['asciimath'] end |
#chart? ⇒ Boolean
Contains chart?
130 131 132 |
# File 'lib/mathpix/result.rb', line 130 def chart? data['contains_chart'] == true end |
#chemistry? ⇒ Boolean
Is this a chemistry result?
353 354 355 |
# File 'lib/mathpix/result.rb', line 353 def chemistry? molecular_formula || inchi || smiles&.match?(%r{^[A-Za-z0-9@\[\]()=#+\-\\/.]+$}) end |
#confidence ⇒ Float
Confidence score
62 63 64 |
# File 'lib/mathpix/result.rb', line 62 def confidence data['confidence'] || 0.0 end |
#confidence_rate ⇒ Float
Confidence rate (alternative)
68 69 70 |
# File 'lib/mathpix/result.rb', line 68 def confidence_rate data['confidence_rate'] || confidence end |
#created_at ⇒ Time?
Created timestamp
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?
136 137 138 |
# File 'lib/mathpix/result.rb', line 136 def diagram? data['contains_diagram'] == true end |
#failure? ⇒ Boolean
Was capture a failure?
367 368 369 |
# File 'lib/mathpix/result.rb', line 367 def failure? !success? end |
#handwritten? ⇒ Boolean
Is content handwritten?
124 125 126 |
# File 'lib/mathpix/result.rb', line 124 def handwritten? data['is_handwritten'] == true end |
#html ⇒ String?
HTML output (may contain SVG)
56 57 58 |
# File 'lib/mathpix/result.rb', line 56 def html data['html'] end |
#inchi ⇒ String?
InChI notation (chemistry)
317 318 319 |
# File 'lib/mathpix/result.rb', line 317 def inchi data['inchi'] end |
#inchikey ⇒ String?
InChI Key (chemistry)
323 324 325 |
# File 'lib/mathpix/result.rb', line 323 def inchikey data['inchikey'] end |
#inspect ⇒ String
Inspect
303 304 305 |
# File 'lib/mathpix/result.rb', line 303 def inspect "#<Mathpix::Result confidence=#{confidence} text=#{text&.[](0..50)}>" end |
#latex ⇒ String? Also known as: latex_styled
LaTeX styled output
30 31 32 |
# File 'lib/mathpix/result.rb', line 30 def latex data['latex_styled'] || data['latex'] end |
#latex_simplified ⇒ String?
Simplified LaTeX
38 39 40 |
# File 'lib/mathpix/result.rb', line 38 def latex_simplified data['latex_simplified'] end |
#line_data ⇒ Array<Hash>
Line-level data array (alias for lines_json).
106 107 108 |
# File 'lib/mathpix/result.rb', line 106 def line_data lines_json end |
#lines ⇒ Array<Line>
Get line-by-line data with bounding boxes
Requires snap(..., include_line_data: true)
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_json ⇒ Array<Hash>
Get lines as JSON array (raw data)
173 174 175 |
# File 'lib/mathpix/result.rb', line 173 def lines_json data['line_data'] || [] end |
#mathml ⇒ String?
MathML output
44 45 46 |
# File 'lib/mathpix/result.rb', line 44 def mathml data['mathml'] end |
#metadata ⇒ Hash
Get metadata
148 149 150 |
# File 'lib/mathpix/result.rb', line 148 def data['metadata'] || {} end |
#molecular_formula ⇒ String?
Molecular formula
329 330 331 |
# File 'lib/mathpix/result.rb', line 329 def molecular_formula data['molecular_formula'] end |
#molecular_name ⇒ String?
Molecular name
335 336 337 |
# File 'lib/mathpix/result.rb', line 335 def molecular_name data['molecular_name'] end |
#molecular_weight ⇒ Float?
Molecular weight
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
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
374 375 376 377 |
# File 'lib/mathpix/result.rb', line 374 def on_success yield self if success? self end |
#position ⇒ Hash?
Bounding box / position of the detected region, if the API returned one.
94 95 96 |
# File 'lib/mathpix/result.rb', line 94 def position data['position'] end |
#printed? ⇒ Boolean
Is content printed (vs handwritten)?
118 119 120 |
# File 'lib/mathpix/result.rb', line 118 def printed? data['is_printed'] == true end |
#processing_time_ms ⇒ Integer?
Processing time in milliseconds
88 89 90 |
# File 'lib/mathpix/result.rb', line 88 def processing_time_ms data['processing_time_ms'] end |
#request_id ⇒ String?
Request ID
82 83 84 |
# File 'lib/mathpix/result.rb', line 82 def request_id data['request_id'] end |
#smiles ⇒ String?
SMILES notation (chemistry)
311 312 313 |
# File 'lib/mathpix/result.rb', line 311 def smiles text if chemistry? end |
#source_url ⇒ String?
Get source URL if image was processed from URL
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?
347 348 349 |
# File 'lib/mathpix/result.rb', line 347 def stereochemistry? data['has_stereochemistry'] == true || smiles&.include?('@') end |
#success? ⇒ Boolean
Was capture successful?
361 362 363 |
# File 'lib/mathpix/result.rb', line 361 def success? !text.nil? && confidence >= 0.5 end |
#table? ⇒ Boolean
Contains table?
142 143 144 |
# File 'lib/mathpix/result.rb', line 142 def table? data['contains_table'] == true end |
#tags ⇒ Array<String>
Get tags
154 155 156 |
# File 'lib/mathpix/result.rb', line 154 def data['tags'] || [] end |
#text ⇒ String?
Text result
24 25 26 |
# File 'lib/mathpix/result.rb', line 24 def text data['text'] end |
#timestamp ⇒ String?
Raw capture timestamp (used by recent/search results).
100 101 102 |
# File 'lib/mathpix/result.rb', line 100 def data['timestamp'] || data['created'] end |
#to_h ⇒ Hash
Convert to hash
291 292 293 |
# File 'lib/mathpix/result.rb', line 291 def to_h data end |
#to_json ⇒ String
Convert to JSON
297 298 299 |
# File 'lib/mathpix/result.rb', line 297 def to_json(*) data.to_json(*) end |
#word_data ⇒ Array<Hash>
Word-level data array, if present.
112 113 114 |
# File 'lib/mathpix/result.rb', line 112 def word_data data['word_data'] || [] end |