GithubHelp home page GithubHelp logo

Comments (8)

stopachka avatar stopachka commented on April 24, 2024 1

Hey @nsanthanam

You can provide the reference time in the context, when calling parse.

Here's an example:

module Duckling.TimezoneExample
  ( parseText ) where

import Data.Text (Text)

import Duckling.Core
import Duckling.Data.TimeZone

parseText :: Text -> Text -> Lang -> IO [Entity]
parseText text tz lang = do
  tzs <- loadTimeZoneSeries "/usr/share/zoneinfo/"
  refTime <- currentReftime tzs tz
  let
    context = Context
      { referenceTime = refTime
      , lang = lang
      }
  return $ parse text context [This Time]

Now if we did parseText "in 2 minutes" "UTC" EN

You will get

[Entity {dim = "time", body = "in 2 minutes", value = Object (fromList [("values",Array [Object (fromList [("value",String "2017-09-01T19:51:15.246+00:00"),("grain",String "second"),("type",String "value")])]),("value",String "2017-09-01T19:51:15.246+00:00"),("grain",String "second"),("type",String "value")]), start = 0, end = 12}]

from duckling.

nsanthanam avatar nsanthanam commented on April 24, 2024 1

Excellent thanks for the feedback, I was wondering why the code didn't work. From what you're saying, I can do something like:

parseText :: Text -> Text -> Locale -> IO [Entity]
parseText text tz locale = do
  tzs <- loadTimeZoneSeries "/usr/share/zoneinfo/"
  refTime <- currentReftime tzs tz
  let
    context = Context
      { referenceTime = refTime
      , locale = locale
      }
  return $ parse text context [This Time]

Here I'm assuming that locale is of type Locale rather than constructing Locale from lang and region. Is this correct?

How would I have to change my input to reflect this?

Thanks so much for all the help so far, really appreciate it.

from duckling.

nsanthanam avatar nsanthanam commented on April 24, 2024

HI @stopachka, I compiled the code that you provided using :l parseText.hs (where parseText.hs is the filename, stored in the Duckling directory) in the stack repl (loaded with stack repl --no-load) and I see the following error:

[316 of 319] Compiling Duckling.Rules   ( /Users/navaneethan/Documents/freshML/duckling/Duckling/Rules.hs, interpreted )
[317 of 319] Compiling Duckling.Api     ( /Users/navaneethan/Documents/freshML/duckling/Duckling/Api.hs, interpreted )
[318 of 319] Compiling Duckling.Core    ( /Users/navaneethan/Documents/freshML/duckling/Duckling/Core.hs, interpreted )
[319 of 319] Compiling Duckling.TimezoneExample ( parseText.hs, interpreted )
parseText.hs:16:9: error: Not in scope: β€˜lang’
Failed, modules loaded: Duckling.AmountOfMoney.BG.Rules, ...
...
...

It appears that the line , lang = lang (line 16) is causing the problem, but I don't understand Haskell well at all, so I'm not sure.

Could you help understand what the issue is?

Steps followed:
MacBook-Pro-4:Duckling navaneethan$ stack repl --no-load
Prelude> :l parseText.hs

from duckling.

nsanthanam avatar nsanthanam commented on April 24, 2024

Hi @stopachka, I think I've managed to figure out the issue and wanted to see if this was the right way to fix it.

module Duckling.TimezoneExample
  ( parseText ) where

import Data.Text (Text)


import Duckling.Core
import Duckling.Data.TimeZone
import Duckling.Locale

parseText :: Text -> Text -> Lang -> Maybe Region -> IO [Entity]
parseText text tz lang region = do
  tzs <- loadTimeZoneSeries "/usr/share/zoneinfo/"
  refTime <- currentReftime tzs tz
  let
    context = Context
      { referenceTime = refTime
      , locale = Locale lang (region)
      }
  return $ parse text context [This Time]

I made two changes

  1. Line 11: I changed parseText :: Text -> Text -> Lang -> IO [Entity] to parseText :: Text -> Text -> Lang -> Maybe Region -> IO [Entity].
  2. Line 18: I changed , lang = lang to , locale = Locale lang (region)

The reason (I think) this works is because Context consists of a referenceTime and a locale, not a lang. And locale is composed of a lang and region. I don't yet understand how Maybe works, though.

Now the code compiles and when I present it with timezones of the format Asia/Tbilisi or America/Chicago, it parses the string and returns the time relative to that timezone.

Could you tell me if this is the correct way of doing things? Also, I'm guessing I can expose parseText as an API endpoint, but wanted to check.

from duckling.

patapizza avatar patapizza commented on April 24, 2024

Hi @nsanthanam, since then we've implemented locales (this is why @stopachka's example isn't working anymore). What you did works, though I would suggest to have parseText directly accepts a Locale. For an example on how to wrap this into a server, you can look into exe/ExampleMain.hs.

from duckling.

patapizza avatar patapizza commented on April 24, 2024

That's correct, you'd have to move the locale construction from the function to the callsite.

from duckling.

ydshieh avatar ydshieh commented on April 24, 2024

Hi @nsanthanam, since then we've implemented locales (this is why @stopachka's example isn't working anymore). What you did works, though I would suggest to have parseText directly accepts a Locale. For an example on how to wrap this into a server, you can look into exe/ExampleMain.hs.

@stopachka, Do you know if the server duckling-example-exe in exe/ExampleMain.hs already support specifying timezone and how? I can't find information I can use.

from duckling.

patapizza avatar patapizza commented on April 24, 2024

@chiapas Yes, through the tz parameter.

from duckling.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.