我試圖在Clojure中構建一個POS tagger。我需要迭代一個文件並構建特徵向量。的輸入是(文本POS塊)從如下所示的三倍文件:如何將一系列映射序列拼合成一系列向量?
input from the file:
I PP B-NP
am VBP B-VB
groot NN B-NP
我寫功能,以輸入該文件,變換每一行到地圖,然後通過數據可變量的滑動。
(defn lazy-file-lines
"open a file and make it a lazy sequence."
[filename]
(letfn [(helper [rdr]
(lazy-seq
(if-let [line (.readLine rdr)]
(cons line (helper rdr))
(do (.close rdr) nil))))]
(helper (clojure.java.io/reader filename))))
(defn to-map
"take a a line from a file and make it a map."
[lines]
(map
#(zipmap [:text :pos :chunk] (clojure.string/split (apply str %) #" "))lines)
)
(defn window
"create windows around the target word."
[size filelines]
(partition size 1 [] filelines))
我打算使用上述功能通過以下方式:
(take 2 (window 3(to-map(lazy-file-lines "/path/to/train.txt"))))
其給出對於序列中的前兩個條目的輸出如下:
(({:chunk B-NP, :pos NN, :text Confidence} {:chunk B-PP, :pos IN, :text in} {:chunk B-NP, :pos DT, :text the}) ({:chunk B-PP, :pos IN, :text in} {:chunk B-NP, :pos DT, :text the} {:chunk I-NP, :pos NN, :text pound}))
鑑於每個序列我想爲每個地圖提取:pos
和:text
,並將它們放在一個向量中。像這樣:
[Confidence in the NN IN DT]
[in the pound IN DT NN]
我還沒有能夠概念化如何在clojure中處理這個問題。我的部分嘗試的解決方案是如下:
(defn create-features
"creates the features and tags from the datafile."
[filename windowsize & features]
(map #(apply select-keys % [:text :pos])
(->>
(lazy-file-lines filename)
(window windowsize))))
我想到的一個問題是,申請被引用序列本身,所以選擇密鑰是不是一個地圖上進行操作。不過,我不確定如何嵌套另一個應用函數。
對此代碼的任何想法都會很棒。謝謝。
如果你的問題實際上只是關於如何拼接一系列地圖序列,那麼前兩個代碼塊和地圖目的的描述等等,只是混淆了這個問題。不相關的信息使得你很快就能得到答案。在這個特定的問題中,給出一個你想要作爲輸入處理的地圖序列序列的例子,以及你想要輸出的例子。 (如果您不確定額外材料是否相關,請解釋原因 - 在這種情況下,這是問題的一部分。) – Mars
我認爲您實際需要的不僅僅是展平操作,而是通過按鍵選擇然後變平。 – Mars
'to-map'永遠不會被使用........?應該如何理解這裏提出的問題? windowsize的目的是什麼?輸入與「超級基本輸出」有什麼關係?你想解決什麼問題? –