-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnovel.php
966 lines (857 loc) · 42.5 KB
/
novel.php
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<style>
p { margin:0px 8px; }
p.new { margin:40px 8px 0px 8px; }
.divider { margin:60px; text-align:center; }
.endnote { font-size:0.8em; }
a { color:#999 }
</style>
</head>
<body style="font-family: 'Vollkorn', serif;">
<link href='https://fonts.googleapis.com/css?family=Vollkorn' rel='stylesheet' type='text/css'>
<?
$roomsleft = 5000;
$traveller = "Passepartout";
$localguide = "";
$localguidehome = "";
$laststation = ""; $passedplace = "";
$chapternumber = 0; $paragraphnumber = 1; $chaptersize = 99;
$lastspeaker = ""; $articlecount = 0;
$lastname = "";
$weather = 1;
$crossed180=0;
//makeChapter("Bull Ring, Birmingham",52.4777, -1.894178);
//makeChapter("Tottenham Court Road tube station",51.5162, -0.1309);
//makeChapter("London Waterloo station",51.5031,-0.1132);
//makeChapter("Reform Club",51.506667, -0.133333);
$leap = 0.005;
$farside = 0;
$elapsedseconds = 9*60*60;
$fastovershot = 0;
$checkpoint = 0;
$accelerating = 0; $prevlat = 0; $prevlon = 0;
$lastprintedlat = 0; $lastprintedlon = 0;
$opening = 0;
$crossed180=0;
list($mylat,$mylon) = fetchCoords("Reform Club");
travelTo("Paris");
travelTo("Brussels");
travelTo("Oslo");
travelTo("Florence");
travelTo("Berlin");
travelTo("Vienna");
travelTo("Stockholm");
travelTo("Moscow");
travelTo("New York City");
travelTo("Reform Club");
?>
<p> Our time was up, and our journey was at an end.</p>
<hr>
<p class="endnote">This edition of <i>Around The World in <?=$articlecount?> Wikipedia Articles</i> was autogenerated on <?=date("l jS \of F Y")?> using the Wikipedia articles named
in full in each paragraph, as well as the biography articles of the subjects of any statues mentioned. The story takes a route passing through the <a
href="http://www.kmjn.org/notes/geographically_dense_wikipedia.html">ten most Wikipedia-documented metropolitan areas</a>, and was run through a generative script written by <a
href="http://kevan.org">Kevan Davis</a> for <a href="https://github.com/dariusk/NaNoGenMo-2015">NaNoGenMo</a> in November 2015. The articles' text was made available under a
Creative Commons Attribution-ShareAlike 3.0 Unported Licence, and the text of this edition is released under that same licence.</p>
<?
exit;
exit;
function travelTo($destination)
{
global $mylat,$mylon,$targetlat,$targetlon,$checkpoint;
list($targetlat,$targetlon) = fetchCoords($destination);
visitPoint($mylat,$mylon,$destination);
//print "<p>(We arrived at $destination ($targetlat,$targetlon).)</p>";
$checkpoint=0;
$mylat = $targetlat;
$mylon = $targetlon;
$done = array(); // throw away array of visited locations
}
function visitPoint($lat,$lon,$destination)
{
global $roomsleft,$done,$leap,$chapternumber,$farside,$targetlat,$targetlon,$elapsedseconds,$fastovershot,$opening,
$checkpoint,$laststation,$accelerating,$prevlat,$prevlon,$lastprintedlat,$lastprintedlon,$paragraphnumber,$crossed180,$passedplace,$articlecount,
$chaptersize;
$json = fetchArticlesNear($lat,$lon);
$json_array = json_decode($json, true);
//print "(visiting $lat/$lon at speed $leap)";
$pagetitle = "unknown"; $pagelat = 0; $pagelon = 0;
if (isset($json_array['query']['pages']))
{
foreach($json_array['query']['pages'] as $page){
if ($pagetitle == "unknown" && isset($page['title']) && !isset($done[$page['title']]))
{
$pagetitle = $page['title'];
$done[$page['title']] = 1;
if (isset($page['coordinates'][0]['lat']) && isset($page['coordinates'][0]['lon']))
{
//$offset = (hexdec( substr(md5($pagetitle), 0, 5) ))/10000000000;
$pagelat = $page['coordinates'][0]['lat'];
$pagelon = $page['coordinates'][0]['lon'];
if (isset($page['coordinates'][0]['type'])) { $pagetype = $page['coordinates'][0]['type']; } else { $pagetype = "unknown"; }
/*
$dx = floor(($pagelon + $offset - $startlon)*10000);
$dy = floor(($pagelat + $offset - $startlat)*10000);
$angle = 90-rad2deg(atan2($dy,$dx));
$distance = sqrt(($dx*$dx)+($dy*$dy));
$direction = floor(($angle+22.5)/45+8)%8;
*/
}
}
}
}
if (preg_match("/station/",$pagetitle) || preg_match("/(port|dock)/",$pagetitle) || preg_match("/airport/",$pagetitle))
{
$laststation = $pagetitle;
}
if ($pagelat == 0 && $pagelon == 0)
{
//print "(nothing at $lat,$lon; accelerating to $leap)";
$leap = $leap*2; $accelerating=1;
if ($leap>0.5) {$leap=0.5; }
}
else if ($accelerating == 1 && $leap>0.05) // Hit this point while accelerating at speed, so back up to previous point and slow down.
{
//print "(backing up to $prevlat, $prevlon) ";
$pagelat = $prevlat;
$pagelon = $prevlon;
$leap = 0.001;
$accelerating = 0;
}
else
{
$elapsedseconds=floor($elapsedseconds+300);
$chapter = makeChapter($pagetitle,$pagelat,$pagelon);
if ($opening == 0)
{
$passedplace = $pagetitle; // don't have an opening paragraph, just note the article title
$opening = 1;
}
elseif (strlen($chapter) > 300 && $pagetitle <> "unknown") // only use the text if it's long enough, and a real article
{
$dx=$lastprintedlat-$pagelat;
$dy=$lastprintedlon-$pagelon;
$distance = sqrt($dx*$dx+$dy*$dy);
$minuteswalked = $distance*2000;
if ($opening == 1) { $opening = 2; } // have now printed the opening paragraph of the book
$chaptersize++;
if ($minuteswalked>=240)
{
if ($chaptersize>10)
{
$chapternumber++;
print "<h2>Chapter $chapternumber</h2>";
$chaptersize = 0;
}
else
{
print "<div class=\"divider\"><img src=\"http://kevan.org/images/divider.png\" width=131 height=18 alt=\"---\"></div>";
}
}
//print "(distance=$distance / $lastprintedlat-$pagelat / $lastprintedlon-$pagelon)";
//print "(".gmdate("H:i:s", $elapsedseconds)." - ".($leap*250)." miles) - ";
print $chapter; //." (speed=$leap)";
$roomsleft--;
$leap = 0.001;
$lastprintedlat = $pagelat;
$lastprintedlon = $pagelon;
$articlecount++;
$paragraphnumber++;
if ($paragraphnumber > rand(5,10)) { $paragraphnumber = 1; }
}
else
{
if ($pagetitle <> "unknown" && $opening==2) { $passedplace = $pagetitle; }
//print "(at $lat,$lon; accelerating to $leap)";
$leap = $leap*2; $accelerating=1;
if ($leap>0.5) {$leap=0.5; }
}
}
if ($pagelat != 0 && $pagelon != 0)
{
$checklon = $pagelon;
if ($checklon>180) { $checklon = -360+$checklon; } // reverse any offset for western hemisphere
$checktarglon = $targetlon;
if ($checktarglon>180 && $crossed180 == 1) { $checktarglon = -360+$checktarglon; } // reverse any offset for western hemisphere
if ($checklon>$checktarglon) // count passing the longitude as meeting the checkpoint
{ $checkpoint = 1; }
}
if ($roomsleft>0 && $checkpoint == 0)
{
if ($pagelat <> 0) { $lat = $pagelat; }
if ($pagelon <> 0) { $lon = $pagelon; }
$dx = floor(($targetlon-$lon)*10000);
$dy = floor(($targetlat-$lat)*10000);
$heading = atan2($dx,$dy);
// print "[heading $heading at $leap : lon+".$leap*sin($heading).", lat+".$leap*cos($heading)."";
if ((($lon+$leap*sin($heading)) > $targetlon)) // if overshooting target city, slow right down
{
//print "(overshot, ".$lon."+".$leap."*".sin($heading).">$targetlon, was going at $leap, decelerating)";
$leap = 0.001;
}
$lon += $leap*sin($heading);
$lat += $leap*cos($heading);
$prevlat = $lat;
$prevlon = $lon;
if ($lon>180) { $crossed180 = 1; }
if ($destination == $pagetitle) { $checkpoint = 1; } // have definitely arrived if we've hit the target article
if ($checkpoint == 0) { visitPoint($lat,$lon,$destination); }
}
}
function makeChapter($startpoint,$startlat,$startlon,$direction=-1)
{
global $roomsleft,$done,$localguide,$localguidehome,$traveller,$leap,$laststation,$weatherlist,$weather,$lastspeaker,$lastprintedlat,
$lastprintedlon,$opening,$paragraphnumber,$passedplace;
$lastspeaker = "";
$ourgroup = "$traveller and I";
if ($localguide != "")
{ $ourgroup = "$traveller, $localguide and I"; }
$dx=$lastprintedlat-$startlat;
$dy=$lastprintedlon-$startlon;
$distance = sqrt($dx*$dx+$dy*$dy);
$minuteswalked = $distance*2000;
if ($minuteswalked>=240) // time for a new chapter
{ $paragraphnumber = 1; }
$chapter = "";
if ($paragraphnumber == 1)
{ $chapter .= "<p class=\"new\">"; }
else
{ $chapter .= "<p> "; }
list($original,$article) = fetchArticle($startpoint);
$cleanstartpoint = preg_replace("/[^A-Za-z 0-9,']/","",$startpoint);
$speaker = $traveller;
if (rand(1,2)==1 && $localguide <> "") { $speaker = $localguide; }
if ($opening==1)
{
$chapter .= "Setting out from $passedplace, we headed for ".$startpoint.". ";
}
elseif (preg_match("/'''".$cleanstartpoint."''' was/",$original,$matches))
{
switch (rand(1,4)) {
case 1; $chapter .= "We passed the site of ".$startpoint.". "; break;
case 2; $chapter .= "Near here was the site of ".$startpoint.". "; break;
case 3; $chapter .= "I remembered ".$startpoint.". "; break;
case 4; $chapter .= "$speaker told me about ".$startpoint.". "; break;
}
}
elseif (preg_match("/HMS/",$startpoint,$matches))
{
switch (rand(1,4)) {
case 1; $chapter .= "We drifted past ".$startpoint.". "; break;
case 2; $chapter .= "We passed ".$startpoint.". "; break;
case 3; $chapter .= "Through the fog we saw ".$startpoint.". "; break;
case 4; $chapter .= "$speaker pointed out ".$startpoint.". "; break;
}
}
elseif (preg_match("/^List of (.+)$/",$startpoint,$matches)) // some list articles have geotags, it turns out
{
switch (rand(1,4)) {
case 1; $chapter .= "While we travelled, I ".randString("perused","skimmed","read")." a list of ".$startpoint.". "; break;
case 2; $chapter .= "My guidebook provided a list of ".$startpoint.". "; break;
case 3; $chapter .= "Unexpectedly, $traveller began to ".randString("reel","read")." out a list of ".$startpoint.". "; break;
case 4; $chapter .= "I ".randString("perused","skimmed","read")." a list of ".$startpoint.". "; break;
}
}
else
{
if ($minuteswalked<1)
{
switch (rand(1,6)) {
case 1; $chapter .= "Right around the corner was ".$startpoint.". "; break;
case 2; $chapter .= "We ".randString("walked","strolled","wandered")." a short distance to ".$startpoint.". "; break;
case 3; $chapter .= "We approached ".$startpoint.". "; break;
case 4; $chapter .= "Nearby was ".$startpoint.". "; break;
case 5; $chapter .= "Close by was ".$startpoint.". "; break;
case 6; $chapter .= "This was also the location of ".$startpoint.". "; break;
}
}
elseif ($minuteswalked<20)
{
$passing = randString("Crossing a main road","Moving through the crowds");
if ($passedplace <> "") { $passing = "Passing $passedplace"; $passedplace = ""; }
switch (rand(1,11)) {
case 1; $chapter .= "We ".randString("walked at a brisk pace","strolled","wandered","walked")." to ".$startpoint.". "; break;
case 2; $chapter .= "We ".randString("walked","strolled","wandered")." a short distance to ".$startpoint.". "; break;
case 3; $chapter .= "We ".randString("sprinted","ran")." to ".$startpoint.". "; break;
case 4; $chapter .= "A few streets away was ".$startpoint.". "; break;
case 5; $chapter .= "Moving on, we arrived at ".$startpoint.". "; break;
case 6; $chapter .= "With ".randString("some urgency","no time to spare","no time to lose").", we ".randString("ran","walked")." to ".$startpoint.". "; break;
case 7; $chapter .= "It was a short walk to ".$startpoint.". "; break;
case 8; $chapter .= "$ourgroup walked to ".$startpoint.". "; break;
case 9; $chapter .= "$traveller suggested that we ".randString("pay a visit to","visit")." ".$startpoint.". "; break;
case 10; $chapter .= "$passing, we approached ".$startpoint.". "; break;
case 11; $chapter .= "";
$newweather = rand(1,9);
while ($newweather == $weather) { $newweather = rand(1,9); }
$weather = $newweather;
switch ($weather) {
case 1; $chapter .= "It began to rain"; break;
case 2; $chapter .= "The sun came out"; break;
case 3; $chapter .= "The sky clouded over"; break;
case 4; $chapter .= "A cold wind picked up"; break;
case 5; $chapter .= "The sky darkened"; break;
case 6; $chapter .= "A fog descended"; break;
case 7; $chapter .= "The weather improved"; break;
case 8; $chapter .= "The weather worsened"; break;
case 9; $chapter .= "Thunder cracked in the distance"; break;
}
$chapter .= " as we approached ".$startpoint.". "; break;
}
}
elseif ($minuteswalked<60)
{
if ($localguide != "" && rand(1,10)==1)
{
switch (rand(1,2)) {
case 1; $chapter .= "$localguide gave us a lift to ".$startpoint.". "; break;
case 2; $chapter .= "$localguide drove us to ".$startpoint.". "; break;
}
}
else
{
switch (rand(1,10)) {
case 1; $chapter .= "We caught a cab to ".$startpoint.". "; break;
case 2; $chapter .= "We hailed a cab to ".$startpoint.". "; break;
case 3; $chapter .= "We hitched a lift to ".$startpoint.". "; break;
case 4; $chapter .= "Some distance further was ".$startpoint.". "; break;
case 5; $chapter .= "Our next stop was ".$startpoint.". "; break;
case 6; $chapter .= "Moving on, we arrived at ".$startpoint.". "; break;
case 7; $chapter .= "With no time to lose, we travelled to ".$startpoint.". "; break;
case 8; $chapter .= "We made our way to ".$startpoint.". "; break;
case 9; $chapter .= "$ourgroup hitched a lift to ".$startpoint.". "; break;
case 10; $chapter .= "$traveller flagged down a bus to ".$startpoint.". "; break;
}
}
}
elseif ($minuteswalked<120)
{
if (!preg_match("/station/",$startpoint) && $laststation != "")
{
$chapter .= "".randString("With poor weather ahead, we ","Lost, we ","Unable to travel further on foot, we","We still had a long way to go, so we ","We")." ".randString("retraced our steps to","doubled back to","made our way to","walked to")." $laststation and ";
if (preg_match("/station/",$laststation))
{ $chapter .= "caught ".randString("an express","a","the next")." train, alighting near ".$startpoint.". "; }
elseif (preg_match("/airport/",$laststation))
{ $chapter .= "booked ourselves onto ".randString("a private","a","the next")." plane, alighting near ".$startpoint.". "; }
elseif (preg_match("/(dock|port)/",$laststation))
{ $chapter .= "arranged passage on ".randString("a fishing","a passenger","a","the next")." boat. Some time later, it docked near ".$startpoint." and we walked the rest of the way. "; }
else
{ $chapter .= "arranged transport to ".$startpoint.". "; }
$laststation = "";
}
else
{
switch (rand(1,5)) {
case 1; $chapter .= "We alighted from a bus ".randString("not too far from","near")." ".$startpoint.". "; break;
case 2; $chapter .= "Our train arrived ".randString("not too far from","near")." ".$startpoint.". "; break;
case 3; $chapter .= "We chartered a private plane which touched down ".randString("not too far from","near")." ".$startpoint.". "; break;
case 4; $chapter .= "Our coach pulled up at ".$startpoint.". "; break;
case 5; $chapter .= "We parked ".randString("not too far from","near")." ".$startpoint.". "; break;
}
}
}
else
{
if (!preg_match("/station/",$startpoint) && $laststation != "")
{
$chapter .= "".randString("The weather was worsening, so we ","Lost, we ","Time was running short, so we ","We")." ".randString("retraced our steps to","doubled back to","made our way to","walked to")." $laststation and ";
if (preg_match("/station/",$laststation))
{ $chapter .= "caught ".randString("an express","a","the next")." train, alighting near ".$startpoint.". "; }
elseif (preg_match("/airport/",$laststation))
{ $chapter .= "booked ourselves onto ".randString("a private","a","the next")." plane, alighting near ".$startpoint.". "; }
elseif (preg_match("/(dock|port)/",$laststation))
{ $chapter .= "arranged passage on ".randString("a fishing","a passenger","a","the next")." boat. Some time later, it docked near ".$startpoint." and we walked the rest of the way. "; }
else
{ $chapter .= "arranged transport to ".$startpoint.". "; }
$laststation = "";
}
else
{
switch (rand(1,5)) {
case 1; $chapter .= "We alighted from a ".randString("coach","bus")." near ".$startpoint.". "; break;
case 2; $chapter .= "Our train arrived near ".$startpoint.". "; break;
case 3; $chapter .= "Much later we arrived near ".$startpoint.". "; break;
case 4; $chapter .= "Our ".randString("bus","coach")." pulled up at ".$startpoint.". "; break;
case 5; $chapter .= "We parked near ".$startpoint.". "; break;
}
}
}
}
$bracketedarticle = "(its |the |their |his |her |a |an )?";
$mainwas = ""; $secondarywas = ""; $wasoff = 0; $wasphrase = rand(1,10);
if (preg_match("/ leads? to([^\".,]{10,100})[.,]/",$article,$matches))
{
$chapter .= cleanLine("$speaker ".randString("made a joke about","told us a long story about","pointed out","talked about")." ".$matches[1].". ");
}
if (preg_match("/ was ([^.,]{20,100})[.,]/",$article,$matches,PREG_OFFSET_CAPTURE))
{
list($mainwas,$wasoff) = $matches[1];
switch ($wasphrase) {
case 1; $chapter .= cleanLine("$speaker told me it was ".$mainwas.". "); break;
case 2; $chapter .= cleanLine("I could see that it was ".$mainwas.". "); break;
case 3; $chapter .= cleanLine("It was clearly ".$mainwas.". "); break;
case 4; $chapter .= cleanLine("Unless I was ".randString("very much mistaken","mistaken").", this was ".$mainwas.". "); break;
case 5; $chapter .= cleanLine("I ".randString("recalled","remembered")." it was ".$mainwas.". "); break;
case 6; $chapter .= cleanLine("My guidebook ".randString("told me","claimed")." it was ".$mainwas.". "); break;
case 7; $chapter .= cleanLine("It looked as if it was ".$mainwas.". "); break;
case 8; $chapter .= cleanLine("$traveller asked me if it was ".$mainwas.", but I did not know. "); break;
case 9; $chapter .= cleanLine("".randString("As every schoolboy knows",
"To the best of my recollection","As far as I could remember","If I recalled correctly",
"To the best of my knowledge","As far as I knew","If I remembered correctly").", this was ".$mainwas.". "); break;
case 10; $chapter .= cleanLine("Imagine my ".randString("amazement","astonishment","surprise")." to learn it was ".$mainwas."! "); break;
}
}
if ($wasoff>0 && preg_match("/ was ([^.,]{20,100})[.,]/",$article,$matches,NULL,$wasoff))
{
$secondarywas = $matches[1]; //$chapter .= "WHAT'S MORE: ";
$secondphrase = rand(1,10);
while ($secondphrase == $wasphrase)
{ $secondphrase = rand(1,10); }
$secondspeech = $traveller;
if ($wasphrase == 8 || ($wasphrase == 1 && $speaker == $traveller)) { $secondspeech = "He"; }
switch ($secondphrase) {
case 1; $chapter .= cleanLine("$secondspeech said it was ".$matches[1].". "); break;
case 2; $chapter .= cleanLine("I could see that it was ".$matches[1].". "); break;
case 3; $chapter .= cleanLine("It was clearly ".$matches[1].". "); break;
case 4; $chapter .= cleanLine("Unless I was ".randString("thinking of something else","very much mistaken","mistaken").", this was ".$matches[1].". "); break;
case 5; $chapter .= cleanLine("I ".randString("recalled","remembered")." it was ".$matches[1].". "); break;
case 6; $chapter .= cleanLine("My guidebook ".randString("said","claimed")." it was ".$matches[1].". "); break;
case 7; $chapter .= cleanLine("It looked as if it was ".$matches[1].". "); break;
case 8; $chapter .= cleanLine("$secondspeech asked me if it was ".$matches[1].", but I did not know. "); break;
case 9; $chapter .= cleanLine("".randString("To the best of my knowledge","As far as I knew","If I remembered correctly").", this was ".$matches[1].". "); break;
case 10; $chapter .= cleanLine("Imagine my ".randString("astonishment","surprise")." to learn it was ".$matches[1]."! "); break;
}
}
if (preg_match("/ (was|did) not ([^.,]{10,100})[.,]/",$article,$matches))
{
$was = $matches[1];
$subject = $matches[2];
if ("not ".$subject != $mainwas && "not ".$subject != $secondarywas)
{
if (preg_match("/\b(him|his|himself|he)\b/",$subject,$submatches))
{ $chapter .= cleanLine("$traveller ".randString("admitted","said")." that he ".$was."n't ".$matches[2].". "); }
elseif (preg_match("/\b(her|herself|she)\b/",$subject,$submatches))
{ $chapter .= cleanLine("$traveller ".randString("believed","commented")." that she ".$was."n't ".$matches[2].". "); }
elseif (preg_match("/(their|them|themselves|they)/",$subject,$submatches))
{ $chapter .= cleanLine("$traveller ".randString("remarked","commented","observed")." that they ".$was."n't ".$matches[2].". "); }
elseif (preg_match("/^(support|believe|think|feel)/",$subject,$submatches))
{ $chapter .= cleanLine("$traveller ".randString("admitted","remarked","said")." that he ".$was."n't ".$matches[2].". "); }
else
{ $chapter .= cleanLine("$traveller ".randString("commented","remarked","observed")." that it ".$was."n't ".$matches[2].". "); }
}
}
if (preg_match("/(was|is) home (of|to) ([^.,]{10,100})[.,]/",$article,$matches) && !preg_match("/home/",$mainwas))
{
if ("home of ".$matches[3] != $mainwas && "home of ".$matches[3] != $secondarywas && "home to ".$matches[3] != $mainwas && "home to ".$matches[3] != $secondarywas)
{
$chapter .= cleanLine("We visited ".$matches[3].". ");
}
}
if (preg_match("/ gave its name to $bracketedarticle([^.,]{10,100})[.,]/",$article,$matches))
{
$chapter .= cleanLine(randString("$speaker joked about","$speaker talked about","I recalled")." the ".$matches[2]." of the same name. ");
}
if (preg_match("/ commemorates ([^.,]{10,100})[.,]/",$article,$matches))
{
$chapter .= cleanLine("$traveller and I ".randString("silently remembered","reminisced about","swapped stories about","recalled")." ".$matches[1].". ");
}
if (preg_match("/(this|it) became ([^.,]{6,100})[.,]/",$article,$matches))
{
$chapter .= cleanLine("$speaker remembered it becoming ".$matches[2].". ");
}
if (preg_match("/[Ww]hich would be ([^.,]{6,100})[.,]/",$article,$matches))
{
$chapter .= cleanLine("We ".randString("disagreed as to whether","thought","agreed")." it was ".$matches[1].". ");
}
if (preg_match("/produced ([^.,]{12,100})[.,]/",$article,$matches))
{
$chapter .= cleanLine(randString("$speaker and I","We")." enjoyed ".$matches[1].". ");
}
if (preg_match("/there (were|are) (five|six|seven|eight|nine|ten) ([A-Za-z\-\']+s) /",$article,$matches))
{
$plus = "six";
if ($matches[2]=="six") { $plus = "seven"; }
if ($matches[2]=="seven") { $plus = "eight"; }
if ($matches[2]=="eight") { $plus = "nine"; }
if ($matches[2]=="nine") { $plus = "ten"; }
if ($matches[2]=="ten") { $plus = "eleven"; }
$chapter .= cleanLine("Were there ".$matches[2]." or ".$plus." ".$matches[3]."? $traveller ".randString("didn't seem to care","wasn't sure","thought ".$matches[2]).". ");
}
if (preg_match("/ still (retain|show|feature)s? some (of )?$bracketedarticle([^.,]+)[.,]/",$article,$matches))
{
$chapter .= cleanLine("I admired the ".$matches[4].". ");
}
if (preg_match("/where ([\w ]+) is located/",$article,$matches))
{
$chapter .= cleanLine("We had a".randString("marvellous","good","n excellent"," fine")." view of ".$matches[1]." from here. ");
}
if (preg_match("/with a ([\w ]+) design/",$article,$matches))
{
$chapter .= cleanLine("We ".randString("argued over the merits of","considered","admired","noted")." its ".$matches[1]." design. ");
}
if (preg_match("/(has|uses) (an? )([^.,]{6,100})[,.]/",$article,$matches)) // was ending on " and"
{
$chapter .= cleanLine("".randString("We joined some tourists who were admiring","$speaker and I admired","I admired","We admired")." the ".$matches[3].". ");
}
if (preg_match("/used as ([^.,]{6,100})[,.]/",$article,$matches))
{
if (rand(1,2)==1)
{ $chapter .= cleanLine("It ".randString("was hard to believe it was ever","functioned well as")." ".$matches[1].". "); }
else
{
$question = cleanLine("%$traveller%\"".randString("Was this really","Are you sure this was","Did you say this was")." ".$matches[1]."?\" ".randString("wondered","asked")." $traveller. ");
$answer = cleanLine("%narrator%\"".randString("See for yourself","Now you ask, I am not so sure","I believe so","But of course","Naturally","Certainly","I think so","Yes").".\" I ".randString("said","replied").". ");
if ($question != "" && $answer != "")
{ $chapter .= $question.$answer; }
}
}
elseif (preg_match("/used for ([^.,]{6,100})[,.]/",$article,$matches))
{
if (rand(1,2)==1)
{ $chapter .= cleanLine("It seemed ".randString("a fine spot","quite suitable","perfect")." for ".$matches[1].". "); }
else
{ $chapter .= cleanLine("%$traveller%\"".randString("I can't imagine a worse place","I imagine this is a place","I suppose it's alright","I wonder if this is a place")." for ".$matches[1].".\" said $traveller. "); }
}
if (preg_match("/an example of $bracketedarticle([^.,]{6,100})[,.]/",$article,$matches))
{
$chapter .= cleanLine("$traveller took ".randString("several photographs of","notes on","a photograph of")." the ".$matches[2].". ");
}
if (preg_match("/the (noise|sound) of $bracketedarticle([^.,]+)[,.]/",$article,$matches))
{
$chapter .= cleanLine("I could hardly hear $speaker over the ".randString("noise","sound")." of the ".$matches[3].". ");
}
if (preg_match("/intended to ([^.,]+)[,.]/",$article,$matches))
{
if (rand(1,2) == 1)
{
if (preg_match("/^be (.+)$/",$matches[1],$morematches))
{ $chapter .= cleanLine("Was it ".$morematches[1]."? We ".randString("thought so","thought not","were not sure").". "); }
else
{ $chapter .= cleanLine("Did it ".$matches[1]."? We ".randString("thought so","thought not","were not sure").". "); }
}
else
{
if (preg_match("/^be (.+)$/",$matches[1],$morematches))
{ $chapter .= cleanLine("*$traveller*\"Was it ".$morematches[1]."?\" asked $traveller. ");
if ($localguide != "" && rand(1,2)==1)
{ $chapter .= cleanLine("*narrator*\"I ".randString("couldn't care less","think so","doubt it","think not","am not sure","believe so")."?\" said $localguide."); }
else
{ $chapter .= cleanLine("*narrator*\"I ".randString("think so","doubt it","think not","think not","am not sure")."?\" I said."); }
}
else
{ $chapter .= cleanLine("*$traveller*\"Did it ".$matches[1]."?\" asked $traveller. ");
if ($localguide != "" && rand(1,2)==1)
{ $chapter .= cleanLine("*narrator*\"I ".randString("couldn't care less","think so","doubt it","think not","believe so","am not sure").".\" said $localguide."); }
else
{ $chapter .= cleanLine("*narrator*\"I ".randString("think so","doubt it","think not","think not","am not sure").".\" I said."); }
}
}
}
if (preg_match("/(contains|houses|housed|contained) (an|a|the|some) ([^.,]{20,100})[,.]/",$article,$matches))
{
$chapter .= cleanLine("".randString("I tripped over","$traveller held a magnifying glass up to","$traveller examined","$traveller was unimpressed by","I found and admired")." ".$matches[2]." ".$matches[3].". ");
}
elseif (preg_match("/(contains) ([^.,]+)[,.]/",$article,$matches))
{
$chapter .= cleanLine("I spent ".randString("over an hour","a short time","a few minutes","some time","a while")." ".randString("perusing","studying","examining")." ".$matches[2].". ");
}
if (preg_match("/access to ([^.,]+)[,.]/",$article,$matches))
{
$chapter .= cleanLine("It ".randString("was certainly handy for","was handy for","was within convenient distance of","was convenient for","seemed handy for")." ".$matches[1].". ");
}
if (preg_match("/provides ([^.,]+)[,.]/",$article,$matches))
{
$chapter .= cleanLine("We ".randString("helped ourselves to","made full benefit of","took advantage of")." ".$matches[1].". ");
}
if (preg_match("/in common with ([^.,]+)[,.]/",$article,$matches))
{
$chapter .= cleanLine("It seemed ".randString("indistinguishable from","broadly similar to","quite similar to")." ".$matches[1].". ");
}
if (preg_match("/was (subsequently|eventually) ([^.,]{6,100})[,.]/",$article,$matches))
{
$chapter .= cleanLine("$traveller ".randString("recalled the time before it was","mourned it having been","recounted how it had been","spoke of the time when it was")." ".$matches[2].". ");
}
if (preg_match("/known for (the|its|their) ([^.,]+)[,.]/",$article,$matches))
{
if (rand(1,2)==1)
{ $chapter .= cleanLine("We ".randString("took some time to enjoy","admired","enjoyed")." the ".randString("legendary","famous")." ".$matches[2].". "); }
else
{ $chapter .= cleanLine("%$traveller%\"".randString("Look! The","Look at the","Just look at the")." ".$matches[2]."!\" said $traveller. "); }
}
if (preg_match("/few (of the |of its )?([^.,]{6,100}) (remain|survive)/",$article,$matches))
{
$chapter .= cleanLine("We looked for ".$matches[2]." but ".randString("struggled to","weren't able to","could not")." find any. ");
}
if (preg_match("/designed to ([^.,]+)[,.]/",$article,$matches))
{
$chapter .= cleanLine("$speaker ".randString("explained to us","explained")." how it had been designed to ".$matches[1].". ");
}
if (preg_match("/was (apparently|probably) ([^.,]+)[,.]/",$article,$matches))
{
$chapter .= cleanLine("$speaker ".randString("whispered","said")." it was ".randString("rumoured","believed","thought")." to have been ".$matches[2].". ");
}
if (preg_match("/it would be ([^.,]+)[,.]/",$article,$matches))
{
$chapter .= cleanLine("$traveller disapproved of it being ".$matches[1].". ");
}
if (preg_match("/connected by $bracketedarticle([^.,]+)[,.]/",$article,$matches))
{
$chapter .= cleanLine("We traversed the ".$matches[2].". ");
}
if (preg_match("/until (after )?([^12][^.,]{30,100})[,.]/",$article,$matches))
{
$chapter .= cleanLine("".randString("Times certainly changed","The mood here changed","Things were never the same","Things changed","Things were different")." after ".$matches[2].". ");
}
if (preg_match("/a production of ([^.,]{10,30})[,.]/",$article,$matches))
{
$chapter .= cleanLine("We ".randString("attended a disappointing","attended a mediocre","enjoyed an adequate","enjoyed a fine")." production of ".$matches[1].". ");
}
if (preg_match("/(signed|uilt|irected|culpted) by ([A-Z][^ ,\.]{3,20}) [A-Z]/",$article,$matches) && $localguide == "")
{
$stranger = $matches[2];
if (!strpos($chapter,$stranger)) // Only use a name if we've not already mentioned it.
{
$enterstageleft = "";
if (rand(1,2)==1)
{ $enterstageleft = cleanLine("We ".randString("were hailed by","were greeted by","bumped into","entered into conversation with","were approached by","encountered").
" a".
randString(" mysterious figure"," taxi driver"," dishevelled character"," local shopkeeper"," passer-by"," fellow tourist"," local resident"," local").
", who introduced themselves as ".$stranger.". "); }
else
{ $enterstageleft = cleanLine("We met ".$stranger.", a".
randString("n old friend and terrible bore","n old friend of ".$traveller."'s"," college friend of mine","n old colleague of mine","n old friend of mine").
" who ".
randString("pulled up in a car","worked nearby","had heard about our journey","was in the area","lived nearby","was passing by").". "); }
if ($enterstageleft != "")
{
$chapter .= $enterstageleft;
$localguide = $stranger;
$localguidehome = $startpoint;
}
}
}
elseif (preg_match("/ (tavern|pub|restaurant|train station|hostel|hotel|cinema|theatre|shop|bar|museum|house) /",$article,$matches) && rand(1,3)==1 && $localguide <> "")
{
if (rand(1,2)==1)
{ $chapter .= cleanLine("At this point, $localguide ".randString("hailed a cab","departed from our company","noticed the time and said they had to leave","had to leave","reluctantly left us").", saying that they had ".randString("to return to","to return home via","business to attend to back at")." $localguidehome. "); }
else
{ $chapter .= cleanLine("At this point, $localguide entered a nearby ".$matches[1]." and bade us farewell. "); }
$localguide = "";
}
if (preg_match("/statue of \[\[([^\]]+)\]\]/",$original,$matches) && !preg_match("/[Ss]tatue/",$startpoint))
{
$statue = $matches[1];
$statue = preg_replace("/\|.+$/","",$statue);
list($statueoriginal,$statuearticle) = fetchArticle($statue);
if (preg_match("/ (is|was) ([^.,]{10,100})[.,]/",$statuearticle,$matches))
{
$chapter .= cleanLine("".randString("We couldn't fail to notice","$traveller looked up at","We noted","We admired")." the statue of ".$statue.", ");
$chapter .= cleanLine("".$matches[2].". ");
if (preg_match("/wore ([^.,]{10,100})[.,]/",$statuearticle,$matches))
{
$chapter .= cleanLine("$traveller thought the statue should have been wearing ".$matches[1].". ");
}
elseif (preg_match("/dressed in ([^.,]{10,100})[.,]/",$statuearticle,$matches))
{
$chapter .= cleanLine("$traveller thought the statue should have been wearing ".$matches[1].". ");
}
if (preg_match("/\"([^.,]{10,100})\",? (s?he) said/",$statuearticle,$matches))
{
$chapter .= cleanLine("Who could forget that famous line, \"".$matches[1]."\". ");
}
if (preg_match("/ had a ([^.,]{10,100})[.,]/",$statuearticle,$matches))
{
$chapter .= cleanLine("The sculptor had captured the ".$matches[1]." perfectly. ");
}
if (preg_match("/(s?he) (is|was) ([^.,]{10,100})[.,]/",$statuearticle,$matches))
{
$chapter .= cleanLine("".randString("We were surprised that","We were impressed that","To think that","It seemed fitting")." somebody who was ".$matches[3]." should have ".randString("such ","")."a statue in $startpoint. ");
}
}
else
{
$chapter .= cleanLine("".randString("We couldn't fail to notice","$traveller looked up at","We noted","We admired")." the statue of ".$statue.". ");
}
}
if (preg_match("/\"([A-Z][^.!\"]{10,200}\.)/",$article,$matches) && $localguide <> "")
{
$dialogue = $matches[1];
$dialogue = preg_replace("/ .$/","...",$dialogue);
$chapter .= cleanLine("%local%\"".$dialogue."\" said $localguide. ");
}
elseif (preg_match("/\"([A-Z][^.!\"]{10,200}!)/",$article,$matches) && $localguide <> "")
{
$chapter .= cleanLine("%local%\"".$matches[1]."\" exclaimed $localguide. ");
}
elseif (preg_match("/\"([A-Z][^\?\"]{10,200}\?)/",$article,$matches) && $localguide <> "")
{
$chapter .= cleanLine("%local%\"".$matches[1]."\" asked $localguide. ");
}
if (preg_match("/due to ([^b][^e][^.,]{6,100})[,.]/",$article,$matches))
{
$chapter .= cleanLine(randString("Our visit was cut short due to ","We had heard stories of ","We encounted some problems with ",
"We ignored ",
"We moved on, avoiding ","We moved on, disappointed by ","We moved on, unsure what to make of ",
"We avoided ", "We noted ").$matches[1].". ");
}
return $chapter;
}
// Return a random string, biasing towards later ones.
function randString($a,$b="",$c="",$d="",$e="",$f="",$g="",$h="",$i="",$j="",$k="")
{
$randlist = array($a,$b,$c,$d,$e,$f,$g,$h,$i,$j,$k);
$randlist = array_filter($randlist);
return $randlist[max(rand(0,count($randlist)-1),rand(0,count($randlist))-1)];
}
// Throw back a blank rejected sentence if there's any unwanted wiki markup or obviously truncated text
function cleanLine($string)
{
// $string = preg_replace("/^([^\"])+\"([^\"])+$/","$1$2",$string);
global $lastspeaker;
$string = preg_replace("/ \(\)/", "", $string); // rogue empty brackets
if (preg_match("/[#*{}\[\]\|]/",$string)) // throw out anything with leftover wiki markup
{ return "";}
if (preg_match("/ \. $/",$string)) // rogue space at end of sentence
{ return "";}
if (preg_match("/ (that|and|but|then|which|that|of)\. *$/",$string)) // sentence ending with awkward words due to ending on a comma
{ return "";}
if (preg_match("/http/",$string)) // rogue URL
{ return "";}
if (preg_match("/File:/",$string)) // stray file tags
{ return "";}
if (preg_match("/initially translated from the/",$string)) // plaintext translation notes on some articles
{ return "";}
if (!preg_match("/^((?:[^()]|\((?1)\))*+)$/",$string)) // mismatched brackets
{ return "";}
if (!preg_match("/^((?:[^\"]|\"(?1)\")*+)$/",$string)) // mismatched quotemarks
{ return "";}
if (preg_match("/^%([^*]+)%\"(.+)\" ([^\"]+)$/",$string,$matches)) // indent dialogue
{
if ($matches[1]==$lastspeaker)
{ $string = "\"".$matches[2]."\" "; }
else
{ $string = "<br> \"".$matches[2]."\" ".$matches[3]; $lastspeaker = $matches[1]; }
}
if ($lastspeaker == "Passepartout" && preg_match("/^Passepartout/",$string))
{
$string = preg_replace("/^Passepartout/","He",$string); // if Passepartout does something immediately after talking
$lastspeaker = "";
}
return $string;
}
function fetchArticlesNear($lat,$lon){
if ($lon>180) { $lon = -360+$lon; } // reverse any offset for western hemisphere
$url = "https://en.wikipedia.org/w/api.php?action=query&prop=coordinates|pageterms&format=json&coprop=type&wbptterms=description&generator=geosearch&ggscoord=".$lat."%7C".$lon."&ggsradius=10000&ggsmaxdim=1000&ggslimit=10";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 GTB5');
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
function fetchSurroundingArticles($pagename){
$pagename = urlencode($pagename);
$url = "https://en.wikipedia.org/w/api.php?action=query&prop=coordinates|pageterms&format=json&coprop=type&wbptterms=description&generator=geosearch&ggspage=".$pagename."&ggsradius=400&ggsmaxdim=1000&codistancefrompage=".$pagename."&utf8=";
$url = "https://en.wikipedia.org/w/api.php?action=query&prop=coordinates|pageterms&format=json&coprop=type&wbptterms=description&generator=geosearch&ggspage=".$pagename."&ggsradius=10000&ggsmaxdim=1000&codistancefrompage=".$pagename."&utf8=";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 GTB5');
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
function fetchArticle($pagename,$recurse = 1){
$pagename = urlencode($pagename);
$url = "https://en.wikipedia.org/w/api.php?action=query&prop=extracts&explaintext=&exchars=999999&format=json&exintro=&titles=".$pagename."&utf8=";
$url = "https://en.wikipedia.org/w/api.php?action=query&prop=revisions&format=json&rvprop=content&titles=".$pagename."&utf8=";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 GTB5');
$result = curl_exec($ch);
curl_close($ch);
$json_array = json_decode($result, true);
$extract = "There was nothing here.";
foreach($json_array['query']['pages'] as $page){
foreach($page['revisions'] as $revision){
if (isset($revision['*'])) { $extract = $revision['*']; } else { $extract = "You see nothing special."; }
}
}
// If it's a redirect, follow it (but only once)
if (preg_match("/#REDIRECT \[\[([^\]]+)\]\]/",$extract,$matches) && $recurse>0)
{
list($extract,$temparticle) = fetchArticle($matches[1],0);
}
if (strlen($extract)<5) { $extract = "You see nothing special here."; }
$original = $extract;
// Replace station templates with just the station name
$extract = preg_replace("/{{([^|])+ stations\|station=([^{}]+)}}/","$2",$extract);
// Unpack conversion templates
$extract = preg_replace("/{{convert\|([^|]+)\|([^|]+)\|[^{}]+}}/","$1 $2",$extract);
// Strip wiki template markup three times for good measure
$extract = preg_replace("/{{[^{}]+}}/","",$extract);
$extract = preg_replace("/{{[^{}]+}}/","",$extract);
$extract = preg_replace("/{{[^{}]+}}/","",$extract);
// Strip ref tags and their contents
$extract = preg_replace("/<ref>[^<]+<\/ref>/","",$extract);
$extract = cleanpunctuation($extract);
$extract = strip_tags($extract); // strip all remaining HTML
$extract = preg_replace("/'''?/","",$extract); // strip bold/italic markup
$extract = preg_replace("/=====[^=]+=====/","",$extract); // strip headings
$extract = preg_replace("/====[^=]+====/","",$extract); // strip headings
$extract = preg_replace("/===[^=]+===/","",$extract); // strip headings
$extract = preg_replace("/==[^=]+==/","",$extract); // strip headings
// Cleanup
$extract = preg_replace("/\n/"," ",$extract);
$extract = preg_replace("/ /"," ",$extract);
$extract = preg_replace("/ +/"," ",$extract);
$extract = preg_replace("/\[\[([^|\]]+)\]\]/","$1",$extract); // strip brackets from unpiped links
$extract = preg_replace("/\[\[([^|]+)\|([^]]+)\]\]/","$2",$extract); // replace piped links with second term
$extract = preg_replace("/\[([^\]]+)\]/","$1",$extract); // strip anything left in a bracket (probably just external links)
$extract = preg_replace("/\/[^ ]+\//","",$extract); // strip IPA pronunciation
// Avoid problematic abbreviations that look like they end sentences
$extract = preg_replace("/(St|Dr|Mr|Mrs|Ms|Rt Hon|pp|\(b|\(ca?|No)\./","$1.",$extract);
$extract = preg_replace("/\b([A-Za-z])\./","$1.",$extract);
$extract = preg_replace("/(\d),(\d\d\d)/","$1,$2",$extract);
return array($original,$extract);
}
function cleanpunctuation($string)
{
$fixes = array(json_decode('"\u201C"') => '"', json_decode('"\u201D"') => '"', json_decode('"\u2018"') => "'", json_decode('"\u2019"') => "'", json_decode('"\u2032"') => "'", json_decode('"\u2033"') => '"', json_decode('"\u2013"') => '-', json_decode('"\u2014"') => '--');
$new = strtr($string, $fixes);
return $new;
}
function fetchCoords($place)
{
global $crossed180;
$pagename = urlencode($place);
$url = "https://en.wikipedia.org/w/api.php?action=query&prop=coordinates&format=json&titles=".$pagename;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 GTB5');
$result = curl_exec($ch);
curl_close($ch);
$json_array = json_decode($result, true);
$pagetitle = "unknown"; $pagelat = 0; $pagelon = 0;
if (isset($json_array['query']['pages']))
{
foreach($json_array['query']['pages'] as $page){
if ($pagetitle == "unknown" && isset($page['title']) && !isset($done[$page['title']]))
{
$pagetitle = $page['title'];
if (isset($page['coordinates'][0]['lat']) && isset($page['coordinates'][0]['lon']))
{
$pagelat = $page['coordinates'][0]['lat'];
$pagelon = $page['coordinates'][0]['lon'];
}
}
}
}
if ($pagelon<0 && $crossed180==0) // western hemisphere
{
$pagelon = 360+$pagelon;
}
return array($pagelat,$pagelon);
}
?></tt></body></html>