2

我在 freebase 上从这里获取了一些 json (小心,你只能请求几次而不使用&key=your-key)。

我想将响应转换为类似于此的内容:

    ({:case "Roe v. Wade", :plaintiffs ("Norma McCorvey"), :defendants ("Henry Wade"), :court "Supreme Court of the United States", :subjects ("Abortion" "Privacy"), :article "http://wp/en/68493"} ...)

这是我在使用 clojure.data.json/read-string 后想出的代码:

    (defn extract-data [case]
      {:case (case "name")
       :plaintiffs (flatten (map #(get % "parties") (filter (fn [p] (some #(= (% "id") "/en/plaintiff") (p "role")))
                                                           (case "/law/legal_case/parties"))))
       :defendants (flatten (map #(get % "parties") (filter (fn [p] (some #(= (% "id") "/en/defendant") (p "role")))
                                                           (case "/law/legal_case/parties"))))
       :court (get-in case ["court" 0 "name"])
       :subjects (map #(% "name") (case "subject"))
       :article (get-in case ["/common/topic/article" 0 "source_uri" 0])})

    (def response (-> query-uri
                       java.net.URL.
                       slurp
                       json/read-str))
    (def case-data (map extract-data (response "result")))

提取数据似乎过于复杂,有没有更好的方法来做到这一点?这是可以使用 core.logic 的情况吗?如果是这样,怎么做?

4

2 回答 2

2

您可能会查看不同的查询系统(zip-filters、core.logic、datomic 的集合数据日志等)。或者推出您自己的特别版:

(defn select [x path]
  (if-let [[p & ps] (seq path)]
    (if (fn? p)
      (mapcat #(select % ps) (filter p x))
      (recur (get x p) ps))
    x))

(def mapping
  {:case ["name"]
   :plaintiffs ["/law/legal_case/parties"
                #(= (get-in % ["role" 0 "id" 0]) "/en/plaintiff")
                "parties"]
   :defendants ["/law/legal_case/parties"
                #(= (get-in % ["role" 0 "id" 0]) "/en/defendant")
                "parties"]
   :court ["court" 0 "name" 0]
   :subjects ["subject" (constantly true) "name"]
   :article ["/common/topic/article" 0 "source_uri" 0]})

(defn extract-data [x mapping]
  (into {}
    (for [[k path] mapping]
      [k (if (some fn? path) (select x path) (get-in x path))])))

然后 (map #(extract-data % mapping) results) 应该可以解决问题

=> (extract-data (first result) mapping)
{:case "Roe v. Wade", :plaintiffs ("Norma McCorvey"), :defendants ("Henry Wade"), :court "Supreme Court of the United States", :subjects ("Abortion" "Privacy"), :article "http//wp/en/68493"}

这种类型的代码(查询解释器)可能很脆弱,所以一定要有一个测试套件。

于 2012-12-07T14:38:04.490 回答
0

幽灵可以提供帮助。

(ns stackoverflow-answer.core
  (:require [com.rpl.specter :refer :all]))

; your example data
(def case {"name" "Roe v. Wade"
           "/law/legal_case/parties" [{"role" [{"id" "/en/plaintiff"}]
                                       "parties" ["Norma McCorvey"]}
                                      {"role" [{"id" "/en/defendant"}]
                                       "parties" ["Henry Wade"]}]
           "court" [{"name" "Supreme Court of the United States"}]
           "subject" [{"name" "Abortion"}
                      {"name" "Privacy"}]
           "/common/topic/article" [{"source_uri" ["http://wp/en/68493"]}]})

(defn specter-extract [case]
  {:case (case "name")
   :plaintiffs (select ["/law/legal_case/parties"
                        ALL
                        (selected? "role" ALL "id" (pred= "/en/plaintiff"))
                        "parties"
                        ALL]
                       case)
   ; etc
   })

用法:

stackoverflow-answers.core=> (specter-extract case)
{:case "Roe v. Wade", :plaintiffs ["Norma McCorvey"]}
于 2019-07-05T10:18:48.243 回答