【发布时间】:2015-05-24 04:14:36
【问题描述】:
我正在尝试在 Clojure 中构建一个 POS 标记器。我需要遍历文件并构建特征向量。输入是来自文件的 (text pos chunk) 三元组,如下所示:
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))))
我认为其中一个问题是 apply 引用了一个序列本身,因此 select-keys 不在地图上运行。不过,我不确定如何在其中嵌套另一个应用函数。
对此代码的任何想法都会很棒。谢谢。
【问题讨论】:
-
如果你的问题真的只是关于如何展平一系列地图,那么前两个代码块和地图用途的描述等,只是把问题弄乱了。不相关的信息会降低您快速得到答案的可能性。在这个特定的问题中,举例说明您尝试将其作为输入处理的地图序列的序列类型以及您想要作为输出的说明,这将很有帮助。 (如果您不确定额外的材料是否相关,请解释原因——在这种情况下,这是问题的一部分。)
-
我认为您真正想要的不仅仅是展平操作,而是按键选择然后展平。
-
to-map从未使用过......?应该如何理解这里的要求?windowsize的目的是什么?输入与“超基本输出”有何关系?你想解决什么问题? -
哎呀。我澄清了这个问题,包括我实际在做什么。
标签: clojure