Module: ChemScanner::Interpreter

Defined in:
lib/chem_scanner/interpreter/post_process/assemble.rb,
lib/chem_scanner/interpreter/scheme.rb,
lib/chem_scanner/interpreter/scheme_base.rb,
lib/chem_scanner/interpreter/element/atom.rb,
lib/chem_scanner/interpreter/element/arrow.rb,
lib/chem_scanner/interpreter/formula_to_mol.rb,
lib/chem_scanner/interpreter/element/fragment.rb,
lib/chem_scanner/interpreter/element/molecule.rb,
lib/chem_scanner/interpreter/element/reaction.rb,
lib/chem_scanner/interpreter/pre_process/arrow.rb,
lib/chem_scanner/interpreter/pre_process/graphic.rb,
lib/chem_scanner/interpreter/pre_process/molecule.rb,
lib/chem_scanner/interpreter/element/reaction_step.rb,
lib/chem_scanner/interpreter/element/molecule_group.rb,
lib/chem_scanner/interpreter/text_group/bold_groups.rb,
lib/chem_scanner/interpreter/post_process/text_label.rb,
lib/chem_scanner/interpreter/post_process/reaction_info.rb,
lib/chem_scanner/interpreter/post_process/reaction_step.rb,
lib/chem_scanner/interpreter/post_process/reagent_label.rb,
lib/chem_scanner/interpreter/text_group/retrieve_n_atoms.rb,
lib/chem_scanner/interpreter/post_process/text_as_molecule.rb,
lib/chem_scanner/interpreter/post_process/label_by_molecule.rb,
lib/chem_scanner/interpreter/text_group/molecule_text_group.rb,
lib/chem_scanner/interpreter/text_group/retrieve_alias_info.rb,
lib/chem_scanner/interpreter/text_group/reaction_text_groups.rb,
lib/chem_scanner/interpreter/reaction_detection/molecule_group.rb,
lib/chem_scanner/interpreter/text_group/text_group_interpreter.rb,
lib/chem_scanner/interpreter/reaction_detection/text_assignment.rb,
lib/chem_scanner/interpreter/reaction_detection/assign_to_reaction.rb,
lib/chem_scanner/interpreter/reaction_detection/duplicate_reagents.rb,
lib/chem_scanner/interpreter/reaction_detection/remove_separated_mol.rb,
lib/chem_scanner/interpreter/reaction_detection/multi_line_chain_reaction.rb

Overview

Interpret the parsed/extracted geometry block

Defined Under Namespace

Modules: BoldGroup, PostProcess, PreProcess, ReactionDetection, SchemeBase Classes: Arrow, Atom, Fragment, Molecule, MoleculeGroup, MoleculeTextGroup, Reaction, ReactionStep, ReactionTextGroup, Scheme, TextGroupInterpreter

Constant Summary collapse

ABB_DELIM =
Regexp.new('[.·\s,\'"\/\n]')
ALIAS_GROUP =
["Ar", "X", "Y", "M"].freeze
GENERATE_RGROUP =
0
GENERATE_ALIAS_GROUP =
1
GENERATE_N_ATOM =
2
OPEN_MARK =
'[\(\[\{]'.freeze
CLOSE_MARK =
'[\)\]\}]'.freeze
ESTIMATED_DIST =
0.2
JOIN_WORDS =
%w[and with plus].freeze
START_REGEX =
'(?<=\s|,|;|\n|\r|\[|\(|\.|\A|^)+'
ENDING_REGEX =
'(?=\s|,|;|\n|\r|\]|\)|\.|\z|$)+'
DEGREE_REGEX =
'((°\s*[CF])|(℃|℉))'
RANGE_REGEX =
"(-|−|–|—|~|to|till|until)"
N_ATOMS_REGEXES =
[
  /\A\( +\) *([nm])\z/,
  /\A\[ +\] *([nm])\z/,
  /\A\{ +\} *([nm])\z/,
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.alias_group?(text) ⇒ Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/chem_scanner/interpreter/scheme_base.rb', line 15

def self.alias_group?(text)
  ALIAS_GROUP.include?(text)
end

.rgroup_atom?(text) ⇒ Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/chem_scanner/interpreter/scheme_base.rb', line 19

def self.rgroup_atom?(text)
  !(text =~ /R\d+/).nil? || !(text =~ /R */).nil?
end

.super_atom?(text) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/chem_scanner/interpreter/scheme_base.rb', line 23

def self.super_atom?(text)
  alias_group?(text) || rgroup_atom?(text)
end

Instance Method Details

#mol_from_inorganic_formula(text) ⇒ Object

NOTE: WIP file



10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/chem_scanner/interpreter/formula_to_mol.rb', line 10

def mol_from_inorganic_formula(text)
  return nil unless text.class == String

  string = text.dup
  iter = string =~ /#{OPEN_MARK}/
  return parse_formula(text) if iter.nil?

  reverse_string = string.reverse
  reverse_iter = reverse_string =~ /#{CLOSE_MARK}/

  math_data = text.match(formula_regex)
end

#parse_formula(formula, out_valence = 0) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/chem_scanner/interpreter/formula_to_mol.rb', line 23

def parse_formula(formula, out_valence = 0)
  # NOTE: sort alphabetically then by length,
  # so that C will not be catched first in Ca
  el_names = ELEMENTS.map { |x| x["name"] }
  els = el_names.sort_by { |a| [a[0], -a.size] }.join("|")
  num = "[1-9]"
  charge = "[-+]"
  return nil unless formula.split(/#{els}|#{num}|#{charge}/).empty?

  el_arr = formula.scan(/(#{els})(#{num}{0,2})/).map do |el, elnum|
    el_info = ELEMENTS.detect { |e| e["name"] == el }
    return nil if el_info.nil? || el_info["valences"][2].first.zero?

    {
      name: el,
      num: elnum.empty? ? 1 : elnum.to_i,
      valences: el_info["valences"][2],
    }
  end
  return nil if el_arr.size == 1

  # el_arr.sort_by! { |el| el[:valences].max }
  fel = el_arr.first
  others = el_arr[1..-1]

  valence_combination = []
  idx_map = others.map { |el| el[:valences].count - 1 }

  fel[:valences].each do |fvalen|
    idx_iter = Array.new(idx_map.size, 0)
    iter = idx_iter.size - 1
    stop = false

    until stop do
      vasum = idx_iter.reduce(0) do |sum, idx|
        el_valence = others[idx][:valences]
        cur_val = idx_iter[idx]
        sum += el_valence[cur_val]
      end

      valence_combination.push(idx_iter) if (vasum + fvalen) == out_valence

      if idx_iter[iter] == idx_map[iter]
        stope = true if iter.zero?
        iter -= 1
      else
        idx_iter[iter] += 1
      end
    end
  end
end