forked from jodaiber/model-quickstarter
-
Notifications
You must be signed in to change notification settings - Fork 25
/
index_db.sh
264 lines (211 loc) · 8.61 KB
/
index_db.sh
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
#!/bin/bash
#+------------------------------------------------------------------------------------------------------------------------------+
#| DBpedia Spotlight - Create database-backed model |
#| @author Joachim Daiber |
#+------------------------------------------------------------------------------------------------------------------------------+
# $1 Working directory
# $2 Locale (en_US)
# $3 Stopwords file
# $4 Analyzer+Stemmer language prefix e.g. Dutch
# $5 Model target folder
export MAVEN_OPTS="-Xmx26G"
usage ()
{
echo "index_db.sh"
echo "usage: ./index_db.sh -o /data/spotlight/nl/opennlp wdir nl_NL /data/spotlight/nl/stopwords.nl.list Dutch /data/spotlight/nl/final_model"
echo "Create a database-backed model of DBpedia Spotlight for a specified language."
echo " "
}
opennlp="None"
eval="false"
blacklist="false"
while getopts "eo:b:" opt; do
case $opt in
o) opennlp="$OPTARG";;
e) eval="true";;
b) blacklist="$OPTARG";;
esac
done
shift $((OPTIND - 1))
if [ $# != 5 ]
then
usage
exit
fi
BASE_DIR=$(pwd)
function get_path {
if [[ "$1" = /* ]]
then
echo "$1"
else
echo "$BASE_DIR/$1"
fi
}
BASE_WDIR=$(get_path $1)
TARGET_DIR=$(get_path $5)
STOPWORDS=$(get_path $3)
WDIR="$BASE_WDIR/$2"
if [[ "$opennlp" != "None" ]]; then
opennlp=$(get_path $opennlp)
fi
if [[ "$blacklist" != "false" ]]; then
blacklist=$(get_path $blacklist)
fi
LANGUAGE=`echo $2 | sed "s/_.*//g"`
echo "Language: $LANGUAGE"
echo "Working directory: $WDIR"
mkdir -p $WDIR
########################################################################################################
# Preparing the data.
########################################################################################################
echo "Loading Wikipedia dump..."
if [ -z "$WIKI_MIRROR" ]; then
WIKI_MIRROR="https://dumps.wikimedia.org/"
fi
WP_DOWNLOAD_FILE=$WDIR/dump.xml
echo Checking for wikipedia dump at $WP_DOWNLOAD_FILE
if [ -f "$WP_DOWNLOAD_FILE" ]; then
echo File exists.
else
echo Downloading wikipedia dump.
if [ "$eval" == "false" ]; then
curl -# "$WIKI_MIRROR/${LANGUAGE}wiki/latest/${LANGUAGE}wiki-latest-pages-articles.xml.bz2" | bzcat > $WDIR/dump.xml
else
curl -# "$WIKI_MIRROR/${LANGUAGE}wiki/latest/${LANGUAGE}wiki-latest-pages-articles.xml.bz2" | bzcat | python $BASE_DIR/scripts/split_train_test.py 1200 $WDIR/heldout.txt > $WDIR/dump.xml
fi
fi
cd $WDIR
cp $STOPWORDS stopwords.$LANGUAGE.list
if [ -e "$opennlp/$LANGUAGE-token.bin" ]; then
cp "$opennlp/$LANGUAGE-token.bin" "$LANGUAGE.tokenizer_model" || echo "tokenizer already exists"
else
touch "$LANGUAGE.tokenizer_model"
fi
########################################################################################################
# DBpedia extraction:
########################################################################################################
###### # ####### # ###### # # #####
# # # # # # # # # # # # #
# # # # # # # # # # # #
# # # # # # # ###### # # #####
# # ####### # ####### # # # # #
# # # # # # # # # # # # #
###### # # # # # ###### ##### #####
echo " Downloading the latest version of the following artifacts:
* https://databus.dbpedia.org/dbpedia/generic/disambiguations
* https://databus.dbpedia.org/dbpedia/generic/redirects
* https://databus.dbpedia.org/dbpedia/mappings/instance-types
Note of deviation from original index_db.sh:
takes the direct AND transitive version of redirects and instance-types and the redirected version of disambiguation
"
cd $BASE_WDIR
QUERY="PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX dc: <http://purl.org/dc/elements/1.1/>
PREFIX dataid: <http://dataid.dbpedia.org/ns/core#>
PREFIX dataid-cv: <http://dataid.dbpedia.org/ns/cv#>
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX dcat: <http://www.w3.org/ns/dcat#>
SELECT ?file WHERE {
{
# Subselect latestVersion by artifact
SELECT ?artifact (max(?version) as ?latestVersion) WHERE {
?dataset dataid:artifact ?artifact .
?dataset dct:hasVersion ?version
FILTER (?artifact in (
# GENERIC
<https://databus.dbpedia.org/dbpedia/generic/disambiguations> ,
<https://databus.dbpedia.org/dbpedia/generic/redirects> ,
# MAPPINGS
<https://databus.dbpedia.org/dbpedia/mappings/instance-types>
# latest ontology, currently @denis account
# TODO not sure if needed for Spotlight
# <https://databus.dbpedia.org/denis/ontology/dbo-snapshots>
)) .
}GROUP BY ?artifact
}
?dataset dct:hasVersion ?latestVersion .
{
?dataset dataid:artifact ?artifact .
?dataset dcat:distribution ?distribution .
?distribution dcat:downloadURL ?file .
?distribution dataid:contentVariant '$LANGUAGE'^^xsd:string .
# remove debug info
MINUS {
?distribution dataid:contentVariant ?variants .
FILTER (?variants in ('disjointDomain'^^xsd:string, 'disjointRange'^^xsd:string))
}
}
} ORDER by ?artifact
"
# execute query and trim " and first line from result set
RESULT=`curl --data-urlencode query="$QUERY" --data-urlencode format="text/tab-separated-values" https://databus.dbpedia.org/repo/sparql | sed 's/"//g' | grep -v "^file$" `
# Download
TMPDOWN="dump-tmp-download"
mkdir $TMPDOWN
cd $TMPDOWN
for i in $RESULT
do
wget $i
ls
echo $TMPDOWN
pwd
done
cd ..
echo "decompressing"
bzcat -v $TMPDOWN/instance-types*.ttl.bz2 > $WDIR/instance_types.nt
bzcat -v $TMPDOWN/disambiguations*.ttl.bz2 > $WDIR/disambiguations.nt
bzcat -v $TMPDOWN/redirects*.ttl.bz2 > $WDIR/redirects.nt
# clean
rm -r $TMPDOWN
########################################################################################################
# Setting up Spotlight:
########################################################################################################
cd $BASE_WDIR
if [ -d dbpedia-spotlight ]; then
echo "Updating DBpedia Spotlight..."
cd dbpedia-spotlight
git reset --hard HEAD
git pull
mvn -T 1C -q -Dhttps.protocols=TLSv1.2 clean install
else
echo "Setting up DBpedia Spotlight..."
git clone --depth 1 https://github.com/dbpedia-spotlight/dbpedia-spotlight-model
mv dbpedia-spotlight-model dbpedia-spotlight
cd dbpedia-spotlight
mvn -T 1C -q -Dhttps.protocols=TLSv1.2 install
fi
########################################################################################################
# Extracting wiki stats:
########################################################################################################
cd $BASE_WDIR
rm -Rf wikistatsextractor
git clone --depth 1 https://github.com/dbpedia-spotlight/wikistatsextractor
# Stop processing if one step fails
set -e
#Copy results to local:
cd $BASE_WDIR/wikistatsextractor
mvn install exec:java -Dexec.args="--output_folder $WDIR $LANGUAGE $2 $4Stemmer $WDIR/dump.xml $WDIR/stopwords.$LANGUAGE.list"
if [ "$blacklist" != "false" ]; then
echo "Removing blacklist URLs..."
mv $WDIR/uriCounts $WDIR/uriCounts_all
grep -v -f $blacklist $WDIR/uriCounts_all > $WDIR/uriCounts
fi
echo "Finished wikistats extraction. Cleaning up..."
rm -f $WDIR/dump.xml
########################################################################################################
# Building Spotlight model:
########################################################################################################
#Create the model:
echo "Creating the model"
cd $BASE_WDIR/dbpedia-spotlight
mvn -pl index exec:java -Dexec.cleanupDaemonThreads=false -Dexec.mainClass=org.dbpedia.spotlight.db.CreateSpotlightModel -Dexec.args="$2 $WDIR $TARGET_DIR $opennlp $STOPWORDS $4Stemmer"
if [ "$eval" == "true" ]; then
mvn -pl eval exec:java -Dexec.mainClass=org.dbpedia.spotlight.evaluation.EvaluateSpotlightModel -Dexec.args="$TARGET_DIR $WDIR/heldout.txt" > $TARGET_DIR/evaluation.txt
fi
curl https://raw.githubusercontent.com/dbpedia-spotlight/model-quickstarter/master/model_readme.txt > $TARGET_DIR/README.txt
curl "$WIKI_MIRROR/${LANGUAGE}wiki/latest/${LANGUAGE}wiki-latest-pages-articles.xml.bz2-rss.xml" | grep link | sed -e 's/^.*<link>//' -e 's/<[/]link>.*$//' | uniq >> $TARGET_DIR/README.txt
echo "Collecting data..."
cd $BASE_DIR
mkdir -p data/$LANGUAGE && mv $WDIR/*Counts data/$LANGUAGE
gzip $WDIR/*.nt &
set +e