diff options
author | Morgan Astra <m@morganastra.me> | 2016-09-20 18:04:16 -0700 |
---|---|---|
committer | Morgan Astra <m@morganastra.me> | 2016-09-20 18:04:16 -0700 |
commit | 5d85c1a5e7fbe3f06667982bf1a8f1353921a76f (patch) | |
tree | e70c5e2118e1f657d459620a600c1e6c49fd65dd /src/pronouns/util.clj | |
parent | 93c37971682cbcbda3139c7e8a47ca21311f8947 (diff) |
allow ellipses to shorten unambiguous path, e.g. /they/.../themself per #46
Diffstat (limited to 'src/pronouns/util.clj')
-rw-r--r-- | src/pronouns/util.clj | 32 |
1 files changed, 23 insertions, 9 deletions
diff --git a/src/pronouns/util.clj b/src/pronouns/util.clj index 5aa9530..5086634 100644 --- a/src/pronouns/util.clj +++ b/src/pronouns/util.clj @@ -21,26 +21,40 @@ (let [lines (s/split (slurp path) #"\n")] (map #(s/split % #"\t") lines))) +(defn table-front-filter + [query-key table] + (let [arity (count query-key)] + (filter #(= query-key (take arity %)) table))) + +(defn table-end-filter + [query-key table] + (let [table-arity (count (first table)) + query-arity (count query-key)] + (filter #(= query-key (drop (- table-arity query-arity) %)) table))) + (defn table-lookup [query-key table] - (let [arity (count query-key) - filtered-table (filter #(= query-key (take arity %)) table)] - (first filtered-table))) + (if (some #(= "..." %) query-key) + (let [[query-front query-end-] (split-with #(not= "..." %) query-key) + query-end (drop 1 query-end-) + front-matches (table-front-filter query-front table)] + (first (table-end-filter query-end front-matches))) + (first (table-front-filter query-key table)))) (defn tabfile-lookup [query-key tabfile] (table-lookup query-key (slurp-tabfile tabfile))) (defn minimum-unambiguous-path - ([table sections] (minimum-unambiguous-path table sections 1)) - ([table sections number-of-sections] - (let [sections-subset (take number-of-sections sections) - results (filter #(= (take number-of-sections %) sections-subset) + ([table columns] (minimum-unambiguous-path table columns 1)) + ([table columns number-of-columns] + (let [columns-subset (take number-of-columns columns) + results (filter #(= (take number-of-columns %) columns-subset) table)] (case (count results) 0 nil - 1 (clojure.string/join "/" sections-subset) - (recur table sections (+ number-of-sections 1)))))) + 1 (clojure.string/join "/" columns-subset) + (recur table columns (+ number-of-columns 1)))))) (defn abbreviate "given a list of pronoun rows, return a list of minimum unabiguous paths" |