Line data Source code
1 4 : (ns matchscore.search.translit
2 : "Latin spellings of Cyrillic names, so a name stored in Cyrillic (FSU players)
3 : is found by its Latin spelling (\"panov\" finds \"Панов Микита\").
4 :
5 : The main spelling follows the Ukrainian national transliteration (KMU 2010):
6 : г → h, и → y, є/ї/й/ю/я written ye/yi/y/yu/ya at the start of a word and
7 : ie/i/i/iu/ia inside it, зг → zgh. Russian-only letters (ы, э, ъ, ё) are mapped
8 : too. A second spelling with г → g covers the common Russian-style spelling
9 : (\"Gorbov\" next to \"Horbov\")."
10 : (:require
11 : [clojure.string :as str]))
12 :
13 :
14 4 : (def ^:private letters
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 : \ъ ""
51 : \ё "e"})
52 :
53 :
54 4 : (def ^:private word-start
55 : "Spellings at the start of a word (KMU 2010)."
56 44 : {\є "ye"
57 : \ї "yi"
58 : \й "y"
59 : \ю "yu"
60 : \я "ya"})
61 :
62 :
63 24 : (def ^:private dropped #{\' \’ \ʼ \`})
64 :
65 :
66 4 : (defn cyrillic?
67 : [s]
68 19 : (boolean (and s (re-find #"[Ѐ-ӿ]" s))))
69 :
70 :
71 4 : (defn- capitalize-like
72 : [^String original ^String latin]
73 22 : (if (and (seq latin) (Character/isUpperCase (.charAt original 0)))
74 26 : (str (str/upper-case (subs latin 0 1)) (subs latin 1))
75 0 : latin))
76 :
77 :
78 4 : (defn- word->latin
79 : [g-variant? ^String word]
80 8 : (let [lower (str/lower-case word)
81 4 : n (count lower)]
82 4 : (loop [i 0
83 2 : out (StringBuilder.)]
84 8 : (if (>= i n)
85 12 : (capitalize-like word (str out))
86 6 : (let [c (.charAt lower i)
87 14 : prev (when (pos? i) (.charAt lower (dec i)))
88 19 : latin (cond (dropped c) ""
89 17 : (and (= c \г) (= prev \з)) "gh"
90 15 : (and (= c \г) g-variant?) "g"
91 21 : (and (zero? i) (word-start c)) (word-start c)
92 12 : :else (get letters c (str c)))]
93 2 : (recur (inc i) (.append out latin)))))))
94 :
95 :
96 4 : (defn- ->latin
97 : [g-variant? s]
98 32 : (str/join " " (map #(word->latin g-variant? %) (str/split (str/trim s) #"\s+"))))
99 :
100 :
101 4 : (defn latin-aliases
102 : "Distinct Latin spellings of a Cyrillic `s`; empty for a name without Cyrillic."
103 : [s]
104 36 : (if (cyrillic? s) (vec (distinct [(->latin false s) (->latin true s)])) []))
|