4

我正在尝试读取一个大文本文件并计算特定错误的出现次数。例如,对于以下示例文本

something
bla
error123
foo
test
error123
line
junk
error55
more
stuff

我想结束(虽然我在考虑地图,但并不关心什么数据结构)

error123 - 2
error55 - 1

这是我到目前为止尝试过的

(require '[clojure.java.io :as io])

(defn find-error [line]
  (if (re-find #"error" line)    
       line))


(defn read-big-file [func, filename]
 (with-open [rdr (io/reader filename)]
   (doall (map func (line-seq rdr)))))  

像这样称呼它

 (read-big-file find-error "sample.txt")

返回:

(nil nil "error123" nil nil "error123" nil nil "error55" nil nil)

接下来,我尝试删除 nil 值并将类似项目分组

(group-by identity (remove #(= nil %) (read-big-file find-error "sample.txt")))

返回

{"error123" ["error123" "error123"], "error55" ["error55"]}

这正在接近所需的输出,尽管它可能效率不高。我现在怎样才能得到计数?此外,作为 clojure 和函数式编程的新手,我将不胜感激有关如何改进这一点的任何建议。谢谢!

4

1 回答 1

7

我想你可能正在寻找频率函数:

user=> (doc frequencies)
-------------------------
clojure.core/frequencies
([coll])
  Returns a map from distinct items in coll to the number of times
  they appear.
nil

所以,这应该给你你想要的:

(frequencies (remove nil? (read-big-file find-error "sample.txt")))
;;=> {"error123" 2, "error55" 1}

但是,如果您的文本文件非常大,我建议line-seq您在内联中执行此操作,以确保您不会耗尽内存。这样你也可以使用 afilter而不是mapand remove

(defn count-lines [pred, filename]
  (with-open [rdr (io/reader filename)]
    (frequencies (filter pred (line-seq rdr)))))

(defn is-error-line? [line]
  (re-find #"error" line))

(count-lines is-error-line? "sample.txt")
;; => {"error123" 2, "error55" 1}
于 2013-06-23T02:43:55.277 回答