Created
February 17, 2020 09:15
-
-
Save veer66/5cde677941c93ac09e1e52cdbb402846 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (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