Skip to content

Instantly share code, notes, and snippets.

@veer66
Created February 17, 2020 09:15
Show Gist options
  • Select an option

  • Save veer66/5cde677941c93ac09e1e52cdbb402846 to your computer and use it in GitHub Desktop.

Select an option

Save veer66/5cde677941c93ac09e1e52cdbb402846 to your computer and use it in GitHub Desktop.
(require '[clojure.data.xml :as xml]
'[clojure.string :as str]
'[clojure.java.io :as io])
(defn ext-segs [segs]
(->> segs
(mapcat :content)
(str/join " ")))
(defn ext-tuv [tuv]
[(-> tuv
:attrs
:xml/lang
keyword)
(->> tuv
:content
(filter #(= (:tag %) :seg))
ext-segs)])
(defn ext-tu [tu]
(->> tu
:content
(filter #(= (:tag %) :tuv))
(mapcat ext-tuv)
(apply array-map)))
(defn extract [path]
(let [doc (xml/parse path)
body (-> doc :content (nth 1) :content)
tu-list (->> body
(filter #(= (:tag %) :tu)))]
(map ext-tu tu-list)))
(defn valid-pair? [p]
(and (> (count (:en p)) 0)
(> (count (:th p)) 0)
(not (re-matches #"^\s+$" (:en p)))
(not (re-matches #"^\s+$" (:th p)))))
(with-open [w-en (io/writer "open.en")
w-th (io/writer "open.th")]
(doseq [path ["opensub/en-th.tmx"
"gnome/en-th.tmx"]]
(with-open [r (io/reader path)]
(->> (extract r)
(filter valid-pair?)
(map #(do
(prn %)
(.write w-en (:en %))
(.write w-en "\n")
(.write w-th (:th %))
(.write w-th "\n")))
doall))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment