|
1 | 1 | (ns init
|
2 | 2 | (:require
|
| 3 | + [babashka.fs :as fs] |
3 | 4 | [cheshire.core :as json]
|
4 | 5 | [clojure.java.io :as io]
|
5 | 6 | [clojure.string :as string]))
|
|
17 | 18 | (try
|
18 | 19 | (->> (line-seq (io/reader db-file))
|
19 | 20 | (reduce item {}))
|
20 |
| - (catch Throwable t |
| 21 | + (catch Throwable _ |
21 | 22 | {:entities []
|
22 | 23 | :relations []})))
|
23 | 24 |
|
|
35 | 36 | (string/join "\n"))))
|
36 | 37 |
|
37 | 38 | (comment
|
| 39 | + (fs/delete db-file) |
38 | 40 | (save-graph
|
39 |
| - (load-graph))) |
| 41 | + (load-graph))) |
40 | 42 |
|
41 | 43 | (defn- entity? [graph e]
|
42 | 44 | (some #(= (:name e) (:name %)) (:entities graph)))
|
|
67 | 69 | new-relations (->> relations
|
68 | 70 | (filter
|
69 | 71 | (complement
|
70 |
| - (partial relation? graph))))] |
| 72 | + (partial relation? graph))))] |
71 | 73 | (save-graph (update graph :relations (fnil concat []) new-relations))
|
72 | 74 | new-relations))
|
73 | 75 |
|
|
76 | 78 | (load-graph))
|
77 | 79 |
|
78 | 80 | (defn- add-observation [agg {:keys [entityName contents]}]
|
79 |
| - (if-let [entity (first (filter #(= entityName (:name %)) (-> agg :graph :entities)))] |
80 |
| - (let [new-observations [] |
81 |
| - n 0] |
| 81 | + (if-let [[n entity] (some |
| 82 | + (fn [[_ m :as v]] (when (= entityName (:name m)) v)) |
| 83 | + (->> (-> agg :graph :entities) |
| 84 | + (interleave (range)) |
| 85 | + (partition 2)))] |
| 86 | + (let [new-observations (->> contents |
| 87 | + (filter (complement #(some (partial = %) (:observations entity)))))] |
82 | 88 | (-> agg
|
83 | 89 | (update :results conj {:entityName entityName :addedObservations new-observations})
|
84 |
| - (update :graph (update-in [:entities n :observations] (fnil concat []) new-observations)))) |
| 90 | + (update :graph update-in [:entities n :observations] (fnil concat []) new-observations))) |
85 | 91 | agg))
|
86 | 92 |
|
| 93 | +(comment |
| 94 | + (add-observation |
| 95 | + {:graph {:entities [{:name "me"} {:name "rod"}]}} |
| 96 | + {:entityName "me" :contents ["fact"]})) |
| 97 | + |
87 | 98 | ;; observations are a list of entityName contents maps - contents are string arrays
|
88 | 99 | (defn add-observations [{:keys [observations]}]
|
89 | 100 | (let [graph (load-graph)
|
90 |
| - {:keys [results graph]} |
91 |
| - (->> observations |
92 |
| - (reduce |
93 |
| - add-observation |
94 |
| - {:graph graph :results []}))] |
| 101 | + {:keys [results graph]} (->> observations |
| 102 | + (reduce |
| 103 | + add-observation |
| 104 | + {:graph graph :results []}))] |
95 | 105 | (save-graph graph)
|
96 | 106 | results))
|
97 | 107 |
|
98 | 108 | (comment
|
99 |
| - (add-observations |
100 |
| - {:observations |
101 |
| - [{ :entityName "me" :contents [ "my personal email is [email protected]"]} |
102 |
| - {:entityName "rod" :contents ["Rod is in Sydney, Australia"]}]}) |
| 109 | + (add-observations |
| 110 | + {:observations |
| 111 | + [{ :entityName "me" :contents [ "my personal email is [email protected]"]} |
| 112 | + {:entityName "rod" :contents ["Rod is in Sydney, Australia"]}]}) |
103 | 113 | (load-graph))
|
104 | 114 |
|
105 | 115 | (defn delete-entities [{:keys [entityNames]}]
|
106 |
| - (save-graph |
107 |
| - (-> (load-graph) |
108 |
| - (update :entities (fn [entities] (filter entities))) |
109 |
| - (update :relations (fn [relations] (filter relations)))))) |
| 116 | + (let [entity? (into #{} entityNames) |
| 117 | + relation? (fn [{:keys [from to]}] (or (entity? from) (entity? to)))] |
| 118 | + (save-graph |
| 119 | + (-> (load-graph) |
| 120 | + (update :entities (fn [coll] (filter (complement #(entity? (:name %))) coll))) |
| 121 | + (update :relations (fn [coll] (filter (complement #(relation? %)) coll))))))) |
| 122 | + |
| 123 | +(comment |
| 124 | + (do |
| 125 | + (fs/delete db-file) |
| 126 | + (create-entities {:entities [{:name "me"} {:name "rod"}]}) |
| 127 | + (create-relations {:relations [{:from "me" :to "rod" :relationType "friend"}]}) |
| 128 | + (add-observations |
| 129 | + {:observations |
| 130 | + [{ :entityName "me" :contents [ "my personal email is [email protected]"]} |
| 131 | + {:entityName "rod" :contents ["Rod is in Sydney, Australia"]}]}) |
| 132 | + (load-graph)) |
| 133 | + |
| 134 | + (delete-entities {:entityNames ["rod"]}) |
| 135 | + (load-graph)) |
| 136 | + |
| 137 | +(defn delete-observation [coll {:keys [entityName observations]}] |
| 138 | + (->> coll |
| 139 | + (map (fn [m] |
| 140 | + (if (= entityName (:name m)) |
| 141 | + (update m :observations (fn [obs] (remove (into #{} observations) obs))) |
| 142 | + m))))) |
| 143 | + |
110 | 144 | (defn delete-observations [{:keys [deletions]}]
|
111 | 145 | (save-graph
|
112 | 146 | (-> (load-graph)
|
113 |
| - (update :entities (fn [entities]))))) |
| 147 | + (update :entities (fn [entities] (reduce delete-observation entities deletions)))))) |
| 148 | + |
| 149 | +(comment |
| 150 | + (do |
| 151 | + (fs/delete db-file) |
| 152 | + (create-entities {:entities [{:name "me"} {:name "rod"}]}) |
| 153 | + (create-relations {:relations [{:from "me" :to "rod" :relationType "friend"}]}) |
| 154 | + (add-observations |
| 155 | + {:observations |
| 156 | + [{ :entityName "me" :contents [ "my personal email is [email protected]"]} |
| 157 | + {:entityName "rod" :contents ["Rod is in Sydney, Australia"]}]}) |
| 158 | + (load-graph)) |
| 159 | + |
| 160 | + (delete-observations {:deletions [{:entityName "rod" :observations ["Rod is in Sydney, Australia"]}]}) |
| 161 | + (load-graph)) |
| 162 | + |
| 163 | +(defn delete-relation [coll {:keys [from to relationType]}] |
| 164 | + (remove (fn [m] (and |
| 165 | + (= from (:from m)) |
| 166 | + (= to (:to m)) |
| 167 | + (= relationType (:relationType m)))) coll)) |
| 168 | + |
114 | 169 | (defn delete-relations [{:keys [relations]}]
|
115 | 170 | (save-graph
|
116 | 171 | (-> (load-graph)
|
117 |
| - (update :relations (fn [entities]))))) |
118 |
| -(defn search-nodes [{:keys [query]}]) |
119 |
| -(defn open-nodes [{:keys [names]}]) |
| 172 | + (update :relations (fn [coll] (reduce delete-relation coll relations)))))) |
| 173 | + |
| 174 | +(defn entity-matches? [q entity] |
| 175 | + (or |
| 176 | + (string/includes? (-> entity :name string/lower-case) q) |
| 177 | + (try (string/includes? (-> entity :entityType string/lower-case) q) (catch Throwable _ false)) |
| 178 | + (try (some #(string/includes? (string/lower-case %) q) (:observations entity)) (catch Throwable _ false)))) |
| 179 | + |
| 180 | +(defn search-nodes [{:keys [query]}] |
| 181 | + (let [graph (load-graph) |
| 182 | + filtered-entities (filter (partial entity-matches? (string/lower-case query)) (:entities graph)) |
| 183 | + filtered-entity-names (->> filtered-entities (map :name) (into #{}))] |
| 184 | + {:entities filtered-entities |
| 185 | + :relations (filter |
| 186 | + #(and (filtered-entity-names (:from %)) (filtered-entity-names (:to %))) |
| 187 | + (:relations graph))})) |
| 188 | + |
| 189 | +(defn open-nodes [{:keys [names]}] |
| 190 | + (let [graph (load-graph) |
| 191 | + entity-names (into #{} names) |
| 192 | + filtered-entities (filter (comp entity-names :name) (:entities graph)) |
| 193 | + filtered-entity-names (->> filtered-entities (map :name) (into #{}))] |
| 194 | + {:entities filtered-entities |
| 195 | + :relations (filter |
| 196 | + #(and (filtered-entity-names (:from %)) (filtered-entity-names (:to %))) |
| 197 | + (:relations graph))})) |
| 198 | + |
| 199 | +(comment |
| 200 | + (do |
| 201 | + (fs/delete db-file) |
| 202 | + (create-entities {:entities [{:name "me"} {:name "rod"}]}) |
| 203 | + (create-relations {:relations [{:from "me" :to "rod" :relationType "friend"}]}) |
| 204 | + (add-observations |
| 205 | + {:observations |
| 206 | + [{ :entityName "me" :contents [ "my personal email is [email protected]"]} |
| 207 | + {:entityName "rod" :contents ["Rod is in Sydney, Australia"]}]}) |
| 208 | + (load-graph)) |
| 209 | + |
| 210 | + (search-nodes {:query "rod"}) |
| 211 | + (open-nodes {:names ["me" "rod"]}) |
| 212 | + (load-graph)) |
120 | 213 |
|
121 | 214 | (try
|
122 | 215 | (let [[s raw-json-string] *command-line-args*
|
|
0 commit comments