comtravo / ctparse

Parse natural language time expressions in python
https://www.comtravo.com
MIT License
131 stars 23 forks source link

[Question] adding new attribute to Time object #108

Closed inferense closed 3 years ago

inferense commented 3 years ago

I'm trying to add a new attribute called period to the Time object in types.py which will hold a string "am" or "pm" if it's specified in the input ("5am"). This is so I can make some further modifications based on it to the logic of some rules. For this, I've modified the _maybe_apply_am_pm in rules.py as below:

if ampm_match.lower().startswith("a") and t.hour <= 12:
    t.period = "am"
    return t
if ampm_match.lower().startswith("p") and t.hour < 12:
    return Time(hour=t.hour + 12, minute=t.minute, period='pm')

Initially, this worked but disabled much of the other applicable rules in the pipeline and resulted in latent incomplete outputs (for "5am" I'd get a latent datetime X-X-X with 05:00 instead of a complete datetime for today / tomorrow etc.)

I figured I had to modify Time property isTOD to

@property
def isTOD(self) -> bool:
   """isTimeOfDay - only a time, not date"""
    return self._hasOnly("hour") or self._hasOnly("hour", "minute") or self._hasOnly("hour", "period") or self._hasOnly("hour", "minute", "period")

this enabled the previously latent datetime completion, intervals etc. However, my new attribute got somehow lost in the pipeline of rules and is not propagated to the resulting production. I've spent hours debugging and experimenting with this but I'm really not sure what else to change to make this work?

When testing the simple "5pm", I tried to look into latentDOM rule but the attribute is not propagated to it from the HHMM where its present. When debugging I used the logging as you described in the documentation but it hardly gives me all the rules that are applied, or I'm missing something somewhere else?

Thank you

inferense commented 3 years ago

I finally managed to solve it by adding the attribute to postprocess_latent.py. It works fine for now, but let me know if there's anything else I should be mindful of :)

sebastianmika commented 3 years ago

Happy to hear you managed to solve that - sorry for not yet supporting you. I planned this afternoon, but now you were quicker!