Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Time filtering on hour if datetime_filters is set. #134

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions dialogy/plugins/text/duckling_plugin/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,12 @@
from dialogy.base.entity_extractor import EntityScoringMixin
from dialogy.types import BaseEntity, Intent
from dialogy.types.entity.deserialize import EntityDeserializer
from dialogy.utils import dt2timestamp, lang_detect_from_text, logger
from dialogy.utils import (
dt2timestamp,
lang_detect_from_text,
logger,
unix_ts_to_datetime,
)


class DucklingPlugin(EntityScoringMixin, Plugin):
Expand Down Expand Up @@ -737,10 +742,17 @@ def select_datetime(
entities, lambda entity: entity.dim == const.TIME
)

reference_time = unix_ts_to_datetime(
self.reference_time, timezone=self.timezone
)
reference_time = reference_time.replace(
hour=reference_time.hour, minute=0, second=0, microsecond=0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hour=reference_time.hour isn't that already happening?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my bad, changing this.

)

filtered_time_entities = [
entity
for entity in time_entities
if operation(dt2timestamp(entity.get_value()), self.reference_time)
if operation(dt2timestamp(entity.get_value()), dt2timestamp(reference_time))
]
return filtered_time_entities + other_entities

Expand Down
113 changes: 112 additions & 1 deletion tests/plugin/text/test_duckling_plugin/test_duckling_plugin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import operator
import time
from datetime import datetime
from http.client import REQUESTED_RANGE_NOT_SATISFIABLE

import httpretty
Expand All @@ -10,7 +11,7 @@
from dialogy.base import Input, Output
from dialogy.plugins import DucklingPlugin
from dialogy.types import BaseEntity, Intent, KeywordEntity, TimeEntity
from dialogy.utils import make_unix_ts
from dialogy.utils import dt2timestamp, make_unix_ts
from dialogy.workflow import Workflow
from tests import EXCEPTIONS, load_tests, request_builder

Expand Down Expand Up @@ -100,6 +101,116 @@ def test_remove_low_scoring_entities_doesnt_remove_unscored_entities():
]


def test_datetime_filters_future_entities():
duckling_plugin = DucklingPlugin(
locale="en_IN",
dimensions=["time"],
timezone="Asia/Kolkata",
threshold=0.2,
datetime_filters="future",
)
reference_time = "2022-03-21T18:00:00+05:30"
duckling_plugin.reference_time = dt2timestamp(
datetime.fromisoformat(reference_time)
)

bodyA = "5 pm"
bodyB = "6 pm"
bodyC = "7 pm"

entityA = TimeEntity(
range={
"start": 0,
"end": len(bodyA),
},
body=bodyA,
dim="time",
values=[{"value": "2022-03-21T17:00:00+05:30"}],
grain="hour",
)

entityB = TimeEntity(
range={
"start": 0,
"end": len(bodyB),
},
body=bodyB,
dim="time",
values=[{"value": "2022-03-21T18:00:00+05:30"}],
grain="hour",
)

entityC = TimeEntity(
range={
"start": 0,
"end": len(bodyC),
},
body=bodyC,
dim="time",
values=[{"value": "2022-03-21T19:00:00+05:30"}],
grain="hour",
)

assert duckling_plugin.select_datetime(
[entityA, entityB, entityC], filter_type=duckling_plugin.datetime_filters
) == [entityB, entityC]


def test_datetime_filters_past_entities():
duckling_plugin = DucklingPlugin(
locale="en_IN",
dimensions=["time"],
timezone="Asia/Kolkata",
threshold=0.2,
datetime_filters="past",
)
reference_time = "2022-03-21T18:00:00+05:30"
duckling_plugin.reference_time = dt2timestamp(
datetime.fromisoformat(reference_time)
)

bodyA = "5 pm"
bodyB = "6 pm"
bodyC = "7 pm"

entityA = TimeEntity(
range={
"start": 0,
"end": len(bodyA),
},
body=bodyA,
dim="time",
values=[{"value": "2022-03-21T17:00:00+05:30"}],
grain="hour",
)

entityB = TimeEntity(
range={
"start": 0,
"end": len(bodyB),
},
body=bodyB,
dim="time",
values=[{"value": "2022-03-21T18:00:00+05:30"}],
grain="hour",
)

entityC = TimeEntity(
range={
"start": 0,
"end": len(bodyC),
},
body=bodyC,
dim="time",
values=[{"value": "2022-03-21T19:00:00+05:30"}],
grain="hour",
)

assert duckling_plugin.select_datetime(
[entityA, entityB, entityC], filter_type=duckling_plugin.datetime_filters
) == [entityA, entityB]


def test_duckling_connection_error() -> None:
"""
[summary]
Expand Down