From 42cccac9dc661c9692b4cbec5fcc9d40b2613a8e Mon Sep 17 00:00:00 2001 From: Attila Egri-Nagy Date: Wed, 20 Sep 2023 08:24:10 +0900 Subject: [PATCH] starting to work on the recognizer --- experiments/TRANSDUCERS/automata.clj | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/experiments/TRANSDUCERS/automata.clj b/experiments/TRANSDUCERS/automata.clj index 1f6ea9a..e6dfc10 100644 --- a/experiments/TRANSDUCERS/automata.clj +++ b/experiments/TRANSDUCERS/automata.clj @@ -4,6 +4,8 @@ ;;TODO write a function that checks the io-pairs for contradicting pairs ;; like the same word requiring two different outputs +(def stop \⏹) + (defn proper-prefixes "All proper prefixes of the given word, starting from the empty word." [coll] @@ -19,4 +21,27 @@ inputs." [io-pairs] (update-vals (group-by second io-pairs) - (partial map first))) \ No newline at end of file + (partial map first))) + +(defn firsts-in-trie + "Returns a set of the elements that appear the first positions in the + branches." + [trie] + (cond + (empty? trie) #{} + (vector? (first trie)) (into #{} (map first (first trie))) + :else #{(first trie)})) + +(defn recognizer + "Returns a recognizer FA for the trie." + ([trie] (recognizer trie 0 {} #{:accept})) + ([trie state delta acceptors] + (let [elts (take-while (fn [x] (not (vector? x))) trie) + (reduce (fn []) + delta)] + (if (= (count trie) (count elts)) + (conj result new-so-far) + (reduce + (fn [r v] (retrieve v new-so-far r)) + result + (last trie)))))) \ No newline at end of file