Compare commits

..

8 Commits

8 changed files with 311 additions and 104 deletions

6
.dir-locals.el Normal file
View File

@@ -0,0 +1,6 @@
((nil
. ((eval
. (progn (add-to-list 'load-path (project-root (project-current)))
(require 'helpers)
(add-hook 'before-save-hook
#'hangul-convert-buffer-to-jamo t))))))

View File

@@ -33,6 +33,7 @@
inputs.gf.packages.${system}.gf-with-rgl inputs.gf.packages.${system}.gf-with-rgl
pkgs.graphviz pkgs.graphviz
gf-lsp.gf-lsp gf-lsp.gf-lsp
pkgs.babashka
]; ];
}; };
}); });

56
helpers.el Normal file
View File

@@ -0,0 +1,56 @@
(defun hangul-syllables-to-jamo (str)
"Convert HANGUL SYLLABLES characters in STR to their HANGUL JAMO
equivalents."
(let ((result "")
(i 0))
(while (< i (length str))
(let ((char (aref str i)))
(if (and (>= char #xAC00) (<= char #xD7A3))
;; Hangul syllable
(let* ((code (- char #xAC00))
(lead (/ code (* 21 28)))
(medial (/ (% code (* 21 28)) 28))
(final (% code 28))
(lead-jamo (+ #x1100 lead))
(medial-jamo (+ #x1161 medial))
(final-jamo (if (> final 0) (+ #x11A7 final) nil)))
(setq result
(concat result (char-to-string lead-jamo)
(char-to-string medial-jamo)
(if final-jamo (char-to-string final-jamo) ""))))
;; Not a Hangul syllable
(setq result (concat result (char-to-string char)))))
(setq i (1+ i)))
result))
(defun hangul-convert-region-to-jamo (beg end)
(interactive "r")
(replace-region-contents
beg end (lambda ()
(hangul-syllables-to-jamo (buffer-substring (point-min)
(point-max))))))
(defun hangul-convert-buffer-to-jamo ()
(interactive)
(hangul-convert-region-to-jamo (point-min) (point-max))
(message "Converted Hangul Syllables in buffer to Jamo."))
(require 'dash)
(defconst gf-hangul/choseong
(cl-loop for i from #x1100 to #x1112 collect i))
(defconst gf-hangul/jungseong
(cl-loop for i from #x1161 to #x1175 collect i))
(defconst gf-hangul/batchim
(cl-loop for i from #x11a8 to #x11c2 collect i))
(defun gf-hangul/make-pattern (name seq)
(format "'%s' : pattern Str = #(%s) ;"
name (->> seq
(--map (concat "\"" it "\""))
(-interpose " | ")
(apply #'concat))))
(provide 'helpers)

View File

@@ -0,0 +1,72 @@
resource HangulJamo = open Prelude in {
flags coding=utf8 ;
oper
lemmaToStem : (lemma : Str) -> Str
= \lemma -> case lemma of {
stem + "다" => stem ;
_ => Predef.error ("lemmaToStem was applied to a non-lemma," ++ lemma)
} ;
infinitive : (stem : Str) -> Str
= \stem -> case stem of {
-- 하다 ⇒ 해
init + #ha => init + "해" ;
-- 찾다 ⇒ 찾아, 좁다 ⇒ 좁아
_ + #a_o + #batchim => stem + "아" ;
-- 가다 ⇒ 가,
_ + (#a | #eo) => stem ;
-- 오다 ⇒ 와
init + #o => init + "ᅪ" ;
-- 따르다 ⇒ 따러
init + #eu => init + "ᅥ" ;
-- 기다리다 ⇒ 기다려
init + #i => init + "ᅧ" ;
--
init + #u => init + "ᅯ" ;
-- 맛있다 ⇒ 맛있어
_ => stem + "어"
} ;
present_haeyo : (stem : Str) -> Str
= \stem -> infinitive stem + "요" ;
past_haeyo : (stem : Str) -> Str
= \stem -> infinitive stem + "ᆻ어요" ;
vc_allomorph : (s,vowel,consonant : Str) -> Str
= \s,v,c -> case s of {
_ + #vowel => v ;
_ => c
} ;
oper
a_o : pattern Str = #("ᅡ" | "ᅩ") ;
a : pattern Str = #"ᅡ" ;
o : pattern Str = #"ᅩ" ;
eo : pattern Str = #"ᅥ" ;
eu : pattern Str = #"ᅳ" ;
i : pattern Str = #"ᅵ" ;
u : pattern Str = #"ᅮ" ;
ha : pattern Str = #"하" ;
oper
consonant : pattern Str =
#("ᄀ" | "ᄁ" | "ᄂ" | "ᄃ" | "ᄄ" | "ᄅ" | "ᄆ" | "ᄇ"
| "ᄈ" | "ᄉ" | "ᄊ" | "ᄋ" | "ᄌ" | "ᄍ" | "ᄎ" | "ᄏ"
| "ᄐ" | "ᄑ" | "ᄒ" | "ᆨ" | "ᆩ" | "ᆪ" | "ᆫ" | "ᆬ"
| "ᆭ" | "ᆮ" | "ᆯ" | "ᆰ" | "ᆱ" | "ᆲ" | "ᆳ" | "ᆴ"
| "ᆵ" | "ᆶ" | "ᆷ" | "ᆸ" | "ᆹ" | "ᆺ" | "ᆻ" | "ᆼ"
| "ᆽ" | "ᆾ" | "ᆿ" | "ᇀ" | "ᇁ" | "ᇂ") ;
batchim : pattern Str =
#("ᆨ" | "ᆩ" | "ᆪ" | "ᆫ" | "ᆬ" | "ᆭ" | "ᆮ" | "ᆯ" | "ᆰ"
| "ᆱ" | "ᆲ" | "ᆳ" | "ᆴ" | "ᆵ" | "ᆶ" | "ᆷ" | "ᆸ" | "ᆹ"
| "ᆺ" | "ᆻ" | "ᆼ" | "ᆽ" | "ᆾ" | "ᆿ" | "ᇀ" | "ᇁ" | "ᇂ") ;
choseong : pattern Str =
#("ᄀ" | "ᄁ" | "ᄂ" | "ᄃ" | "ᄄ" | "ᄅ" | "ᄆ" | "ᄇ" | "ᄈ"
| "ᄉ" | "ᄊ" | "ᄋ" | "ᄌ" | "ᄍ" | "ᄎ" | "ᄏ" | "ᄐ" | "ᄑ"
| "ᄒ") ;
vowel : pattern Str =
#("ᅡ" | "ᅢ" | "ᅣ" | "ᅤ" | "ᅥ" | "ᅦ" | "ᅧ" | "ᅨ" | "ᅩ"
| "ᅪ" | "ᅫ" | "ᅬ" | "ᅭ" | "ᅮ" | "ᅯ" | "ᅰ" | "ᅱ" | "ᅲ"
| "ᅳ" | "ᅴ" | "ᅵ") ;
}

View File

@@ -1,6 +1,8 @@
--# -path=.:../abstract --# -path=.:../abstract
concrete MicroLangKor of MicroLang = open MicroResKor, Prelude in { concrete MicroLangKor of MicroLang = open MicroResKor, Prelude in {
flags coding=utf8 ;
----------------------------------------------------- -----------------------------------------------------
---------------- Grammar part ----------------------- ---------------- Grammar part -----------------------
----------------------------------------------------- -----------------------------------------------------
@@ -9,11 +11,11 @@ concrete MicroLangKor of MicroLang = open MicroResKor, Prelude in {
Utt = {s : Str} ; Utt = {s : Str} ;
S = {s : Str} ; S = {s : Str} ;
VP = {verb : Verb ; compl : Str} ; ---s special case of Mini VP = {verb : Verb ; compl : N} ; ---s special case of Mini
Comp = Noun ; Comp = Noun ;
AP = Adjective ; AP = Adjective ;
CN = Noun ; CN = Noun ;
NP = {s : Str} ; NP = Noun ;
Pron = {s : Str} ; Pron = {s : Str} ;
Det = {s : Str} ; Det = {s : Str} ;
Prep = {s : Str} ; Prep = {s : Str} ;
@@ -24,10 +26,10 @@ concrete MicroLangKor of MicroLang = open MicroResKor, Prelude in {
Adv = {s : Str} ; Adv = {s : Str} ;
lin lin
UttS s = s ; UttS s = {s = s.s} ;
UttNP np = {s = np.s} ; UttNP np = {s = np.s ! NBare} ;
-- PredVPS np vp = {s = np.s ++ vp.compl ++ vp.verb.s} ; PredVPS np vp = {s = np.s ! NSubject ++ vp.compl ! NObject ++ vp.verb.s ! VPresent} ;
UseV v = {verb = v ; compl = []} ; UseV v = {verb = v ; compl = {s = \\_ => []}} ;
ComplV2 v2 np = {verb = v2 ; compl = np.s}; ComplV2 v2 np = {verb = v2 ; compl = np.s};
UseN n = n ; UseN n = n ;
DetCN det cn = {s = det.s ++ cn.s} ; DetCN det cn = {s = det.s ++ cn.s} ;
@@ -35,101 +37,103 @@ concrete MicroLangKor of MicroLang = open MicroResKor, Prelude in {
aPl_Det = {s = []} ; aPl_Det = {s = []} ;
the_Det = {s = []} ; the_Det = {s = []} ;
thePl_Det = {s = []} ; thePl_Det = {s = []} ;
this_Det = {s = ""} ; this_Det = {s = "이"} ;
thisPl_Det = {s = ""} ; thisPl_Det = {s = "이"} ;
that_Det = {s = ""} ; that_Det = {s = "그"} ;
thatPl_Det = {s = ""} ; thatPl_Det = {s = "그"} ;
PositA a = a ;
AdjCN ap cn = {s = ap.s ! VAdnomial ++ cn.s} ;
----------------------------------------------------- -----------------------------------------------------
---------------- Lexicon part ----------------------- ---------------- Lexicon part -----------------------
----------------------------------------------------- -----------------------------------------------------
-- lin already_Adv = mkAdv "벌써" ; -- lin already_Adv = mkAdv "벌써" ;
lin animal_N = mkN "동물" ; lin animal_N = mkN "동물" ;
lin apple_N = mkN "사과" ; lin apple_N = mkN "사과" ;
lin baby_N = mkN "아기" ; lin baby_N = mkN "아기" ;
-- lin bad_A = mkA "나쁜" ; lin bad_A = mkA "나쁘다" ;
lin beer_N = mkN "beer" ; -- lin beer_N = mkN "beer" ;
-- lin big_A = mkA "" ; lin big_A = mkA "크다" ;
lin bike_N = mkN "bike" ; -- lin bike_N = mkN "bike" ;
lin bird_N = mkN "bird" ; -- lin bird_N = mkN "bird" ;
-- lin black_A = mkA "black" ; -- lin black_A = mkA "black" ;
lin blood_N = mkN "" ; lin blood_N = mkN "피" ;
-- lin blue_A = mkA "blue" ; -- lin blue_A = mkA "blue" ;
lin boat_N = mkN "boat" ; -- lin boat_N = mkN "boat" ;
lin book_N = mkN "" ; lin book_N = mkN "책" ;
lin boy_N = mkN "소녁" ; lin boy_N = mkN "소녁" ;
lin bread_N = mkN "bread" ; lin bread_N = mkN "빵" ;
-- lin break_V2 = mkV2 (mkV "break" "broke" "broken") ; -- lin break_V2 = mkV2 (mkV "break" "broke" "broken") ;
-- lin buy_V2 = mkV2 (mkV "buy" "bought" "bought") ; lin buy_V2 = mkV2 "사다" ;
lin car_N = mkN "자동차" ; lin car_N = mkN "자동차" ;
lin cat_N = mkN "고양이" ; lin cat_N = mkN "고양이" ;
lin child_N = mkN "어린이" ; lin child_N = mkN "어린이" ;
lin city_N = mkN "city" ; -- lin city_N = mkN "city" ;
-- lin clean_A = mkA "정소한" ; lin clean_A = mkA "정소하다" ;
-- lin clever_A = mkA "똑똑한" ; lin clever_A = mkA "똑똑하다" ;
lin cloud_N = mkN "cloud" ; -- lin cloud_N = mkN "cloud" ;
-- lin cold_A = mkA "차가운" ; -- lin cold_A = mkA "차가운" ;
lin come_V = regVerb_a_o "오다" ; lin come_V = mkV "오다" ;
lin computer_N = mkN "컴퓨터" ; lin computer_N = mkN "컴퓨터" ;
lin cow_N = mkN "cow" ; -- lin cow_N = mkN "cow" ;
-- lin dirty_A = mkA "더러운" ; -- lin dirty_A = mkA "더러운" ;
lin dog_N = mkN "" ; lin dog_N = mkN "개" ;
-- lin drink_V2 = mkV2 (mkV "drink" "drank" "drunk") ; lin drink_V2 = mkV2 "마시다" ;
-- lin eat_V2 = mkV2 (mkV "eat" "ate" "eaten") ; lin eat_V2 = mkV2 "먹다" ;
-- lin find_V2 = mkV2 (mkV "find" "found" "found") ; lin find_V2 = mkV2 "찾다" ;
lin fire_N = mkN "fire" ; -- lin fire_N = mkN "fire" ;
lin fish_N = mkN "생선" ; lin fish_N = mkN "생선" ;
lin flower_N = mkN "flower" ; -- lin flower_N = mkN "flower" ;
lin friend_N = mkN "진구" ; lin friend_N = mkN "진구" ;
lin girl_N = mkN "소녀" ; lin girl_N = mkN "소녀" ;
-- lin good_A = mkA "좋은" ; -- lin good_A = mkA "좋은" ;
-- lin go_V = mkV "go" "went" "gone" ; lin go_V = mkV "가다" ;
lin grammar_N = mkN "grammar" ; -- lin grammar_N = mkN "grammar" ;
-- lin green_A = mkA "green" ; -- lin green_A = mkA "green" ;
-- lin heavy_A = mkA "heavy" ; -- lin heavy_A = mkA "heavy" ;
lin horse_N = mkN "horse" ; -- lin horse_N = mkN "horse" ;
-- lin hot_A = mkA "hot" ; -- lin hot_A = mkA "hot" ;
lin house_N = mkN "" ; lin house_N = mkN "집" ;
-- lin john_PN = mkPN "John" ; -- lin john_PN = mkPN "John" ;
-- lin jump_V = mkV "jump" ; -- lin jump_V = mkV "jump" ;
-- lin kill_V2 = mkV2 "kill" ; lin kill_V2 = mkV2 "죽다" ;
-- lin know_VS = mkVS (mkV "know" "knew" "known") ; -- lin know_V2 = mkV "알다" ;
lin language_N = mkN "언어" ; lin language_N = mkN "언어" ;
-- lin live_V = mkV "live" ; -- lin live_V = mkV "live" ;
-- lin love_V2 = mkV2 (mkV "love") ; -- lin love_V2 = mkV2 (mkV "love") ;
lin man_N = mkN "남자" ; lin man_N = mkN "남자" ;
lin milk_N = mkN "우유" ; lin milk_N = mkN "우유" ;
lin music_N = mkN "음악" ; lin music_N = mkN "음악" ;
-- lin new_A = mkA "new" ; -- lin new_A = mkA "new" ;
-- lin now_Adv = mkAdv "지금" ; lin now_Adv = mkAdv "지금" ;
-- lin old_A = mkA "낡안" ; -- lin old_A = mkA "낡안" ;
-- lin paris_PN = mkPN "Paris" ; -- lin paris_PN = mkPN "Paris" ;
-- lin play_V = mkV "" ; lin play_V = mkV "놀다" ;
-- lin read_V2 = mkV2 (mkV "read" "read" "read") ; lin read_V2 = mkV2 "읽다" ;
-- lin ready_A = mkA "ready" ; -- lin ready_A = mkA "ready" ;
-- lin red_A = mkA "red" ; -- lin red_A = mkA "red" ;
lin river_N = mkN "river" ; lin river_N = mkN "강" ;
-- lin run_V = mkV "run" "ran" "run" ; -- lin run_V = mkV "run" "ran" "run" ;
lin sea_N = mkN "바다" ; lin sea_N = mkN "바다" ;
-- lin see_V2 = mkV2 (mkV "see" "saw" "seen") ; lin see_V2 = mkV2 "보다" ;
lin ship_N = mkN "ship" ; -- lin ship_N = mkN "ship" ;
lin sleep_V = regVerb_a_o "자다" ; lin sleep_V = mkV "자다" ;
-- lin small_A = mkA "작은" ; -- lin small_A = mkA "작은" ;
lin star_N = mkN "" ; lin star_N = mkN "별" ;
-- lin swim_V = mkV "swim" "swam" "swum" ; lin swim_V = mkV "수영하다" ;
-- lin teach_V2 = mkV2 (mkV "teach" "taught" "taught") ; lin teach_V2 = mkV2 "가르치다" ;
lin train_N = mkN "train" ; -- lin train_N = mkN "train" ;
-- lin travel_V = mkV "travel" ; -- lin travel_V = mkV "travel" ;
lin tree_N = mkN "tree" ; -- lin tree_N = mkN "tree" ;
-- lin understand_V2 = mkV2 (mkV "understand" "understood" "understood") ; -- lin understand_V2 = mkV2 (mkV "understand" "understood" "understood") ;
-- lin wait_V2 = mkV2 "wait" "for" ; lin wait_V2 = mkV2 "기다리다" ;
-- lin walk_V = mkV "walk" ; lin walk_V = mkV "걷다" ;
-- lin warm_A = mkA "따뜻한" ; -- lin warm_A = mkA "따뜻한" ;
lin water_N = mkN "" ; lin water_N = mkN "물" ;
-- lin white_A = mkA "하얗은" ; -- lin white_A = mkA "하얗은" ;
lin wine_N = mkN "wine" ; -- lin wine_N = mkN "wine" ;
lin woman_N = mkN "여자" ; lin woman_N = mkN "여자" ;
-- lin yellow_A = mkA "yellow" ; -- lin yellow_A = mkA "yellow" ;
-- lin young_A = mkA "young" ; -- lin young_A = mkA "young" ;
@@ -139,14 +143,23 @@ lin woman_N = mkN "여자" ;
oper oper
mkN : Str -> Noun mkN : Str -> Noun
= \s -> lin N {s = s} ; = \s -> lin N (noun s) ;
-- mkA : Str -> A mkV : Str -> V = \lemma -> lin V (regVerb lemma) ;
-- = \stem -> lin A (mkV stem) ;
mkV2 = overload {
mkV2 : Str -> V2
= \lemma -> lin V2 (mkV lemma) ;
mkV2 : V -> V2
= \v -> lin V2 v ;
} ;
mkAdv : Str -> Adv mkAdv : Str -> Adv
= \s -> lin Adv {s = s} ; = \s -> lin Adv {s = s} ;
mkPrep : Str -> Prep mkPrep : Str -> Prep
= \s -> lin Prep {s = s} ; = \s -> lin Prep {s = s} ;
mkA : Str -> A
= \lemma -> lin A (regAdjective lemma) ;
} }

View File

@@ -1,44 +1,47 @@
resource MicroResKor = open Prelude, Hangul in { resource MicroResKor = open Prelude, HangulJamo in {
param param
VForm = Lemma | VPresent | VPast ; VForm = VLemma | VPresent | VPast | VAdnomial ;
NForm = NBare | NTopic | NSubject | NObject | NPluralSubject | NPluralTopic ;
oper oper
Noun : Type = {s : Str} ; Noun : Type = {s : NForm => Str} ;
Verb : Type = {s : VForm => Str} ; Verb : Type = {s : VForm => Str} ;
Verb2 : Type = Verb ; Verb2 : Type = Verb ;
Adjective : Type = Verb ; Adjective : Type = Verb ;
lemmaToStem : (lemma : Str) -> Str lemmaToStem : (lemma : Str) -> Str
= \lemma -> case lemma of { = \lemma -> case lemma of {
stem + "" => stem ; stem + "다" => stem ;
_ => Predef.error ("lemmaToStem was applied to a non-lemma," ++ lemma) _ => Predef.error ("lemmaToStem was applied to a non-lemma," ++ lemma)
} ; } ;
regVerb_eo : (lemma : Str) -> Verb reg : (descriptive : Bool) -> (lemma : Str) -> Verb
= \lemma -> = \descriptive,lemma ->
let stem = lemmaToStem lemma ; let stem = lemmaToStem lemma ;
in { in {
s = table { s = table {
Lemma => lemma ; VLemma => lemma ;
VPresent => stem + "어요" ; VPresent => present_haeyo stem ;
VPast => stem + "었어요" VPast => past_haeyo stem ;
VAdnomial =>
if_then_else Str descriptive
(stem + vc_allomorph stem "ᆫ" "은")
(stem + "는")
} }
} ; } ;
regVerb_a_o : (lemma : Str) -> Verb regVerb : (lemma : Str) -> Verb = reg False ;
= \lemma -> regAdjective : (lemma : Str) -> Adjective = reg True ;
let stem = lemmaToStem lemma ;
in { noun : (bare : Str) -> Noun
= \bare -> {
s = table { s = table {
Lemma => lemma ; NBare => bare ;
VPresent => stem + "아요" ; NTopic => bare + vc_allomorph bare "는" "은" ;
VPast => stem + "았어요" NSubject => bare + vc_allomorph bare "이" "가" ;
NObject => bare + vc_allomorph bare "를" "을" ;
NPluralTopic => bare + "들은" ;
NPluralSubject => bare + "들이"
} }
} ; } ;
blah : Str -> Str
= \s -> case s of {
_ + "ᅡ" => "아-final" ;
_ => "not 아-final"
} ;
} }

12
scripts/search-hangul Executable file
View File

@@ -0,0 +1,12 @@
#!/usr/bin/env bb
(require '[babashka.fs :as fs]
'[clojure.string :as str])
(doseq [f (fs/glob "lab1/grammar/korean" "**.gf")
[line-number line] (map-indexed (fn [i x] [i x])
(-> f str slurp str/split-lines))]
(when (re-find #"\p{block=HangulJamo}" line)
(printf "JAMO: %s:%d: %s\n" (str f) line-number line))
(when (re-find #"\p{block=HangulSyllables}" line)
(printf "SYLLABLE: %s:%d: %s\n" (str f) line-number line)))

44
scripts/syllables-to-jamo Executable file
View File

@@ -0,0 +1,44 @@
#!/usr/bin/env bb
(require '[babashka.fs :as fs]
'[clojure.string :as str])
(defn syllables->jamo [s]
"Convert Hangul syllables in string S to their jamo components."
(->> s
(map int)
(mapcat
(fn [c]
(if (<= 0xAC00 c 0xD7A3)
;; Hangul syllable - decompose
(let [code (- c 0xAC00)
lead (quot code (* 21 28))
medial (quot (mod code (* 21 28)) 28)
final (mod code 28)
lead-jamo (+ 0x1100 lead)
medial-jamo (+ 0x1161 medial)
final-jamo (if (> final 0) (+ 0x11A7 final) nil)]
(remove nil? [lead-jamo medial-jamo final-jamo]))
;; Not a Hangul syllable
[c])))
(map char)
(apply str)))
(defn -main [& args]
(if (seq args)
(doseq [f args]
(let [x (-> (slurp f)
(str/replace #"\p{block=HangulSyllables}+"
syllables->jamo))]
(spit f x)))
(loop [line (read-line)]
(when line
(-> line
(str/replace #"\p{block=HangulSyllables}+"
syllables->jamo)
println)
(recur (read-line))))))
(when (= *file* (System/getProperty "babashka.file"))
(apply -main *command-line-args*))