rezemika / humanized_opening_hours

A parser for the opening_hours fields from OpenStreetMap
GNU Affero General Public License v3.0
26 stars 20 forks source link

Parsing a description ? (reverse parsing) #38

Open raphodn opened 4 years ago

raphodn commented 4 years ago

This tool is really great for parsing (or sanitizing) strings that have the osm opening_hours format, and getting them in a readable description. πŸ’―

hoh.OHParser(" Mo-Fr 08:30-16:30", locale="fr").description()
// ['Du lundi au vendredi : 08:30 – 16:30.']

I was wondering if it was possible to leverage this tool (or another tool ?) to do the other way around ? πŸ‘‡

hoh.parse_description("Du lundi au vendredi : 08:30 – 16:30", locale="fr")
// Mo-Fr 08:30-16:30

fyi my current implementation/workaround for this is with string replace πŸ™ƒ

import re

oh_description = "Du lundi au vendredi : 08:30 – 16:30"
oh_description = re.sub("du", "", oh_description, flags=re.IGNORECASE)
oh_description = re.sub("lundi", "Mo", oh_description, flags=re.IGNORECASE)
...

Thanks anyway

rezemika commented 4 years ago

Thank you!

Unfortunately, it would not be easily feasible with HOH by now, because the parsing and the rendering logic are very separated. It may be possible to use a Lark's Transformer to transform the tree (obtained from the parsing of the field) into a string, but all remains to be done, and it would make localisation quite difficult.

This way is feasible for simple fields, but not for more complex fields like Jan-Feb Sa[1] 08:30-16:30. The description (not even implemented) would be something like De janvier Γ  fΓ©vrier, le premier samedi du mois : 08:30 – 16:30.

raphodn commented 4 years ago

You're right, it covers 80% of the use-cases, but fails on some specific stuff. I'm making it slowly grow, with tests, I'll see what to do when it becomes un-manageable :)

rezemika commented 4 years ago

Ahah, good luck, I hope it will fit your needs! ^^