MycroftAI / adapt

Adapt Intent Parser
Apache License 2.0
710 stars 154 forks source link

`ZeroDivisionError` in determine_intent when tags are empty #114

Closed PLNech closed 3 years ago

PLNech commented 3 years ago

Stacktrace

  File "/home/pln/.PyCharm2019.3/config/scratches/scratch_12.py", line 16, in <module>
    print([i for i in engine.determine_intent(utterance, include_tags=False)])
  File "/home/pln/.PyCharm2019.3/config/scratches/scratch_12.py", line 16, in <listcomp>
    print([i for i in engine.determine_intent(utterance, include_tags=False)])
  File "/home/pln/.virtualenvs/nluservice/src/adapt-parser/adapt/engine.py", line 131, in determine_intent
    best_intent, tags = self.__best_intent(result, remaining_context)
  File "/home/pln/.virtualenvs/nluservice/src/adapt-parser/adapt/engine.py", line 81, in __best_intent
    i, tags = intent.validate_with_tags(parse_result.get('tags') + context_as_entities, parse_result.get('confidence'))
  File "/home/pln/.virtualenvs/nluservice/src/adapt-parser/adapt/intent.py", line 190, in validate_with_tags
    total_confidence = intent_confidence / len(tags) * confidence
ZeroDivisionError: float division by zero

Steps to replicate the Issue

  1. Define an IntentDeterminationEngine as such:

    
    engine: IntentDeterminationEngine = IntentDeterminationEngine()
    engine.register_entity("Kevin", "who") # same problem if several entities
    builder: IntentBuilder = IntentBuilder("Buddies")
    builder.optionally("who") # same problem if several entity types
    engine.register_intent_parser(builder.build())
  2. Use it on an utterance that contains an entity, get expected results

    print([i for i in engine.determine_intent("Kevin is a friend")]) # works
  3. Use it on an utterance that contains no entity, get an exception

    print([i for i in engine.determine_intent("Julien is a friend")]) # raises

Can also be reproduced with an empty utterance "".

Be as specific as possible about the expected condition, and the deviation from expected condition.

I expect the engine to return an empty determination ([]) when no entity is recognized, instead of raising a generic ZeroDivisionError exception.

PLNech commented 3 years ago

I believe fixing this could just mean refactoring validate_with_tags. For now I'd see this as: