-
Notifications
You must be signed in to change notification settings - Fork 4
/
wordify.r
193 lines (180 loc) · 4.68 KB
/
wordify.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
Rebol [
Title: "Wordify Text"
Author: "Christopher Ross-Gill"
Date: 18-Oct-2011
File: %wordify.r
Version: 0.1.0
Purpose: "Reduce a string to a few core ASCII characters"
Rights: http://opensource.org/licenses/Apache-2.0
Type: 'module
Name: 'rgchris.wordify
Exports: [wordify unwordify]
History: [
18-Oct-2011 0.1.0 "Original Version"
]
Usage: {
>> wordify "Testing Iñtërnâtiônàlizætiøn"
== "testing-internationalizaetion"
}
]
wordify: use [out emit alphanum punct extras extras-loose extras-tight codes chars clean-up][
alphanum: charset [#"0" - #"9" #"a" - #"z" #"A" - #"Z"]
punct: charset "!()" ; "*!',()"
codes: [
copy chars some alphanum (emit chars)
| [" " | "-" | "/" | "_" | "‑" | "–" | "—" | "⁻" | "₋" | " "] (emit "-")
| ["⁰" | "₀"] (emit "0")
| ["¹" | "₁"] (emit "1")
| ["²" | "₂"] (emit "2")
| ["³" | "₃"] (emit "3")
| ["⁴" | "₄"] (emit "4")
| ["⁵" | "₅"] (emit "5")
| ["⁶" | "₆"] (emit "6")
| ["⁷" | "₇"] (emit "7")
| ["⁸" | "₈"] (emit "8")
| ["⁹" | "₉"] (emit "9")
| ["Ä"] (emit "Ae")
| ["À" | "Á" | "Â" | "Ã" | "Å" | "Ā"] (emit "A")
| ["ä"] (emit "ae")
| ["ₐ" | "ª" | "à" | "á" | "â" | "ã" | "å" | "ā"] (emit "a")
| ["Ç" | "Ć"] (emit "C")
| ["ç" | "ć"] (emit "c")
| ["Ð"] (emit "D")
| ["đ"] (emit "d")
| ["È" | "É" | "Ê" | "Ë"] (emit "E")
| ["ₑ" | "è" | "é" | "ê" | "ë"] (emit "e")
| ["ƒ"] (emit "f")
| ["ğ"] (emit "g")
| ["Ğ"] (emit "G")
| ["I" | "Ì" | "Í" | "Î"] (emit "I")
| ["ì" | "í" | "î" | "ī" | "ı"] (emit "i")
| ["Ï"] (emit "Ii")
| ["ï"] (emit "ii")
| ["Ñ"] (emit "N")
| ["ⁿ" | "ñ"] (emit "n")
| ["№"] (emit "Nr")
| ["Ö" | "Œ"] (emit "Oe")
| ["Ò" | "Ó" | "Ô" | "Õ" | "Ø"] (emit "O")
| ["ö" | "œ"] (emit "oe")
| ["ₒ" | "ð" | "ò" | "ó" | "ô" | "õ" | "ø"] (emit "o")
| ["Ş" | "Š"] (emit "S")
| ["ş" | "š"] (emit "s")
| ["ß"] (emit "ss")
| ["Ú" | "Ù" | "Û"] (emit "U")
| ["ù" | "ú" | "û"] (emit "u")
| ["Ü"] (emit "Ue")
| ["ü"] (emit "ue")
| ["×"] (emit "x")
| ["Ý" | "Ÿ"] (emit "Y")
| ["ý" | "ÿ"] (emit "y")
| ["Ž"] (emit "Z")
| ["ž"] (emit "z")
| extras
| skip
]
extras-loose: [
copy chars some punct (emit chars)
| "." (emit "_")
| ["⁰" | "₀"] (emit "(0)")
| ["¹" | "₁"] (emit "(1)")
| ["²" | "₂"] (emit "(2)")
| ["³" | "₃"] (emit "(3)")
| ["⁴" | "₄"] (emit "(4)")
| ["⁵" | "₅"] (emit "(5)")
| ["⁶" | "₆"] (emit "(6)")
| ["⁷" | "₇"] (emit "(7)")
| ["⁸" | "₈"] (emit "(8)")
| ["⁹" | "₉"] (emit "(9)")
| "¼" (emit "(1-4)")
| "½" (emit "(1-2)")
| "¾" (emit "(3-4)")
| "Æ" (emit "(AE)")
| "æ" (emit "(ae)")
| "©" (emit "(c)")
| "°" (emit "(deg)")
| "º" (emit "(o)")
| "®" (emit "(r)")
| "™" (emit "(tm)")
]
extras-tight: [
["⁰" | "₀"] (emit "0")
| ["¹" | "₁"] (emit "1")
| ["²" | "₂"] (emit "2")
| ["³" | "₃"] (emit "3")
| ["⁴" | "₄"] (emit "4")
| ["⁵" | "₅"] (emit "5")
| ["⁶" | "₆"] (emit "6")
| ["⁷" | "₇"] (emit "7")
| ["⁸" | "₈"] (emit "8")
| ["⁹" | "₉"] (emit "9")
| "¼" (emit "1-4")
| "½" (emit "1-2")
| "¾" (emit "3-4")
| "Æ" (emit "AE")
| "æ" (emit "ae")
| "©" (emit "c")
| "º" (emit "o")
| "°" (emit "deg")
| "®" (emit "r")
| "™" (emit "tm")
]
clean-up: use [mk ex tm][
[
(tm: [end skip])
mk: any "-" ex: (remove/part mk ex) :mk
some [
mk: some "-" end (clear mk) :mk
| "-" mk: some "-" ex: (remove/part mk ex) :mk
| skip tm:
]
:tm end
]
]
out: ""
emit: func [data][append out data]
func [text [string!] /case /loose][
out: copy ""
extras: either loose [:extras-loose][:extras-tight]
all [
parse/all/case text [any codes]
parse/all out [clean-up]
either case [out][lowercase out]
]
]
]
unwordify: use [alpha digit punct codes char in-word][
digit: charset [#"0" - #"9"]
alpha: charset [#"a" - #"z" #"A" - #"Z"]
punct: charset ["*!',()"]
codes: [
copy char some alpha (
emit either in-word [char][uppercase/part char 1]
in-word: true
)
| copy char some digit (emit char in-word: true)
| (in-word: true) "(1)" (emit "¹")
| "(2)" (emit "²")
| "(3)" (emit "³")
| "(1-4)" (emit "¼")
| "(1-2)" (emit "½")
| "(3-4)" (emit "¾")
| "(AE)" (emit "Æ")
| "(ae)" (emit "æ")
| "(c)" (emit "©")
| "(o)" (emit "º")
| "(r)" (emit "®")
| "(tm)" (emit "™")
| (in-word: false) "_" (emit ".")
| "-" (emit " ")
| copy char some punct (emit char)
| skip
]
out: ""
emit: func [data][append out data]
func [word [string!]][
out: copy ""
in-word: false
parse/all/case word [any codes]
out
]
]