2017-02-22 2 views
1

私はwit.aiのDuckling(https://duckling.wit.ai/)と一緒に作業していますが、私はJavaアプリケーション内からDucklingを呼び出して呼び出しています。私はClojureの経験はありません...Javaアプリケーション内からDuckling Clojure関数にパラメータを渡す

私はDucklingの解析メソッドを実行することができますが、日付と時刻をコンテキストに渡して時間と日付の解決に使用する方法を理解することはできません。それはファイルの先頭にコンテキスト日付を持って、テストコーパスで

(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]))))) 

:ここ

機能です。これは、コーパスのテスト中に解析関数に渡されます。

{: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); 
} 

私の質問は、私はparse関数へのパラメータとしてでは特定の日付/時刻を挿入しますどのように、ありますか?

EDIT 1もう少し見ると、これはdatetimeオブジェクトのようです。 Ducklingはclj-time 0.8.0に依存していますが、clj-timeを呼び出してJavaで同じオブジェクトを作成する方法を理解することはできません。

答えて

1

Ducklingはduckling.time.obj名前空間に独自のdatetimeヘルパー関数( 't')を持っています。この関数は、期待しているものと同じdatetimeオブジェクトを取得します。

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); 
} 

残っていることは、ハードコードされているのではなく、動的値でデータマップを作成することだけです。

関連する問題