2017-02-22 34 views
1

我與wit.ai的小鴨(https://duckling.wit.ai/)工作中傳遞參數到小鴨Clojure的功能,但是我依賴於從我的Java應用程序中調用小鴨。我沒有Clojure的經驗...從Java應用程序

我能夠運行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); 
} 

我的問題是,我怎麼在插入一個特定的日期/時間作爲參數解析功能?

編輯1再看看它,它看起來像這是一個日期時間對象。小鴨取決於CLJ-時間0.8.0,但我無法弄清楚如何呼喚CLJ-時間在Java中創建相同的對象。

回答

1

小鴨已在duckling.time.obj命名空間,驅動我如何得到同樣的DateTime對象,它的預計了自己的datetime輔助函數(「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); 
} 

唯一剩下要做的就是用動態值而不是硬編碼創建日期圖。