Skip to content

Commit

Permalink
Merge pull request #1280 from Tpt/duckling
Browse files Browse the repository at this point in the history
Duckling HTTP: uses Message time for parsing and allows to set timezone
  • Loading branch information
tmbo committed Aug 2, 2018
2 parents 148a770 + e885cba commit f089272
Showing 1 changed file with 33 additions and 7 deletions.
40 changes: 33 additions & 7 deletions rasa_nlu/extractors/duckling_http_extractor.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import logging
import os
import time

import requests
import simplejson
Expand Down Expand Up @@ -41,6 +42,10 @@ class DucklingHTTPExtractor(EntityExtractor):

# locale - if not set, we will use the language of the model
"locale": None,

# timezone like Europe/Berlin
# if not set the default timezone of Duckling is going to be used
"timezone": None
}

def __init__(self, component_config=None, language=None):
Expand Down Expand Up @@ -72,11 +77,19 @@ def _url(self):

return self.component_config.get("url")

def _duckling_parse(self, text):
def _payload(self, text, reference_time):
return {
"text": text,
"locale": self._locale(),
"tz": self.component_config.get("timezone"),
"reftime": reference_time
}

def _duckling_parse(self, text, reference_time):
"""Sends the request to the duckling server and parses the result."""

try:
payload = {"text": text, "locale": self._locale()}
payload = self._payload(text, reference_time)
headers = {"Content-Type": "application/x-www-form-urlencoded; "
"charset=UTF-8"}
response = requests.post(self._url() + "/parse",
Expand All @@ -99,20 +112,33 @@ def _duckling_parse(self, text):
"Error: {}".format(e))
return []

@staticmethod
def _reference_time_from_message(message):
if message.time is not None:
try:
return int(message.time)
except ValueError as e:
logging.warning("Could not parse timestamp {}. Instead "
"current UTC time will be passed to "
"duckling. Error: {}".format(message.time, e))
# fallbacks to current time
return int(time.time())

def process(self, message, **kwargs):
# type: (Message, **Any) -> None

if self._url() is not None:
matches = self._duckling_parse(message.text)
reference_time = self._reference_time_from_message(message)
matches = self._duckling_parse(message.text, reference_time)
dimensions = self.component_config["dimensions"]
relevant_matches = filter_irrelevant_matches(matches, dimensions)
extracted = convert_duckling_format_to_rasa(relevant_matches)
else:
extracted = []
logger.warn("Duckling HTTP component in pipeline, but no "
"`url` configuration in the config "
"file nor is `RASA_DUCKLING_HTTP_URL` "
"set as an environment variable.")
logger.warning("Duckling HTTP component in pipeline, but no "
"`url` configuration in the config "
"file nor is `RASA_DUCKLING_HTTP_URL` "
"set as an environment variable.")

extracted = self.add_extractor_name(extracted)
message.set("entities",
Expand Down

0 comments on commit f089272

Please sign in to comment.