Line data Source code
1 4 : (ns matchscore.utils.transliteration
2 : (:require
3 : [clojure.string :as str])
4 : (:import
5 : (java.lang Character$UnicodeScript)))
6 :
7 :
8 8 : (def ^:private latin-name-regex #"^[\p{IsLatin}\p{M}][\p{IsLatin}\p{M}\s'.-]*$")
9 :
10 :
11 8 : (def ^:private localized-name-regex #"^[\p{L}\p{M}][\p{L}\p{M}\s'.-]*$")
12 :
13 :
14 4 : (def ^:private transliterations
15 300 : {\а "a"
16 : \б "b"
17 : \в "v"
18 : \г "h"
19 : \ґ "g"
20 : \д "d"
21 : \е "e"
22 : \є "ie"
23 : \ж "zh"
24 : \з "z"
25 : \и "y"
26 : \і "i"
27 : \ї "i"
28 : \й "i"
29 : \к "k"
30 : \л "l"
31 : \м "m"
32 : \н "n"
33 : \о "o"
34 : \п "p"
35 : \р "r"
36 : \с "s"
37 : \т "t"
38 : \у "u"
39 : \ф "f"
40 : \х "kh"
41 : \ц "ts"
42 : \ч "ch"
43 : \ш "sh"
44 : \щ "shch"
45 : \ь ""
46 : \ю "iu"
47 : \я "ia"
48 : \ы "y"
49 : \э "e"
50 : \ё "io"
51 : \ъ ""})
52 :
53 :
54 4 : (def ^:private initial-transliterations
55 52 : {\є "ye"
56 : \ї "yi"
57 : \й "y"
58 : \ю "yu"
59 : \я "ya"
60 : \ё "yo"})
61 :
62 :
63 13 : (defn- upper-case-letter? [ch] (Character/isUpperCase ^char ch))
64 :
65 :
66 4 : (defn- capitalize-output
67 : [output]
68 51 : (if (seq output) (str (str/upper-case (subs output 0 1)) (subs output 1)) output))
69 :
70 :
71 4 : (defn- word-initial?
72 : [previous]
73 54 : (or (nil? previous) (Character/isWhitespace ^char previous) (= previous \-)))
74 :
75 :
76 4 : (defn- allowed-passthrough?
77 : [ch]
78 23 : (or
79 9 : (Character/isWhitespace ^char ch)
80 6 : (= Character$UnicodeScript/LATIN (Character$UnicodeScript/of (int ch)))
81 5 : (= (Character/getType ^char ch) Character/NON_SPACING_MARK)
82 5 : (= (Character/getType ^char ch) Character/COMBINING_SPACING_MARK)
83 4 : (#{\- \.} ch)))
84 :
85 :
86 4 : (defn- normalize-spaces
87 : "Turns no-break spaces into regular spaces so they separate words."
88 : [s]
89 36 : (str/replace (or s "") #"[\u00A0\u2007\u202F]" " "))
90 :
91 :
92 4 : (defn transliterate
93 : [s]
94 18 : (let [chars (vec (normalize-spaces s))]
95 9 : (loop [idx 0
96 : previous nil
97 3 : result (StringBuilder.)]
98 15 : (if (>= idx (count chars))
99 3 : (->
100 3 : result
101 3 : str
102 15 : (str/replace #"\s+" " ")
103 3 : str/trim)
104 12 : (let [ch (nth chars idx)
105 9 : lower-ch (Character/toLowerCase ^char ch)
106 33 : next-ch (when (< (inc idx) (count chars)) (nth chars (inc idx)))
107 18 : lower-next (when next-ch (Character/toLowerCase ^char next-ch))]
108 37 : (cond (and (= lower-ch \з) (= lower-next \г))
109 11 : (let [output (cond-> "zgh" (upper-case-letter? ch) capitalize-output)]
110 2 : (.append result output)
111 1 : (recur (+ idx 2) next-ch result))
112 :
113 19 : (#{\' \’ \ʼ} ch) (recur (inc idx) ch result)
114 :
115 12 : (contains? transliterations lower-ch)
116 16 : (let [base (or
117 24 : (when (word-initial? previous) (get initial-transliterations lower-ch))
118 6 : (get transliterations lower-ch))
119 30 : output (cond-> base (upper-case-letter? ch) capitalize-output)]
120 6 : (.append result output)
121 3 : (recur (inc idx) ch result))
122 :
123 21 : (allowed-passthrough? ch) (do (.append result ch) (recur (inc idx) ch result))
124 :
125 1 : :else (recur (inc idx) ch result)))))))
126 :
127 :
128 4 : (defn- valid-name?
129 : [regex s]
130 36 : (let [value (some->
131 4 : s
132 4 : str/trim)]
133 80 : (and (some? value) (<= 1 (count value) 120) (boolean (re-matches regex value)))))
134 :
135 :
136 4 : (defn latin-name?
137 : [s]
138 16 : (valid-name? latin-name-regex s))
139 :
140 :
141 4 : (defn localized-name?
142 : [s]
143 12 : (valid-name? localized-name-regex s))
144 :
145 :
146 4 : (defn sanitize
147 : [s]
148 4 : (->
149 4 : (->>
150 12 : (normalize-spaces s)
151 12 : (filter
152 58 : #(or
153 12 : (Character/isLetter ^char %)
154 20 : (= (Character/getType ^char %) Character/NON_SPACING_MARK)
155 20 : (= (Character/getType ^char %) Character/COMBINING_SPACING_MARK)
156 12 : (Character/isWhitespace ^char %)
157 14 : (#{\' \’ \ʼ \. \-} %)))
158 8 : (apply str))
159 16 : (str/replace #"\s+" " ")
160 4 : str/trim))
|