Class: ChemScanner::Interpreter::ReactionTextGroup
- Inherits:
-
Object
- Object
- ChemScanner::Interpreter::ReactionTextGroup
show all
- Includes:
- BoldGroup
- Defined in:
- lib/chem_scanner/interpreter/text_group/reaction_text_groups.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Methods included from BoldGroup
#extract_alphabet_number, #extract_range_number, #group_combinations, #line_bold_groups, #normalize_bold, #normalize_bold_groups, #text_bold_groups
Constructor Details
#initialize(reaction, alias_info, scheme) ⇒ ReactionTextGroup
Returns a new instance of ReactionTextGroup.
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
|
# File 'lib/chem_scanner/interpreter/text_group/reaction_text_groups.rb', line 12
def initialize(reaction, alias_info, scheme)
@reaction = reaction
@alias_info = alias_info
@scheme = scheme
@mol_map = scheme.mol_map
@text_map = scheme.text_map
@alias_groups = []
@text_groups = []
@reactants = {}
@reagents = {}
@products = {}
retrieve_alias_groups
end
|
Instance Attribute Details
#alias_groups ⇒ Object
Returns the value of attribute alias_groups.
10
11
12
|
# File 'lib/chem_scanner/interpreter/text_group/reaction_text_groups.rb', line 10
def alias_groups
@alias_groups
end
|
#bold_groups ⇒ Object
Returns the value of attribute bold_groups.
10
11
12
|
# File 'lib/chem_scanner/interpreter/text_group/reaction_text_groups.rb', line 10
def bold_groups
@bold_groups
end
|
#plain_groups ⇒ Object
Returns the value of attribute plain_groups.
10
11
12
|
# File 'lib/chem_scanner/interpreter/text_group/reaction_text_groups.rb', line 10
def plain_groups
@plain_groups
end
|
#text_groups ⇒ Object
Returns the value of attribute text_groups.
10
11
12
|
# File 'lib/chem_scanner/interpreter/text_group/reaction_text_groups.rb', line 10
def text_groups
@text_groups
end
|
Instance Method Details
#generate_from_bold_groups ⇒ 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
167
168
169
170
171
172
173
174
175
176
|
# File 'lib/chem_scanner/interpreter/text_group/reaction_text_groups.rb', line 129
def generate_from_bold_groups
max = max_group_size
genr_arr = []
used_groups = []
(0..max - 1).each do |idx|
new_r = false
genr = @reaction.clone
[@reactants, @reagents, @products].each do |group|
group.select { |k, _| @alias_info.key?(k) }.each do |k, v|
next if v.bold_groups.empty?
mg = v.bold_groups[idx] || v.bold_groups.last
mg_info = mg
if mg[:group].empty? && !mg[:bold].nil?
mgs = @bold_groups.select do |bg|
bg[:bold] == mg[:bold] && !bg[:group].empty?
end
next unless mgs.count == 1
mg_info = mg.merge(mgs.first)
end
is_used = used_groups.detect do |x|
(
x[:bold] == mg_info[:bold] &&
x[:group].to_a == mg_info[:group].to_a
)
end
next unless is_used.nil?
new_r = true
mtg = MoleculeTextGroup.new(@mol_map[k], @alias_info, @scheme)
used_groups.push(mg_info)
mtg.text_groups = [mg_info]
gmol = mtg.generate_molecule.first
genr.replace_molecule(k, gmol)
end
end
genr_arr.push(genr) if new_r
end
genr_arr
end
|
#generate_from_plain_groups ⇒ Object
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
# File 'lib/chem_scanner/interpreter/text_group/reaction_text_groups.rb', line 100
def generate_from_plain_groups
genr_arr = []
combis = @text_groups.reject { |x| x.key?(:bold) }
combis.each do |combi|
mids = @alias_info.keys
rmids = @reaction.all_ids.select do |mid|
mids.include?(mid) || @mol_map[mid].clone_from
end
next if rmids.empty?
genr = @reaction.clone
rmids.each do |mid|
next unless @alias_info.key?(mid)
mtg = MoleculeTextGroup.new(@mol_map[mid], @alias_info, @scheme)
mtg.text_groups = [combi]
gmol = mtg.generate_molecule.first
genr.replace_molecule(mid, gmol)
end
genr_arr.push(genr)
end
genr_arr
end
|
#generate_reaction ⇒ Object
93
94
95
96
97
98
|
# File 'lib/chem_scanner/interpreter/text_group/reaction_text_groups.rb', line 93
def generate_reaction
creactions = generate_from_plain_groups
bg_reactions = generate_from_bold_groups
creactions.concat(bg_reactions)
end
|
#inspect ⇒ Object
211
212
213
214
215
216
217
218
|
# File 'lib/chem_scanner/interpreter/text_group/reaction_text_groups.rb', line 211
def inspect
(
"#<ReactionTextGroup: id=#{@reaction.arrow_id}, " +
"alias_groups: #{@alias_groups}, " +
"plain_groups: #{@plain_groups}, " +
"bold_groups: #{@bold_groups} >"
)
end
|
#interpret ⇒ Object
42
43
44
45
46
47
48
49
50
51
52
53
54
|
# File 'lib/chem_scanner/interpreter/text_group/reaction_text_groups.rb', line 42
def interpret
@bold_groups = []
@plain_groups = {}
interpret_reaction_text
%w[reactant reagent product].each { |g| interpret_molecule_group(g) }
@plain_groups.select! { |k, _| @alias_groups.include?(k) }
combis = group_combinations(@plain_groups).map { |g| { group: g } }
@text_groups = @bold_groups + combis
end
|
#interpret_molecule_group(group) ⇒ Object
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'lib/chem_scanner/interpreter/text_group/reaction_text_groups.rb', line 73
def interpret_molecule_group(group)
group_ids = @reaction.send("#{group}_ids")
gmols = group_ids.select { |i| @mol_map.key?(i) }
layout = {}
gmols.each do |mid|
mol = @mol_map[mid]
mtg = MoleculeTextGroup.new(mol, @alias_info, @scheme)
mtg.interpret
@bold_groups.concat(mtg.bold_groups)
@plain_groups.merge!(mtg.plain_groups) { |_, o, n| o.concat(n) }
layout[mid] = mtg
end
@bold_groups.reject! { |bg| bg[:group].empty? }
instance_variable_set("@#{group}s".to_sym, layout) unless layout.empty?
end
|
#interpret_reaction_text ⇒ Object
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
# File 'lib/chem_scanner/interpreter/text_group/reaction_text_groups.rb', line 56
def interpret_reaction_text
@reaction.text_ids.each do |tid|
text = @text_map[tid].markdown.gsub(/ *\*\* *\n/, "\n**")
lines = text.split("\n")
lines.each do |line|
groups = reaction_line_bold_groups(line)
plain, bolds = groups.partition { |x| x[:bold].nil? }
@bold_groups.concat(bolds)
plain.each do |nb|
@plain_groups.merge!(nb) { |_, old, new| old.concat(new) }
end
end
end
end
|
#max_group_size ⇒ Object
198
199
200
201
202
203
204
205
206
207
208
209
|
# File 'lib/chem_scanner/interpreter/text_group/reaction_text_groups.rb', line 198
def max_group_size
max_group_size = 0
[@reactants, @reagents, @products].each do |group|
group.each_value do |v|
gmax = v.bold_groups.count
max_group_size = gmax if gmax > max_group_size
end
end
max_group_size
end
|
#reaction_line_bold_groups(line) ⇒ Object
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
# File 'lib/chem_scanner/interpreter/text_group/reaction_text_groups.rb', line 178
def reaction_line_bold_groups(line)
bold_info, groups = line_bold_groups(line, @alias_groups)
list_bold = bold_info.reduce([]) do |arr, bold|
bold.gsub!(":", "")
norm = normalize_bold(bold)
barr = norm.empty? ? bold : norm
arr.push(barr)
end
bolds = list_bold.reject(&:empty?)
return [] if bolds.empty? && groups.empty?
rgroups = bolds.reduce([]) do |arr, bold|
arr.concat(normalize_bold_groups(bold, groups))
end
bolds.empty? ? [groups] : rgroups
end
|
#retrieve_alias_groups ⇒ Object
32
33
34
35
36
37
38
39
40
|
# File 'lib/chem_scanner/interpreter/text_group/reaction_text_groups.rb', line 32
def retrieve_alias_groups
mids = @reaction.all_ids.select { |id| @mol_map.key?(id) }
@alias_groups = mids.reduce([]) do |arr, mid|
next arr unless @alias_info.key?(mid)
alias_groups = @alias_info[mid].map { |i| i[:group] }.compact
arr.concat(alias_groups.reject(&:empty?))
end
end
|