Skip to content

Instantly share code, notes, and snippets.

@cra
Created April 1, 2020 12:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cra/a0f574e3a9b43e813574360516579cc0 to your computer and use it in GitHub Desktop.
Save cra/a0f574e3a9b43e813574360516579cc0 to your computer and use it in GitHub Desktop.
to_parse = """
If (feature 1 <= 0.0690282131661442)
Predict: 0.0
Else (feature 1 > 0.0690282131661442)
Predict: 1.0
"""
def parse_this_shit(shit):
lines = [line.strip() for line in shit.split('\n')]
def _parse_lines():
i = 0
while i < len(lines):
line = lines[i]
if line.startswith('If') or line.startswith('Else'):
nom = line.split('(feature ')[1]
feature_index = nom.split()[0]
threshold = line.split()[-1].strip(')')
predict_line = lines[i + 1]
predict_class = predict_line.split('Predict: ')[1]
yield {'feature': feature_index, 'predict': predict_class, 'threshold': threshold}
i += 1
return list(_parse_lines())
parse_this_shit(to_parse)
# [{'feature': '1', 'predict': '0.0', 'threshold': '0.0690282131661442'},
# {'feature': '1', 'predict': '1.0', 'threshold': '0.0690282131661442'}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment