-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwikidata-lexemes-to-solr-synonyms.py
151 lines (103 loc) · 4.94 KB
/
wikidata-lexemes-to-solr-synonyms.py
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
#!/usr/bin/python3
# -*- coding: utf-8 -*-
import requests
import json
from SPARQLWrapper import SPARQLWrapper, XML, JSON
#
# Generate Solr synonyms graph filter resource config from lexemes (f.e. OpenData from Wikidata)
#
class lexemes2solr(object):
dictionary = {}
def __init__(self):
self.verbose = True
self.solr = "http://localhost:8983/solr/"
self.solr_core = "core1"
self.solr_managed_synonyms_resource = "lexemes"
self.sparql_endpoint = 'https://query.wikidata.org/sparql'
self.language = 'en'
# Todo: If got not listed language code as command line option instead of language entity URI, ask Wikidata by SELECT ?s WHERE { ?s wdt:P218 'languagecode'. }
self.langugages = { 'en': 'http://www.wikidata.org/entity/Q1860',
'de': 'http://www.wikidata.org/entity/Q188',
'es': 'http://www.wikidata.org/entity/Q1321',
'pt': 'http://www.wikidata.org/entity/Q5146',
'hu': 'http://www.wikidata.org/entity/Q9067',
}
#
# safe the dictionary of synonyms by Solr REST API for managed resources
#
def synonyms2solr(self):
url = self.solr
if not url.endswith('/'):
url += '/'
url += self.solr_core + '/schema/analysis/synonyms/' + self.solr_managed_synonyms_resource
headers = {'content-type' : 'application/json'}
r = requests.post(url=url, data=json.dumps(self.dictionary), headers=headers)
#
# get lemmas and lexicalform entries for given language from Wikidata, convert to dictionary and safe to Solr
#
def process(self):
# get wikidata entity URI for language from preconfigured mappings, if got language code instead full wikidata URI as language selection
language_entity = self.language
if language_entity in self.langugages:
language_entity = self.langugages[language_entity]
# build SPARQL query
query = """
SELECT DISTINCT ?lemma ?representation WHERE
{
?LexicalEntry rdf:type <http://www.w3.org/ns/lemon/ontolex#LexicalEntry>.
?LexicalEntry <http://purl.org/dc/terms/language> <""" + language_entity + """>.
?LexicalEntry wikibase:lemma ?lemma.
?LexicalEntry <http://www.w3.org/ns/lemon/ontolex#lexicalForm> ?LexicalFormEntry.
?LexicalFormEntry <http://www.w3.org/ns/lemon/ontolex#representation> ?representation.
}
"""
# read query results from SPARQL endpoint
sparql = SPARQLWrapper(self.sparql_endpoint)
sparql.setQuery(query)
sparql.setReturnFormat(JSON)
results = sparql.query().convert()
count_lemmas = 0
count_representations = 0
# Read lemmas and represations from SPARQL result to dictionary {lemma: [representations]}
for result in results["results"]["bindings"]:
lemma = result["lemma"]["value"]
representation = result["representation"]["value"]
# skip, if lemma same as representation, so no need for synonym config for this entry
if lemma != representation:
# create dictionary entry for lemma
if not lemma in self.dictionary:
# add lemma itself as synonym, so Solr Synonyms Graph resolution includes original concept/lemma, too, not only rewritten to synonym(s)
self.dictionary[lemma] = [lemma]
count_lemmas += 1
self.dictionary[lemma].append(representation)
count_representations += 1
# subconnect multiple lexical forms: connect the lexical forms as synonym for each other, too, not only to the lemma
extended_dictionary = self.dictionary.copy()
for lemma in self.dictionary:
for representation in self.dictionary[lemma]:
if not representation in extended_dictionary:
extended_dictionary[representation] = [representation]
for otherrepresentation in self.dictionary[lemma]:
if not otherrepresentation in extended_dictionary[representation]:
extended_dictionary[representation].append(otherrepresentation)
self.dictionary = extended_dictionary
# write the synonyms (collected in dictionary) to Solr at once (better performance, since the json config has only to be rewritten once)
self.synonyms2solr()
# output stats
if self.verbose:
print ("Imported {} lemmas and their {} (different) representations".format(count_lemmas, count_representations))
# start by command line
if __name__ == "__main__":
from optparse import OptionParser
parser = OptionParser("wikidata-lexemes-to-solr-synonyms [options]")
parser.add_option("-s", "--solr", dest="solr", default='http://localhost:8983/solr/', help="Solr URI like http://localhost:8983/solr/")
parser.add_option("-c", "--core", dest="core", default='core1', help="Solr core/index name")
parser.add_option("-r", "--resource", dest="resource", default='lexemes', help="Solr managed synonyms resource where to store the results")
parser.add_option("-l", "--language", dest="language", default='en', help="Language (Wikidata entity)")
(options, args) = parser.parse_args()
converter = lexemes2solr()
converter.language = options.language
converter.solr = options.solr
converter.solr_core = options.core
converter.solr_managed_synonyms_resource = options.resource
converter.process()