<jupyter_start><jupyter_text>Useful resources:* https://tomassetti.me/parsing-in-python/* https://github.com/lark-parser/lark/blob/master/docs/lark_cheatsheet.pdf* https://github.com/lark-parser/lark/blob/9f666a74595ff8ac0f550abbb687a517fe5d495d/lark/grammars/common.lark* https://gitlab.cern.ch/lhcb-datapkg/Gen/DecFiles/tree/master/dkfiles* https://github.com/PMunkes/evtgen/blob/master/DECAY.DEC<jupyter_code>from particle import Particle
from particle.particle.particle import  ParticleNotFound
from decaylanguage import data
with data.open_text(data, 'DECAY_LHCB.DEC') as f:
    txt = f.read()
with data.open_text(data, 'decfile.lark') as f:
    grammar = f.read()
from decaylanguage.dec import dec
from lark import Lark, Transformer, Tree
l = Lark(grammar, parser='lalr', lexer='standard') # , transformer = TreeToDec())
%%time
parsed = l.parse(txt)
bool(parsed)
transformed = dec.TreeToDec().transform(parsed)
define = dec.define(transformed)
pythia_def = dec.pythia_def(transformed)
alias = dec.alias(transformed)
chargeconj = dec.chargeconj(transformed)
global_photos = dec.global_photos(transformed)
decay = dec.decay(transformed)
cdecay = dec.cdecay(transformed)
setlspw = dec.setlspw(transformed)
for item in pythia_def:
    print(item[0], ":", item[1], "=", item[2])
missing = set()
keyerrs = set()

class TreeToDec2(Transformer):
    def __init__(self, alias_dict):
        self.alias_dict = alias_dict
    def particle(self, items):
        label, = items
        if label in self.alias_dict:
            label = self.alias_dict[label]
        try:
            return Particle.from_string(str(label))
        except ParticleNotFound:
            missing.add(str(label))
            return str(label)
        except KeyError:
            keyerrs.add(str(label))
            return str(label)
%%time
labelled = TreeToDec2(alias).transform(decay)
missing
keyerrs
print(labelled.children[0].pretty())<jupyter_output>decay
  Upsilon(4S)0
  decayline
    0.515122645
    B+
    B-
    model	VSS
  decayline
    0.483122645
    B0
    B~0
    model
      VSS
      model_options
        _BMIX
        dm
  decayline
    1.5583e-05
    e+
    e-
    PhotosEnum.yes
    model	VLL
  decayline
    1.5766e-05
    mu+
    mu-
    PhotosEnum.yes
    model	VLL
  decayline
    1.5766e-05
    tau+
    tau-
    PhotosEnum.yes
    model	VLL
  decayline
    8.4099e-05
    Upsilon(2S)0
    pi+
    pi-
    model
      VVP
      model_options	IPI
  decayline
    4.4342e-05
    Upsilon(2S)0
    pi0
    pi0
    model
      VVP
      model_options	IPI
  decayline
    8.0123e-05
    Upsilon(1S)0
    pi+
    pi-
    model
      VVP
      model_options	IPI
  decayline
    4.4342e-05
    Upsilon(1S)0
    pi0
    pi0
    model
      VVP
      model_options	IPI
  decayline
    0.000194392
    Upsilon(1S)0
    eta0
    model
      PARTWAVE
      model_options
        0.0
        0.0
        1.0
        0.0
        0.0
        0.0
[...]
{'fraction_non_alphanumeric': 0.10300136425648022, 'fraction_numerical': 0.047748976807639835, 'mean_word_length': 2.529482551143201, 'pattern_counts': {'":': 1, '<': 4, '<?xml version=': 0, '>': 4, 'https://': 5, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '550697', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 1164, 'n_tokens_neox': 1089, 'n_words': 204, 'path': 'notebooks/DecReader.ipynb', 'repo_name': 'ch2ohch2oh/decaylanguage', 'size': '2932'}