Skip to content

Commit

Permalink
fix(parser): handle decimal subscripts
Browse files Browse the repository at this point in the history
Translate a Unicode decimal point in a subscript as a text decimal
point.  Use `.` as the decimal point operator and `..` as the hydrate
operator.  Add zinc nitrate example from bjodah#207.

Signed-off-by: Jeremy A Gray <jeremy.a.gray@gmail.com>
  • Loading branch information
jeremyagray committed Sep 23, 2023
1 parent 1ef1bf6 commit e194d81
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 3 deletions.
8 changes: 5 additions & 3 deletions chempy/util/parsing.py
Expand Up @@ -95,7 +95,7 @@ def _get_formula_parser():
| '{' formula '}'
| '[' formula ']' ) count prime charge?
formula :: term+
hydrate :: '.' count? formula
hydrate :: '..' count? formula
state :: '(' ( 's' | 'l' | 'g' | 'aq' | 'cr' ) ')'
compound :: count formula hydrate? state?
Expand Down Expand Up @@ -600,9 +600,11 @@ def formula_to_latex(formula, prefixes=None, infixes=None, **kwargs):
)


_unicode_sub = {}
_unicode_sub = {
".": ".",
}

for k, v in enumerate("₀₁₂₃₄₅₆₇₈₉"):
for k, v in enumerate("₀₁₂₃₄₅₆₇₈₉."):
_unicode_sub[str(k)] = v

_unicode_sup = {
Expand Down
38 changes: 38 additions & 0 deletions chempy/util/tests/test_parsing.py
Expand Up @@ -11,6 +11,7 @@
parsing_library,
to_reaction,
)

from ..testing import requires


Expand Down Expand Up @@ -625,6 +626,22 @@ def test_formula_to_latex_caged(species, latex):
("[Fe(H2O)6][Fe(CN)6]..19H2O(aq)", r"[Fe(H₂O)₆][Fe(CN)₆]·19H₂O(aq)"),
("[Fe(CN)6]-3", r"[Fe(CN)₆]³⁻"),
("[Fe(CN)6]-3(aq)", r"[Fe(CN)₆]³⁻(aq)"),
(
"Ca2.832Fe0.6285Mg5.395(CO3)6",
r"Ca₂.₈₃₂Fe₀.₆₂₈₅Mg₅.₃₉₅(CO₃)₆",
),
(
"Ca2.832Fe0.6285Mg5.395(CO3)6(s)",
r"Ca₂.₈₃₂Fe₀.₆₂₈₅Mg₅.₃₉₅(CO₃)₆(s)",
),
(
"Ca2.832Fe0.6285Mg5.395(CO3)6..8H2O(s)",
r"Ca₂.₈₃₂Fe₀.₆₂₈₅Mg₅.₃₉₅(CO₃)₆·8H₂O(s)",
),
(
"Zn(NO3)2..6H2O",
r"Zn(NO₃)₂·6H₂O",
),
],
)
@requires(parsing_library)
Expand Down Expand Up @@ -692,8 +709,29 @@ def test_formula_to_unicode_caged(species, unicode):
),
("[Fe(CN)6]-3", r"[Fe(CN)<sub>6</sub>]<sup>3-</sup>"),
("[Fe(CN)6]-3(aq)", r"[Fe(CN)<sub>6</sub>]<sup>3-</sup>(aq)"),
(
"Ca2.832Fe0.6285Mg5.395(CO3)6",
r"Ca<sub>2.832</sub>Fe<sub>0.6285</sub>Mg<sub>5.395</sub>(CO<sub>3</sub>)<sub>6</sub>",
),
(
"Ca2.832Fe0.6285Mg5.395(CO3)6(s)",
r"Ca<sub>2.832</sub>Fe<sub>0.6285</sub>Mg<sub>5.395</sub>(CO<sub>3</sub>)<sub>6</sub>(s)",
),
(
"Ca2.832Fe0.6285Mg5.395(CO3)6..8H2O(s)",
r"Ca<sub>2.832</sub>Fe<sub>0.6285</sub>Mg<sub>5.395</sub>(CO<sub>3</sub>)<sub>6</sub>&sdot;8H<sub>2</sub>O(s)",
),
(
"Ca2.832Fe0.6285Mg5.395(CO3)6..8H2O(s)",
r"Ca<sub>2.832</sub>Fe<sub>0.6285</sub>Mg<sub>5.395</sub>(CO<sub>3</sub>)<sub>6</sub>&sdot;8H<sub>2</sub>O(s)",
),
(
"Zn(NO3)2..6H2O",
r"Zn(NO<sub>3</sub>)<sub>2</sub>&sdot;6H<sub>2</sub>O",
),
],
)

@requires(parsing_library)
def test_formula_to_html(species, html):
assert formula_to_html(species) == html
Expand Down

0 comments on commit e194d81

Please sign in to comment.