1

我正在使用 wit.ai 的 Duckling ( https://duckling.wit.ai/ ),但是我依赖并从我的 Java 应用程序中调用 Duckling。我没有 Clojure 经验...

我能够运行 Duckling 的 parse 方法,但是我不知道如何传递日期/时间以用作时间和日期解析的上下文。

这是功能:

(defn parse
  "Public API. Parses text using given module. If dims are provided as a list of
  keywords referencing token dimensions, only these dimensions are extracted.
  Context is a map with a :reference-time key. If not provided, the system
  current date and time is used."
  ([module text]
   (parse module text []))
  ([module text dims]
   (parse module text dims (default-context :now)))
  ([module text dims context]
   (->> (analyze text context module (map (fn [dim] {:dim dim :label dim}) dims) nil)
        :winners
        (map #(assoc % :value (engine/export-value % {})))
        (map #(select-keys % [:dim :body :value :start :end :latent])))))

在测试语料库中,它在文件顶部有上下文日期。这在测试语料库时被传递到 parse 函数中。

{:reference-time (time/t -2 2013 2 12 4 30 0)
   :min (time/t -2 1900)
   :max (time/t -2 2100)}

这是我的Java代码:

public void extract(String input) {
    IFn require = Clojure.var("clojure.core", "require");
    require.invoke(Clojure.read("duckling.core"));
    Clojure.var("duckling.core", "load!").invoke();
    LazySeq o = (LazySeq) Clojure.var("duckling.core", "parse").invoke("en$core", input, dims);
}

我的问题是,如何将特定的日期/时间作为参数插入到解析函数中?

编辑 1再看一遍,看起来这是一个日期时间对象。Duckling 依赖于 clj-time 0.8.0,但是我不知道如何通过调用 clj-time 在 Java 中创建相同的对象。

4

1 回答 1

1

Duckling 在duckling.time.obj 命名空间中有自己的日期时间辅助函数('t'),它驱动我如何获得它所期望的相同日期时间对象。

private final Keyword REFERENCE_TIME = Keyword.intern("reference-time");
private final Keyword MIN = Keyword.intern("min");
private final Keyword MAX = Keyword.intern("max");

public void extract(String input) {

    PersistentArrayMap datemap = (PersistentArrayMap) Clojure.var("duckling.time.obj", "t").invoke(-5, 2017, 2, 21, 23, 30, 0);
    PersistentArrayMap minMap = (PersistentArrayMap) Clojure.var("duckling.time.obj", "t").invoke(-5, 1900);
    PersistentArrayMap maxMap = (PersistentArrayMap) Clojure.var("duckling.time.obj", "t").invoke(-5, 2100);
    Object[] contextArr = new Object[6];
    contextArr[0] = REFERENCE_TIME;
    contextArr[1] = datemap;
    contextArr[2] = MIN;
    contextArr[3] = minMap;
    contextArr[4] = MAX;
    contextArr[5] = maxMap;
    PersistentArrayMap cljContextMap = PersistentArrayMap.createAsIfByAssoc(contextArr);

    LazySeq results = (LazySeq) Clojure.var("duckling.core", "parse").invoke("en$core", input, dims, cljContextMap);
}

剩下要做的就是使用动态值而不是硬编码创建日期图。

于 2017-02-22T19:48:12.767 回答