From 5d85c1a5e7fbe3f06667982bf1a8f1353921a76f Mon Sep 17 00:00:00 2001 From: Morgan Astra Date: Tue, 20 Sep 2016 18:04:16 -0700 Subject: [PATCH] allow ellipses to shorten unambiguous path, e.g. /they/.../themself per #46 --- resources/pronouns.tab | 1 + src/pronouns/util.clj | 32 +++++++++++++++++++++++--------- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/resources/pronouns.tab b/resources/pronouns.tab index 64ccbbb..b6e00dd 100644 --- a/resources/pronouns.tab +++ b/resources/pronouns.tab @@ -3,6 +3,7 @@ ze zir zir zirs zirself she her her hers herself he him his his himself they them their theirs themselves +they them their theirs themself xey xem xyr xyrs xemself sie hir hir hirs hirself it it its its itself 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"