get-wikimedia.sh 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. #!/usr/bin/env bash
  2. #
  3. # Copyright (c) 2016-present, Facebook, Inc.
  4. # All rights reserved.
  5. #
  6. # This source code is licensed under the BSD-style license found in the
  7. # LICENSE file in the root directory of this source tree. An additional grant
  8. # of patent rights can be found in the PATENTS file in the same directory.
  9. #
  10. set -e
  11. normalize_text() {
  12. sed -e "s/’/'/g" -e "s/′/'/g" -e "s/''/ /g" -e "s/'/ ' /g" -e "s/“/\"/g" -e "s/”/\"/g" \
  13. -e 's/"/ " /g' -e 's/\./ \. /g' -e 's/<br \/>/ /g' -e 's/, / , /g' -e 's/(/ ( /g' -e 's/)/ ) /g' -e 's/\!/ \! /g' \
  14. -e 's/\?/ \? /g' -e 's/\;/ /g' -e 's/\:/ /g' -e 's/-/ - /g' -e 's/=/ /g' -e 's/=/ /g' -e 's/*/ /g' -e 's/|/ /g' \
  15. -e 's/«/ /g' | tr 0-9 " "
  16. }
  17. export LANGUAGE=en_US.UTF-8
  18. export LC_ALL=en_US.UTF-8
  19. export LANG=en_US.UTF-8
  20. NOW=$(date +"%Y%m%d")
  21. ROOT="data/wikimedia/${NOW}"
  22. mkdir -p "${ROOT}"
  23. echo "Saving data in ""$ROOT"
  24. read -r -p "Choose a language (e.g. en, bh, fr, etc.): " choice
  25. LANG="$choice"
  26. echo "Chosen language: ""$LANG"
  27. read -r -p "Continue to download (WARNING: This might be big and can take a long time!)(y/n)? " choice
  28. case "$choice" in
  29. y|Y ) echo "Starting download...";;
  30. n|N ) echo "Exiting";exit 1;;
  31. * ) echo "Invalid answer";exit 1;;
  32. esac
  33. wget -c "https://dumps.wikimedia.org/""$LANG""wiki/latest/""${LANG}""wiki-latest-pages-articles.xml.bz2" -P "${ROOT}"
  34. echo "Processing ""$ROOT"/"$LANG""wiki-latest-pages-articles.xml.bz2"
  35. bzip2 -c -d "$ROOT"/"$LANG""wiki-latest-pages-articles.xml.bz2" | awk '{print tolower($0);}' | perl -e '
  36. # Program to filter Wikipedia XML dumps to "clean" text consisting only of lowercase
  37. # letters (a-z, converted from A-Z), and spaces (never consecutive)...
  38. # All other characters are converted to spaces. Only text which normally appears.
  39. # in the web browser is displayed. Tables are removed. Image captions are.
  40. # preserved. Links are converted to normal text. Digits are spelled out.
  41. # *** Modified to not spell digits or throw away non-ASCII characters ***
  42. # Written by Matt Mahoney, June 10, 2006. This program is released to the public domain.
  43. $/=">"; # input record separator
  44. while (<>) {
  45. if (/<text /) {$text=1;} # remove all but between <text> ... </text>
  46. if (/#redirect/i) {$text=0;} # remove #REDIRECT
  47. if ($text) {
  48. # Remove any text not normally visible
  49. if (/<\/text>/) {$text=0;}
  50. s/<.*>//; # remove xml tags
  51. s/&amp;/&/g; # decode URL encoded chars
  52. s/&lt;/</g;
  53. s/&gt;/>/g;
  54. s/<ref[^<]*<\/ref>//g; # remove references <ref...> ... </ref>
  55. s/<[^>]*>//g; # remove xhtml tags
  56. s/\[http:[^] ]*/[/g; # remove normal url, preserve visible text
  57. s/\|thumb//ig; # remove images links, preserve caption
  58. s/\|left//ig;
  59. s/\|right//ig;
  60. s/\|\d+px//ig;
  61. s/\[\[image:[^\[\]]*\|//ig;
  62. s/\[\[category:([^|\]]*)[^]]*\]\]/[[$1]]/ig; # show categories without markup
  63. s/\[\[[a-z\-]*:[^\]]*\]\]//g; # remove links to other languages
  64. s/\[\[[^\|\]]*\|/[[/g; # remove wiki url, preserve visible text
  65. s/{{[^}]*}}//g; # remove {{icons}} and {tables}
  66. s/{[^}]*}//g;
  67. s/\[//g; # remove [ and ]
  68. s/\]//g;
  69. s/&[^;]*;/ /g; # remove URL encoded chars
  70. $_=" $_ ";
  71. chop;
  72. print $_;
  73. }
  74. }
  75. ' | normalize_text | awk '{if (NF>1) print;}' | tr -s " " | shuf > "${ROOT}"/wiki."${LANG}".txt