This repository has been archived by the owner on May 31, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
create_corpus_yso_finna.py
executable file
·152 lines (130 loc) · 4.54 KB
/
create_corpus_yso_finna.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
152
#!/usr/bin/env python3
from SPARQLWrapper import SPARQLWrapper, JSON
import requests
import re
import os
import os.path
import time
import sys
FINTO_ENDPOINT='http://api.dev.finto.fi/sparql'
FINNA_API_SEARCH='https://api.finna.fi/v1/search'
lang = sys.argv[1]
# map ISO 639-1 language codes into the ISO 639-2 codes that Finna uses
LANGMAP = {
'fi': 'fin',
'sv': 'swe',
'en': 'eng'
}
def row_to_concept(row):
concept = {'uri': row['c']['value'],
'pref': row['pref']['value'],
'ysapref': row['ysapref']['value'],
'allarspref': row['allarspref']['value']}
if 'alts' in row:
concept['alts'] = row['alts']['value']
return concept
def get_concepts(lang):
sparql = SPARQLWrapper(FINTO_ENDPOINT)
sparql.setQuery("""
PREFIX skos: <http://www.w3.org/2004/02/skos/core#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX ysometa: <http://www.yso.fi/onto/yso-meta/>
SELECT ?c ?pref (GROUP_CONCAT(?alt) AS ?alts) ?ysapref ?allarspref
WHERE {
GRAPH <http://www.yso.fi/onto/yso/> {
?c a skos:Concept .
?c skos:prefLabel ?pref .
FILTER(LANG(?pref)='%s')
OPTIONAL {
?c skos:altLabel ?alt .
FILTER(LANG(?alt)='%s')
}
FILTER NOT EXISTS { ?c owl:deprecated true }
FILTER NOT EXISTS { ?c a ysometa:Hierarchy }
}
GRAPH <http://www.yso.fi/onto/ysa/> {
?ysac skos:closeMatch|skos:exactMatch ?c .
?ysac skos:prefLabel ?ysapref .
}
GRAPH <http://www.yso.fi/onto/allars/> {
?allarsc skos:closeMatch|skos:exactMatch ?c .
?allarsc skos:prefLabel ?allarspref .
}
}
GROUP BY ?c ?pref ?ysapref ?allarspref
#LIMIT 500
""" % (lang, lang))
sparql.setReturnFormat(JSON)
results = sparql.query().convert()
return [row_to_concept(row) for row in results['results']['bindings']]
concepts = get_concepts(lang)
def search_finna(params):
r = requests.get(FINNA_API_SEARCH, params=params, headers={'User-agent': 'annif 0.1'})
return r.json()
def records_to_texts(records):
texts = []
for rec in records:
if 'title' in rec:
texts.append(rec['title'])
if 'summary' in rec:
for summary in rec['summary']:
texts.append(summary)
return texts
def generate_text(concept, lang):
# start with pref- and altlabels
labels = [concept['pref']]
if lang == 'fi':
# we can use the YSA label too
labels.append(concept['ysapref'])
if lang == 'sv':
# we can use the Allars label too
labels.append(concept['allarspref'])
if 'alts' in concept:
labels.append(concept['alts'])
labels = ' '.join(labels)
# look for more text in Finna API
texts = []
fields = ['title','summary']
finnaterms = (concept['ysapref'], concept['allarspref'])
finnalang = LANGMAP[lang]
# Search type 1: exact matches using topic facet
params = {'lookfor': 'topic_facet:"%s" OR topic_facet:"%s"' % finnaterms, 'filter[]': 'language:%s' % finnalang, 'lng':lang, 'limit':100, 'field[]':fields}
response = search_finna(params)
if 'records' in response:
texts += records_to_texts(response['records'])
# Search type 2: exact matches using Subject search
params['lookfor'] = '"%s" OR "%s"' % finnaterms
params['type'] = 'Subject'
response = search_finna(params)
if 'records' in response:
texts += records_to_texts(response['records'])
# Search type 3: fuzzy matches using Subject search
params['lookfor'] = '(%s) OR (%s)' % finnaterms
response = search_finna(params)
if 'records' in response:
texts += records_to_texts(response['records'])
return "\n".join([labels] + list(set(texts)))
for concept in concepts:
localname = concept['uri'].split('/')[-1]
outfile = 'corpus/%s-%s.raw' % (localname, lang)
if os.path.exists(outfile):
continue
text = None
tries = 0
while tries < 10:
try:
text = generate_text(concept, lang)
break
except:
# failure, try again until tries exhausted
tries += 1
print("Error generating text for concept %s, trying again (attempt %d)" % (concept['uri'], tries))
time.sleep(tries) # wait progressively longer between attempts
if text is None:
print("Failed looking up concept %s, exiting" % concept['uri'])
sys.exit(1)
print(localname, lang, concept['pref'], concept['ysapref'], concept['allarspref'], len(text.split()))
f = open(outfile, 'w')
print (concept['uri'], concept['pref'], file=f)
print (text, file=f)
f.close()