daskol

filter-wiki-dump.pl

Nov 27th, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 2.04 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. # Program to filter Wikipedia XML dumps to "clean" text consisting only of lowercase
  4. # letters (a-z, converted from A-Z), and spaces (never consecutive).
  5. # All other characters are converted to spaces.  Only text which normally appears
  6. # in the web browser is displayed.  Tables are removed.  Image captions are
  7. # preserved.  Links are converted to normal text.  Digits are spelled out.
  8.  
  9. # Written by Matt Mahoney, June 10, 2006.  This program is released to the public domain.
  10.  
  11. $/=">";                     # input record separator
  12. while (<>) {
  13.   if (/<text /) {$text=1;}  # remove all but between <text> ... </text>
  14.   if (/#redirect/i) {$text=0;}  # remove #REDIRECT
  15.   if ($text) {
  16.  
  17.     # Remove any text not normally visible
  18.     if (/<\/text>/) {$text=0;}
  19.     s/<.*>//;               # remove xml tags
  20.     s/&amp;/&/g;            # decode URL encoded chars
  21.     s/&lt;/</g;
  22.     s/&gt;/>/g;
  23.     s/<ref[^<]*<\/ref>//g;  # remove references <ref...> ... </ref>
  24.     s/<[^>]*>//g;           # remove xhtml tags
  25.     s/\[http:[^] ]*/[/g;    # remove normal url, preserve visible text
  26.     s/\|thumb//ig;          # remove images links, preserve caption
  27.     s/\|left//ig;
  28.     s/\|right//ig;
  29.     s/\|\d+px//ig;
  30.     s/\[\[image:[^\[\]]*\|//ig;
  31.     s/\[\[category:([^|\]]*)[^]]*\]\]/[[$1]]/ig;  # show categories without markup
  32.     s/\[\[[a-z\-]*:[^\]]*\]\]//g;  # remove links to other languages
  33.     s/\[\[[^\|\]]*\|/[[/g;  # remove wiki url, preserve visible text
  34.     s/{\{[^}]*}}//g;         # remove {{icons}} and {tables}
  35.     s/{[^}]*}//g;
  36.     s/\[//g;                # remove [ and ]
  37.     s/\]//g;
  38.     s/&[^;]*;/ /g;          # remove URL encoded chars
  39.  
  40.     # convert to lowercase letters and spaces, spell digits
  41.     $_=" $_ ";
  42.     tr/A-Z/a-z/;
  43.     s/0/ zero /g;
  44.     s/1/ one /g;
  45.     s/2/ two /g;
  46.     s/3/ three /g;
  47.     s/4/ four /g;
  48.     s/5/ five /g;
  49.     s/6/ six /g;
  50.     s/7/ seven /g;
  51.     s/8/ eight /g;
  52.     s/9/ nine /g;
  53.     tr/a-z\.\n/ /cs;
  54.     tr/\n/./;
  55.     s/(\.+\s*)+/\. /g;
  56.     chop;
  57.     print $_;
  58.   }
  59. }
Add Comment
Please, Sign In to add comment