pumi 0.19.0 → 0.20.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +14 -0
- data/README.md +8 -0
- data/bin/parse_data +2 -5
- data/data/communes.yml +9904 -0
- data/data/districts.yml +1576 -0
- data/data/provinces.yml +225 -0
- data/lib/pumi/bot/wikipedia/article.rb +13 -0
- data/lib/pumi/bot/wikipedia/communes_in_cambodia_article.rb +157 -0
- data/lib/pumi/bot/wikipedia/districts_in_cambodia_article.rb +122 -0
- data/lib/pumi/bot/wikipedia/templates/commune_list.wikitext.erb +46 -0
- data/lib/pumi/bot/wikipedia/templates/district_list.wikitext.erb +27 -0
- data/lib/pumi/bot/wikipedia.rb +10 -0
- data/lib/pumi/bot.rb +6 -0
- data/lib/pumi/data_source/geocoder.rb +251 -0
- data/lib/pumi/data_source/iso31662.rb +29 -0
- data/lib/pumi/data_source/wikipedia.rb +19 -524
- data/lib/pumi/data_source.rb +2 -0
- data/lib/pumi/geodata.rb +3 -0
- data/lib/pumi/location.rb +2 -0
- data/lib/pumi/parser.rb +7 -0
- data/lib/pumi/version.rb +1 -1
- data/lib/pumi/wikipedia/client.rb +68 -0
- data/lib/pumi/wikipedia/response.rb +15 -0
- data/lib/pumi/wikipedia.rb +7 -0
- data/lib/pumi.rb +1 -1
- data/pumi.gemspec +4 -1
- metadata +58 -4
- data/lib/pumi/scraper/result.rb +0 -5
data/data/provinces.yml
CHANGED
@@ -10,6 +10,15 @@ provinces:
|
|
10
10
|
en: Province
|
11
11
|
links:
|
12
12
|
wikipedia: https://en.wikipedia.org/wiki/Banteay_Meanchey_province
|
13
|
+
geodata:
|
14
|
+
lat: '13.7989147'
|
15
|
+
long: '102.8862666'
|
16
|
+
bounding_box:
|
17
|
+
- '13.3579351'
|
18
|
+
- '14.2502283'
|
19
|
+
- '102.3338282'
|
20
|
+
- '103.4427283'
|
21
|
+
iso3166_2: KH-1
|
13
22
|
'02':
|
14
23
|
name:
|
15
24
|
km: បាត់ដំបង
|
@@ -20,6 +29,15 @@ provinces:
|
|
20
29
|
en: Province
|
21
30
|
links:
|
22
31
|
wikipedia: https://en.wikipedia.org/wiki/Battambang_province
|
32
|
+
geodata:
|
33
|
+
lat: '12.9256791'
|
34
|
+
long: '103.23171364274523'
|
35
|
+
bounding_box:
|
36
|
+
- '12.3711193'
|
37
|
+
- '13.4793632'
|
38
|
+
- '102.3450807'
|
39
|
+
- '103.9011713'
|
40
|
+
iso3166_2: KH-2
|
23
41
|
'03':
|
24
42
|
name:
|
25
43
|
km: កំពង់ចាម
|
@@ -30,6 +48,15 @@ provinces:
|
|
30
48
|
en: Province
|
31
49
|
links:
|
32
50
|
wikipedia: https://en.wikipedia.org/wiki/Kampong_Cham_province
|
51
|
+
geodata:
|
52
|
+
lat: '12.1174857'
|
53
|
+
long: '105.20800220742942'
|
54
|
+
bounding_box:
|
55
|
+
- '11.7342165'
|
56
|
+
- '12.5014623'
|
57
|
+
- '104.8198072'
|
58
|
+
- '105.7759428'
|
59
|
+
iso3166_2: KH-3
|
33
60
|
'04':
|
34
61
|
name:
|
35
62
|
km: កំពង់ឆ្នាំង
|
@@ -40,6 +67,15 @@ provinces:
|
|
40
67
|
en: Province
|
41
68
|
links:
|
42
69
|
wikipedia: https://en.wikipedia.org/wiki/Kampong_Chhnang_province
|
70
|
+
geodata:
|
71
|
+
lat: '12.1382025'
|
72
|
+
long: '104.3354098'
|
73
|
+
bounding_box:
|
74
|
+
- '11.7344035'
|
75
|
+
- '12.6074435'
|
76
|
+
- '104.1710946'
|
77
|
+
- '104.9019904'
|
78
|
+
iso3166_2: KH-4
|
43
79
|
'05':
|
44
80
|
name:
|
45
81
|
km: កំពង់ស្ពឺ
|
@@ -50,6 +86,15 @@ provinces:
|
|
50
86
|
en: Province
|
51
87
|
links:
|
52
88
|
wikipedia: https://en.wikipedia.org/wiki/Kampong_Speu_province
|
89
|
+
geodata:
|
90
|
+
lat: '11.57753675'
|
91
|
+
long: '104.27140233242656'
|
92
|
+
bounding_box:
|
93
|
+
- '11.0779221'
|
94
|
+
- '12.0768453'
|
95
|
+
- '103.7729787'
|
96
|
+
- '104.7925761'
|
97
|
+
iso3166_2: KH-5
|
53
98
|
'06':
|
54
99
|
name:
|
55
100
|
km: កំពង់ធំ
|
@@ -60,6 +105,15 @@ provinces:
|
|
60
105
|
en: Province
|
61
106
|
links:
|
62
107
|
wikipedia: https://en.wikipedia.org/wiki/Kampong_Thom_province
|
108
|
+
geodata:
|
109
|
+
lat: '12.6687923'
|
110
|
+
long: '104.8879197'
|
111
|
+
bounding_box:
|
112
|
+
- '12.1886895'
|
113
|
+
- '13.4495556'
|
114
|
+
- '104.1734824'
|
115
|
+
- '105.7348383'
|
116
|
+
iso3166_2: KH-6
|
63
117
|
'07':
|
64
118
|
name:
|
65
119
|
km: កំពត
|
@@ -70,6 +124,15 @@ provinces:
|
|
70
124
|
en: Province
|
71
125
|
links:
|
72
126
|
wikipedia: https://en.wikipedia.org/wiki/Kampot_province
|
127
|
+
geodata:
|
128
|
+
lat: '10.6314754'
|
129
|
+
long: '104.132637'
|
130
|
+
bounding_box:
|
131
|
+
- '10.3536198'
|
132
|
+
- '11.1722266'
|
133
|
+
- '103.8390342'
|
134
|
+
- '104.727076'
|
135
|
+
iso3166_2: KH-7
|
73
136
|
'08':
|
74
137
|
name:
|
75
138
|
km: កណ្តាល
|
@@ -80,6 +143,15 @@ provinces:
|
|
80
143
|
en: Province
|
81
144
|
links:
|
82
145
|
wikipedia: https://en.wikipedia.org/wiki/Kandal_province
|
146
|
+
geodata:
|
147
|
+
lat: '11.396262499999999'
|
148
|
+
long: '105.02078685074581'
|
149
|
+
bounding_box:
|
150
|
+
- '10.8964776'
|
151
|
+
- '11.8943134'
|
152
|
+
- '104.6562154'
|
153
|
+
- '105.2971605'
|
154
|
+
iso3166_2: KH-8
|
83
155
|
'09':
|
84
156
|
name:
|
85
157
|
km: កោះកុង
|
@@ -90,6 +162,15 @@ provinces:
|
|
90
162
|
en: Province
|
91
163
|
links:
|
92
164
|
wikipedia: https://en.wikipedia.org/wiki/Koh_Kong_province
|
165
|
+
geodata:
|
166
|
+
lat: '11.6813417'
|
167
|
+
long: '103.0258043'
|
168
|
+
bounding_box:
|
169
|
+
- '10.6542293'
|
170
|
+
- '12.116263'
|
171
|
+
- '102.5849839'
|
172
|
+
- '104.0645553'
|
173
|
+
iso3166_2: KH-9
|
93
174
|
'10':
|
94
175
|
name:
|
95
176
|
km: ក្រចេះ
|
@@ -100,6 +181,15 @@ provinces:
|
|
100
181
|
en: Province
|
101
182
|
links:
|
102
183
|
wikipedia: https://en.wikipedia.org/wiki/Krati%C3%A9_province
|
184
|
+
geodata:
|
185
|
+
lat: '12.678301699999999'
|
186
|
+
long: '106.05817329824376'
|
187
|
+
bounding_box:
|
188
|
+
- '11.9494318'
|
189
|
+
- '13.4073182'
|
190
|
+
- '105.5871802'
|
191
|
+
- '106.8019914'
|
192
|
+
iso3166_2: KH-10
|
103
193
|
'11':
|
104
194
|
name:
|
105
195
|
km: មណ្ឌលគិរី
|
@@ -110,6 +200,15 @@ provinces:
|
|
110
200
|
en: Province
|
111
201
|
links:
|
112
202
|
wikipedia: https://en.wikipedia.org/wiki/Mondulkiri_province
|
203
|
+
geodata:
|
204
|
+
lat: '12.7396454'
|
205
|
+
long: '107.00882593208544'
|
206
|
+
bounding_box:
|
207
|
+
- '12.0614615'
|
208
|
+
- '13.4175148'
|
209
|
+
- '106.3385777'
|
210
|
+
- '107.6033126'
|
211
|
+
iso3166_2: KH-11
|
113
212
|
'12':
|
114
213
|
name:
|
115
214
|
km: ភ្នំពេញ
|
@@ -120,6 +219,15 @@ provinces:
|
|
120
219
|
en: Capital
|
121
220
|
links:
|
122
221
|
wikipedia: https://en.wikipedia.org/wiki/Phnom_Penh
|
222
|
+
geodata:
|
223
|
+
lat: '11.5730391'
|
224
|
+
long: '104.857807'
|
225
|
+
bounding_box:
|
226
|
+
- '11.4200852'
|
227
|
+
- '11.7349524'
|
228
|
+
- '104.7204046'
|
229
|
+
- '105.0440261'
|
230
|
+
iso3166_2: KH-12
|
123
231
|
'13':
|
124
232
|
name:
|
125
233
|
km: ព្រះវិហារ
|
@@ -130,6 +238,15 @@ provinces:
|
|
130
238
|
en: Province
|
131
239
|
links:
|
132
240
|
wikipedia: https://en.wikipedia.org/wiki/Preah_Vihear_province
|
241
|
+
geodata:
|
242
|
+
lat: '13.746298150000001'
|
243
|
+
long: '104.97403002709669'
|
244
|
+
bounding_box:
|
245
|
+
- '13.0553454'
|
246
|
+
- '14.4374188'
|
247
|
+
- '104.3579998'
|
248
|
+
- '105.8992487'
|
249
|
+
iso3166_2: KH-13
|
133
250
|
'14':
|
134
251
|
name:
|
135
252
|
km: ព្រៃវែង
|
@@ -140,6 +257,15 @@ provinces:
|
|
140
257
|
en: Province
|
141
258
|
links:
|
142
259
|
wikipedia: https://en.wikipedia.org/wiki/Prey_Veng_province
|
260
|
+
geodata:
|
261
|
+
lat: '11.365959700000001'
|
262
|
+
long: '105.45572772976189'
|
263
|
+
bounding_box:
|
264
|
+
- '10.8598854'
|
265
|
+
- '11.8712687'
|
266
|
+
- '105.1039948'
|
267
|
+
- '105.8091078'
|
268
|
+
iso3166_2: KH-14
|
143
269
|
'15':
|
144
270
|
name:
|
145
271
|
km: ពោធិ៍សាត់
|
@@ -150,6 +276,15 @@ provinces:
|
|
150
276
|
en: Province
|
151
277
|
links:
|
152
278
|
wikipedia: https://en.wikipedia.org/wiki/Pursat_province
|
279
|
+
geodata:
|
280
|
+
lat: '12.4662592'
|
281
|
+
long: '103.7426829518918'
|
282
|
+
bounding_box:
|
283
|
+
- '11.8432598'
|
284
|
+
- '13.0891438'
|
285
|
+
- '102.70351'
|
286
|
+
- '104.3951534'
|
287
|
+
iso3166_2: KH-15
|
153
288
|
'16':
|
154
289
|
name:
|
155
290
|
km: រតនគិរី
|
@@ -160,6 +295,15 @@ provinces:
|
|
160
295
|
en: Province
|
161
296
|
links:
|
162
297
|
wikipedia: https://en.wikipedia.org/wiki/Ratanakiri_province
|
298
|
+
geodata:
|
299
|
+
lat: '13.9311155'
|
300
|
+
long: '107.03914990288844'
|
301
|
+
bounding_box:
|
302
|
+
- '13.1721622'
|
303
|
+
- '14.6904224'
|
304
|
+
- '106.5301893'
|
305
|
+
- '107.6276788'
|
306
|
+
iso3166_2: KH-16
|
163
307
|
'17':
|
164
308
|
name:
|
165
309
|
km: សៀមរាប
|
@@ -170,6 +314,15 @@ provinces:
|
|
170
314
|
en: Province
|
171
315
|
links:
|
172
316
|
wikipedia: https://en.wikipedia.org/wiki/Siem_Reap_province
|
317
|
+
geodata:
|
318
|
+
lat: '13.314962'
|
319
|
+
long: '103.805433'
|
320
|
+
bounding_box:
|
321
|
+
- '12.7720495'
|
322
|
+
- '13.9718578'
|
323
|
+
- '103.3892306'
|
324
|
+
- '104.6832474'
|
325
|
+
iso3166_2: KH-17
|
173
326
|
'18':
|
174
327
|
name:
|
175
328
|
km: ព្រះសីហនុ
|
@@ -180,6 +333,15 @@ provinces:
|
|
180
333
|
en: Province
|
181
334
|
links:
|
182
335
|
wikipedia: https://en.wikipedia.org/wiki/Sihanoukville_province
|
336
|
+
geodata:
|
337
|
+
lat: '10.50614075'
|
338
|
+
long: '103.26205644863938'
|
339
|
+
bounding_box:
|
340
|
+
- '9.4110961'
|
341
|
+
- '11.436044'
|
342
|
+
- '102.6295604'
|
343
|
+
- '104.0772547'
|
344
|
+
iso3166_2: KH-18
|
183
345
|
'19':
|
184
346
|
name:
|
185
347
|
km: ស្ទឹងត្រែង
|
@@ -190,6 +352,15 @@ provinces:
|
|
190
352
|
en: Province
|
191
353
|
links:
|
192
354
|
wikipedia: https://en.wikipedia.org/wiki/Stung_Treng_province
|
355
|
+
geodata:
|
356
|
+
lat: '13.852468'
|
357
|
+
long: '106.0889848986395'
|
358
|
+
bounding_box:
|
359
|
+
- '13.1137001'
|
360
|
+
- '14.5919671'
|
361
|
+
- '105.5194671'
|
362
|
+
- '106.7891813'
|
363
|
+
iso3166_2: KH-19
|
193
364
|
'20':
|
194
365
|
name:
|
195
366
|
km: ស្វាយរៀង
|
@@ -200,6 +371,15 @@ provinces:
|
|
200
371
|
en: Province
|
201
372
|
links:
|
202
373
|
wikipedia: https://en.wikipedia.org/wiki/Svay_Rieng_province
|
374
|
+
geodata:
|
375
|
+
lat: '11.1942275'
|
376
|
+
long: '105.7973275083619'
|
377
|
+
bounding_box:
|
378
|
+
- '10.7889074'
|
379
|
+
- '11.5996145'
|
380
|
+
- '105.5796471'
|
381
|
+
- '106.206119'
|
382
|
+
iso3166_2: KH-20
|
203
383
|
'21':
|
204
384
|
name:
|
205
385
|
km: តាកែវ
|
@@ -210,6 +390,15 @@ provinces:
|
|
210
390
|
en: Province
|
211
391
|
links:
|
212
392
|
wikipedia: https://en.wikipedia.org/wiki/Tak%C3%A9o_province
|
393
|
+
geodata:
|
394
|
+
lat: '10.93539205'
|
395
|
+
long: '104.78988300047047'
|
396
|
+
bounding_box:
|
397
|
+
- '10.5187794'
|
398
|
+
- '11.3530403'
|
399
|
+
- '104.4101661'
|
400
|
+
- '105.0983022'
|
401
|
+
iso3166_2: KH-21
|
213
402
|
'22':
|
214
403
|
name:
|
215
404
|
km: ឧត្តរមានជ័យ
|
@@ -220,6 +409,15 @@ provinces:
|
|
220
409
|
en: Province
|
221
410
|
links:
|
222
411
|
wikipedia: https://en.wikipedia.org/wiki/Oddar_Meanchey_province
|
412
|
+
geodata:
|
413
|
+
lat: '14.1585631'
|
414
|
+
long: '103.78279595655918'
|
415
|
+
bounding_box:
|
416
|
+
- '13.8769057'
|
417
|
+
- '14.4403696'
|
418
|
+
- '103.0318699'
|
419
|
+
- '104.5085773'
|
420
|
+
iso3166_2: KH-22
|
223
421
|
'23':
|
224
422
|
name:
|
225
423
|
km: កែប
|
@@ -230,6 +428,15 @@ provinces:
|
|
230
428
|
en: Province
|
231
429
|
links:
|
232
430
|
wikipedia: https://en.wikipedia.org/wiki/Kep_province
|
431
|
+
geodata:
|
432
|
+
lat: '10.402153949999999'
|
433
|
+
long: '104.26506181616313'
|
434
|
+
bounding_box:
|
435
|
+
- '10.1548084'
|
436
|
+
- '10.5908859'
|
437
|
+
- '104.0710119'
|
438
|
+
- '104.4259394'
|
439
|
+
iso3166_2: KH-23
|
233
440
|
'24':
|
234
441
|
name:
|
235
442
|
km: ប៉ៃលិន
|
@@ -240,6 +447,15 @@ provinces:
|
|
240
447
|
en: Province
|
241
448
|
links:
|
242
449
|
wikipedia: https://en.wikipedia.org/wiki/Pailin_province
|
450
|
+
geodata:
|
451
|
+
lat: '12.89053285'
|
452
|
+
long: '102.62946042446026'
|
453
|
+
bounding_box:
|
454
|
+
- '12.663855'
|
455
|
+
- '13.1166697'
|
456
|
+
- '102.485444'
|
457
|
+
- '102.7533088'
|
458
|
+
iso3166_2: KH-24
|
243
459
|
'25':
|
244
460
|
name:
|
245
461
|
km: ត្បូងឃ្មុំ
|
@@ -250,3 +466,12 @@ provinces:
|
|
250
466
|
en: Province
|
251
467
|
links:
|
252
468
|
wikipedia: https://en.wikipedia.org/wiki/Tboung_Khmum_province
|
469
|
+
geodata:
|
470
|
+
lat: '11.950072599999999'
|
471
|
+
long: '105.824571891073'
|
472
|
+
bounding_box:
|
473
|
+
- '11.5995257'
|
474
|
+
- '12.3005412'
|
475
|
+
- '105.408445'
|
476
|
+
- '106.4674381'
|
477
|
+
iso3166_2: KH-25
|
@@ -0,0 +1,157 @@
|
|
1
|
+
require "ostruct"
|
2
|
+
|
3
|
+
module Pumi
|
4
|
+
module Bot
|
5
|
+
module Wikipedia
|
6
|
+
class CommunesInCambodiaArticle < Article
|
7
|
+
class Location < SimpleDelegator
|
8
|
+
def communes_summary
|
9
|
+
summary = communes_by_type.map do |administrative_unit, communes|
|
10
|
+
next if Array(communes).empty?
|
11
|
+
|
12
|
+
text = "#{communes.size} #{administrative_unit.name_en}"
|
13
|
+
text << "s" if communes.size > 1
|
14
|
+
text << " (#{administrative_unit.name_km} #{administrative_unit.name_latin})"
|
15
|
+
end
|
16
|
+
|
17
|
+
summary << "#{format_number(villages_count)} Villages (ភូមិ Phum)"
|
18
|
+
|
19
|
+
if summary.size <= 2
|
20
|
+
summary.join(" and ")
|
21
|
+
else
|
22
|
+
[summary[0..-2].join(", "), summary[-1]].join(" and ")
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def communes_by_type
|
29
|
+
communes.each_with_object({}) do |commune, result|
|
30
|
+
administrative_unit = commune.administrative_unit
|
31
|
+
result[administrative_unit] ||= []
|
32
|
+
result[administrative_unit] << commune
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def format_number(number)
|
37
|
+
number.to_s.gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1,")
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
class Province < Location
|
42
|
+
def districts
|
43
|
+
Pumi::District.where(province_id: id).map { |district| District.new(district) }
|
44
|
+
end
|
45
|
+
|
46
|
+
def communes
|
47
|
+
Pumi::Commune.where(province_id: id)
|
48
|
+
end
|
49
|
+
|
50
|
+
def villages_count
|
51
|
+
Pumi::Village.where(province_id: id).count
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
class District < Location
|
56
|
+
def communes
|
57
|
+
Pumi::Commune.where(district_id: id)
|
58
|
+
end
|
59
|
+
|
60
|
+
def villages_count
|
61
|
+
Pumi::Village.where(district_id: id).count
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
PAGE_TITLE = "List_of_communes_in_Cambodia".freeze
|
66
|
+
INTRO_TEXT = "<div id=\"intro\">The '''communes of Cambodia''' ({{lang|km|ឃុំ}} ''[[khum]]''/{{lang|km|សង្កាត់}} ''[[sangkat]]'') are the third-level administrative divisions in Cambodia. They are the subdivisions of the [[List of districts in Cambodia|districts and municipalities of Cambodia]]. Communes can consist of as few as %<min_villages>s<ref>{{cite web|url=http://db.ncdd.gov.kh/gazetteer/view/commune.castle?cm=%<smallest_commune_id>s |title=%<smallest_commune_name>s |publisher=National Committee for Sub-National Democratic Development }}</ref> or as many as %<max_villages>s<ref>{{cite web|url=http://db.ncdd.gov.kh/gazetteer/view/commune.castle?cm=%<largest_commune_id>s |title=%<largest_commune_name>s |publisher=National Committee for Sub-National Democratic Development }}</ref> villages (''[[phum]]''), depending on the population.\nThere are a total of %<communes_count>s communes and %<villages_count>s villages in Cambodia.</div>".freeze
|
67
|
+
|
68
|
+
TEMPLATE = File.read("#{__dir__}/templates/commune_list.wikitext.erb")
|
69
|
+
|
70
|
+
def publish
|
71
|
+
replace_intro
|
72
|
+
replace_number_of_communes
|
73
|
+
replace_communes_list
|
74
|
+
client.update_page(title: PAGE_TITLE, source:, comment: "Update page")
|
75
|
+
end
|
76
|
+
|
77
|
+
private
|
78
|
+
|
79
|
+
def source_html
|
80
|
+
@source_html ||= Nokogiri::HTML(source)
|
81
|
+
end
|
82
|
+
|
83
|
+
def source
|
84
|
+
@source ||= page.fetch(:source).dup
|
85
|
+
end
|
86
|
+
|
87
|
+
def page
|
88
|
+
@page ||= client.get_page(title: PAGE_TITLE)
|
89
|
+
end
|
90
|
+
|
91
|
+
def intro_section
|
92
|
+
result = source_html.at_css('[id="intro"]')
|
93
|
+
raise "Could not find section with <div id=\"intro\">" if result.nil?
|
94
|
+
|
95
|
+
result
|
96
|
+
end
|
97
|
+
|
98
|
+
def communes_list_section
|
99
|
+
result = source_html.at_css('[id="communes-list"]')
|
100
|
+
raise "Could not find section with <div id=\"communes-list\">" if result.nil?
|
101
|
+
|
102
|
+
result
|
103
|
+
end
|
104
|
+
|
105
|
+
def replace_communes_list
|
106
|
+
provinces = Pumi::Province.all.map { |province| Province.new(province) }
|
107
|
+
data = OpenStruct.new(provinces:)
|
108
|
+
communes_list = ERB.new(TEMPLATE).result(data.instance_eval { binding })
|
109
|
+
source.sub!(communes_list_section.to_html, communes_list)
|
110
|
+
end
|
111
|
+
|
112
|
+
def replace_number_of_communes
|
113
|
+
source.sub!(
|
114
|
+
/current_number\s*=\s*(?:^|\s)(\d*\.?\d+|\d{1,3}(?:,\d{3})*(?:\.\d+)?)(?!\S)/,
|
115
|
+
"current_number = #{format_number(Pumi::Commune.all.size)}"
|
116
|
+
)
|
117
|
+
end
|
118
|
+
|
119
|
+
def replace_intro
|
120
|
+
villages = Pumi::Village.all.each_with_object({}) do |village, result|
|
121
|
+
result[village.commune] ||= []
|
122
|
+
|
123
|
+
result[village.commune] << village
|
124
|
+
end
|
125
|
+
|
126
|
+
village_sizes = villages.each_with_object({}) do |(commune, villages), result|
|
127
|
+
result[villages.size] = commune
|
128
|
+
end
|
129
|
+
|
130
|
+
min_villages = village_sizes.keys.min
|
131
|
+
max_villages = village_sizes.keys.max
|
132
|
+
|
133
|
+
smallest_commune = village_sizes[min_villages]
|
134
|
+
largest_commune = village_sizes[max_villages]
|
135
|
+
|
136
|
+
intro = format(
|
137
|
+
INTRO_TEXT,
|
138
|
+
min_villages:,
|
139
|
+
max_villages:,
|
140
|
+
smallest_commune_id: smallest_commune.id,
|
141
|
+
smallest_commune_name: smallest_commune.name_en,
|
142
|
+
largest_commune_id: largest_commune.id,
|
143
|
+
largest_commune_name: largest_commune.name_en,
|
144
|
+
communes_count: format_number(Pumi::Commune.all.count),
|
145
|
+
villages_count: format_number(Pumi::Village.all.count)
|
146
|
+
)
|
147
|
+
|
148
|
+
source.sub!(intro_section.to_html, intro)
|
149
|
+
end
|
150
|
+
|
151
|
+
def format_number(number)
|
152
|
+
number.to_s.gsub(/(\d)(?=(\d\d\d)+(?!\d))/, "\\1,")
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
157
|
+
end
|
@@ -0,0 +1,122 @@
|
|
1
|
+
require "ostruct"
|
2
|
+
|
3
|
+
module Pumi
|
4
|
+
module Bot
|
5
|
+
module Wikipedia
|
6
|
+
class DistrictsInCambodiaArticle < Article
|
7
|
+
PAGE_TITLE = "List_of_districts,_municipalities_and_sections_in_Cambodia".freeze
|
8
|
+
|
9
|
+
INTRO_TEXT = "This is a list of [[Cambodia]]'s %<districts_count>s districts ({{lang|km|ស្រុក}} ''srok''), %<municipalities_count>s district-level municipalities ({{lang|km|ក្រុង}} ''krong'') and %<sections_count>s sections ({{lang|km|ខណ្ឌ}} ''khan'') organized by each [[Provinces of Cambodia|province]] and an [[Provinces of Cambodia|autonomous municipality]] ([[Phnom Penh]]).".freeze
|
10
|
+
DISTRICTS_TEMPLATE = File.read("#{__dir__}/templates/district_list.wikitext.erb")
|
11
|
+
|
12
|
+
Misspelling = Struct.new(:incorrect_text, :correct_text, keyword_init: true)
|
13
|
+
|
14
|
+
MISSPELLINGS = [
|
15
|
+
Misspelling.new(incorrect_text: "Kratié", correct_text: "Kratie"),
|
16
|
+
Misspelling.new(incorrect_text: "Mondulkiri", correct_text: "Mondul Kiri"),
|
17
|
+
Misspelling.new(
|
18
|
+
incorrect_text: "Phnom Penh (autonomous municipality)",
|
19
|
+
correct_text: "Phnom Penh"
|
20
|
+
),
|
21
|
+
Misspelling.new(incorrect_text: "Ratanakiri", correct_text: "Ratanak Kiri"),
|
22
|
+
Misspelling.new(incorrect_text: "Siem Reap", correct_text: "Siemreap"),
|
23
|
+
Misspelling.new(incorrect_text: "Takéo", correct_text: "Takeo")
|
24
|
+
].freeze
|
25
|
+
|
26
|
+
def publish
|
27
|
+
page = client.get_page(title: PAGE_TITLE)
|
28
|
+
source = page.fetch(:source).dup
|
29
|
+
|
30
|
+
replace_intro(source:, replacement: generate_intro)
|
31
|
+
replace_number_of_districts(source:, replacement: District.all.size)
|
32
|
+
|
33
|
+
Pumi::Province.all.each do |province|
|
34
|
+
section = find_section_by_title(source:, title: province.name_en)
|
35
|
+
new_section = generate_districts_section(province:)
|
36
|
+
source.sub!(section[1], new_section)
|
37
|
+
end
|
38
|
+
|
39
|
+
client.update_page(title: PAGE_TITLE, source:, comment: "Update page")
|
40
|
+
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def generate_districts_section(province:)
|
45
|
+
districts = Pumi::District.where(province_id: province.id)
|
46
|
+
|
47
|
+
data = OpenStruct.new(
|
48
|
+
province:,
|
49
|
+
districts:,
|
50
|
+
districts_summary: generate_districts_summary(districts:)
|
51
|
+
)
|
52
|
+
result = ERB.new(DISTRICTS_TEMPLATE).result(data.instance_eval { binding })
|
53
|
+
"\n\n#{result}\n"
|
54
|
+
end
|
55
|
+
|
56
|
+
def generate_districts_summary(districts:)
|
57
|
+
summary = districts_by_type(collection: districts).map do |au, d|
|
58
|
+
next if Array(d).empty?
|
59
|
+
|
60
|
+
text = "#{d.size} #{au.name_en}"
|
61
|
+
if d.size > 1
|
62
|
+
au.name_en.end_with?("y") ? text.chomp!("y") << "ies" : text << "s"
|
63
|
+
end
|
64
|
+
text << " (#{au.name_km} #{au.name_latin})"
|
65
|
+
end
|
66
|
+
|
67
|
+
if summary.size <= 2
|
68
|
+
summary.join(" and ")
|
69
|
+
else
|
70
|
+
[summary[0..-2].join(", "), summary[-1]].join(" and ")
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
def find_section_by_title(source:, title:)
|
75
|
+
misspelling = MISSPELLINGS.find { |m| m.correct_text == title }
|
76
|
+
search_term = misspelling&.incorrect_text || title
|
77
|
+
|
78
|
+
section = source.match(/==\s*\[\[[[:word:]\s]+\|#{Regexp.escape(search_term)}\]\].+?==(.+?(?===))/m)
|
79
|
+
|
80
|
+
raise "Missing section for #{search_term}" if section.nil?
|
81
|
+
|
82
|
+
section
|
83
|
+
end
|
84
|
+
|
85
|
+
def generate_intro
|
86
|
+
all_districts = districts_by_type
|
87
|
+
|
88
|
+
format(
|
89
|
+
INTRO_TEXT,
|
90
|
+
districts_count: fetch_districts_by_type("District", collection: all_districts).size,
|
91
|
+
municipalities_count: fetch_districts_by_type(
|
92
|
+
"Municipality",
|
93
|
+
collection: all_districts
|
94
|
+
).size,
|
95
|
+
sections_count: fetch_districts_by_type("Section", collection: all_districts).size
|
96
|
+
)
|
97
|
+
end
|
98
|
+
|
99
|
+
def replace_number_of_districts(source:, replacement:)
|
100
|
+
source.sub!(/current_number\s*=\s*\d+/, "current_number = #{replacement}")
|
101
|
+
end
|
102
|
+
|
103
|
+
def replace_intro(source:, replacement:)
|
104
|
+
source.sub!(/This is a list of.+/, replacement)
|
105
|
+
end
|
106
|
+
|
107
|
+
def districts_by_type(collection: Pumi::District.all)
|
108
|
+
collection.each_with_object({}) do |district, result|
|
109
|
+
administrative_unit = district.administrative_unit
|
110
|
+
result[administrative_unit] ||= []
|
111
|
+
result[administrative_unit] << district
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
def fetch_districts_by_type(type, collection:)
|
116
|
+
administrative_unit = collection.keys.find { |au| au.name_en == type }
|
117
|
+
Array(collection[administrative_unit])
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|