]> git.openstreetmap.org Git - nominatim.git/blob - data-sources/wikipedia-wikidata/import_wikipedia.sh
wikipedia: create all language pagelinkcount tables before querying them
[nominatim.git] / data-sources / wikipedia-wikidata / import_wikipedia.sh
1 #!/bin/bash
2
3 psqlcmd() {
4      psql wikiprocessingdb
5 }
6
7 mysql2pgsqlcmd() {
8      ./mysql2pgsql.perl /dev/stdin /dev/stdout
9 }
10
11
12 # list the languages to process (refer to List of Wikipedias here: https://en.wikipedia.org/wiki/List_of_Wikipedias)
13
14 language=( "ar" "bg" "ca" "cs" "da" "de" "en" "es" "eo" "eu" "fa" "fr" "ko" "hi" "hr" "id" "it" "he" "lt" "hu" "ms" "nl" "ja" "no" "pl" "pt" "kk" "ro" "ru" "sk" "sl" "sr" "fi" "sv" "tr" "uk" "vi" "vo" "war" "zh" )
15
16
17 # create wikipedia calculation tables
18
19 echo "CREATE TABLE linkcounts (language text, title text, count integer, sumcount integer, lat double precision, lon double precision);"  | psqlcmd
20 echo "CREATE TABLE wikipedia_article (language text NOT NULL, title text NOT NULL, langcount integer, othercount integer, totalcount integer, lat double precision, lon double precision, importance double precision, title_en text, osm_type character(1), osm_id bigint );" | psqlcmd
21 echo "CREATE TABLE wikipedia_redirect (language text, from_title text, to_title text );"  | psqlcmd
22
23
24 # download individual wikipedia language tables
25
26 for i in "${language[@]}"
27 do
28     wget https://dumps.wikimedia.org/${i}wiki/latest/${i}wiki-latest-page.sql.gz
29     wget https://dumps.wikimedia.org/${i}wiki/latest/${i}wiki-latest-pagelinks.sql.gz
30     wget https://dumps.wikimedia.org/${i}wiki/latest/${i}wiki-latest-langlinks.sql.gz
31     wget https://dumps.wikimedia.org/${i}wiki/latest/${i}wiki-latest-redirect.sql.gz
32 done
33
34
35 # import individual wikipedia language tables
36
37 for i in "${language[@]}"
38 do
39     gzip -dc ${i}wiki-latest-pagelinks.sql.gz | sed "s/\`pagelinks\`/\`${i}pagelinks\`/g" | mysql2pgsqlcmd | psqlcmd
40     gzip -dc ${i}wiki-latest-page.sql.gz | sed "s/\`page\`/\`${i}page\`/g" | mysql2pgsqlcmd | psqlcmd
41     gzip -dc ${i}wiki-latest-langlinks.sql.gz | sed "s/\`langlinks\`/\`${i}langlinks\`/g" | mysql2pgsqlcmd | psqlcmd
42     gzip -dc ${i}wiki-latest-redirect.sql.gz | sed "s/\`redirect\`/\`${i}redirect\`/g" | mysql2pgsqlcmd | psqlcmd
43 done
44
45
46 # process language tables and associated pagelink counts
47
48 for i in "${language[@]}"
49 do
50     echo "create table ${i}pagelinkcount as select pl_title as title,count(*) as count from ${i}pagelinks where pl_namespace = 0 group by pl_title;" | psqlcmd
51     echo "insert into linkcounts select '${i}',pl_title,count(*) from ${i}pagelinks where pl_namespace = 0 group by pl_title;" | psqlcmd
52     echo "insert into wikipedia_redirect select '${i}',page_title,rd_title from ${i}redirect join ${i}page on (rd_from = page_id) where page_namespace = 0 and rd_namespace = 0;" | psqlcmd
53     echo "alter table ${i}pagelinkcount add column othercount integer;" | psqlcmd
54     echo "update ${i}pagelinkcount set othercount = 0;" | psqlcmd
55 done
56
57 for i in "${language[@]}"
58 do
59     for j in "${language[@]}"
60     do
61         echo "update ${i}pagelinkcount set othercount = ${i}pagelinkcount.othercount + x.count from (select page_title as title,count from ${i}langlinks join ${i}page on (ll_from = page_id) join ${j}pagelinkcount on (ll_lang = '${j}' and ll_title = title)) as x where x.title = ${i}pagelinkcount.title;" | psqlcmd
62     done
63     echo "insert into wikipedia_article select '${i}', title, count, othercount, count+othercount from ${i}pagelinkcount;" | psqlcmd
64 done
65
66
67 # calculate importance score for each wikipedia page
68
69 echo "update wikipedia_article set importance = log(totalcount)/log((select max(totalcount) from wikipedia_article))" | psqlcmd
70
71
72 # clean up intermediate tables to conserve space
73
74 for i in "${language[@]}"
75 do
76     echo "DROP TABLE ${i}pagelinks;" | psqlcmd
77     echo "DROP TABLE ${i}page;" | psqlcmd
78     echo "DROP TABLE ${i}langlinks;" | psqlcmd
79     echo "DROP TABLE ${i}redirect;" | psqlcmd
80     echo "DROP TABLE ${i}pagelinkcount;" | psqlcmd
81 done