CSV_to_RDF_Datini.py 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089
  1. # Utilities to read/write csv files
  2. import csv
  3. # Utilities to handle character encodings
  4. import unicodedata
  5. # Ordered Dicts
  6. from collections import OrderedDict
  7. from urllib.request import urlopen
  8. from bs4 import BeautifulSoup
  9. import json
  10. # OPZIONAL IMPORTS
  11. # For timestamping/simple speed tests
  12. from datetime import datetime
  13. # Random number generator
  14. from random import *
  15. # System & command line utilities
  16. import sys
  17. # Json for the dictionary
  18. import json
  19. import_dir = '/Users/federicaspinelli/TEAMOVI/Parser/DATA/MPP/CSV/corretti/'
  20. export_dir = '/Users/federicaspinelli/TEAMOVI/Parser/DATA/MPP/RDF/'
  21. # Custom class to store URIs + related infos for the ontologies/repositories
  22. class RDFcoords:
  23. def __init__(self, uri, prefix, code=None):
  24. self.uri = uri
  25. self.prefix = prefix
  26. self.code = code
  27. # Repositories
  28. museoCoords = RDFcoords('<https://palazzopretorio.prato.it/it/le-opere/alcuni-capolavori/>', 'mpp:')
  29. autCoords = RDFcoords('<https://palazzopretorio.prato.it/it/opere/autori/>', 'aut:')
  30. cidocCoords = RDFcoords('<http://www.cidoc-crm.org/cidoc-crm/>', 'crm:')
  31. aatCoords = RDFcoords('<http://vocab.getty.edu/aat/>', 'aat:')
  32. nsCoords = RDFcoords('<http://www.w3.org/1999/02/22-rdf-syntax-ns#>', 'rdf:')
  33. schemaCoords = RDFcoords('<http://www.w3.org/2000/01/rdf-schema#>', 'rdfs:')
  34. xsdCoords = RDFcoords('<http://www.w3.org/2001/XMLSchema#>', 'xsd:')
  35. iconCoords = RDFcoords('<http://iconclass.org/>', 'ico:')
  36. # Basic functions for triples / shortened triples in TTL format
  37. def triple(subject, predicate, object1):
  38. line = subject + ' ' + predicate + ' ' + object1
  39. return line
  40. def doublet(predicate, object1):
  41. line = ' ' + predicate + ' ' + object1
  42. return line
  43. def singlet(object1):
  44. line = ' ' + object1
  45. return line
  46. # Line endings in TTL format
  47. continueLine1 = ' ;\n'
  48. continueLine2 = ' ,\n'
  49. closeLine = ' .\n'
  50. def writeTTLHeader(output):
  51. output.write('@prefix ' + museoCoords.prefix + ' ' + museoCoords.uri + closeLine)
  52. output.write('@prefix ' + cidocCoords.prefix + ' ' + cidocCoords.uri + closeLine)
  53. output.write('@prefix ' + aatCoords.prefix + ' ' + aatCoords.uri + closeLine)
  54. output.write('@prefix ' + schemaCoords.prefix + ' ' + schemaCoords.uri + closeLine)
  55. output.write('@prefix ' + nsCoords.prefix + ' ' + nsCoords.uri + closeLine)
  56. output.write('@prefix ' + autCoords.prefix + ' ' + autCoords.uri + closeLine)
  57. output.write('@prefix ' + xsdCoords.prefix + ' ' + xsdCoords.uri + closeLine)
  58. output.write('@prefix ' + iconCoords.prefix + ' ' + iconCoords.uri + closeLine)
  59. output.write('\n')
  60. filePrefix = '00_SR20OA_'
  61. fileType = 'Datini'
  62. max_entries = 1000000000
  63. def get_aut_url(code):
  64. aut_file = open(import_dir + 'AR20AUT_' + fileType + '.csv', newline="")
  65. reader = csv.DictReader(aut_file)
  66. for row in reader:
  67. auth = int(row['AUTH'])
  68. cod = int(code)
  69. role = ''
  70. if row['AUTQ'] != '':
  71. role = row['AUTQ']
  72. else:
  73. role = ''
  74. if auth == cod:
  75. return [row['URL'], role]
  76. def get_role(role):
  77. role_file = open('/Users/federicaspinelli/TEAMOVI/Parser/DATA/MPP/CSV/AAT_RUOLI.csv', newline="")
  78. reader = csv.DictReader(role_file)
  79. for row in reader:
  80. if row['Label'] == role:
  81. return row['AAT']
  82. def get_elem(mtc):
  83. mtc_file = open('/Users/federicaspinelli/TEAMOVI/Parser/DATA/MPP/CSV/AAT_MTC.csv', newline="")
  84. reader = csv.DictReader(mtc_file)
  85. for row in reader:
  86. if row['MTC'] == mtc:
  87. return [row['AAT'], row['Type']]
  88. with open(import_dir + filePrefix + fileType + '.csv', newline="") as csv_file, open(
  89. export_dir + filePrefix + fileType + '.ttl', 'w') as output:
  90. reader = csv.DictReader(csv_file)
  91. writeTTLHeader(output)
  92. first = True
  93. ii = 0
  94. for row in reader:
  95. # The index ii is used to process a limited number of entries for testing purposes
  96. ii = ii + 1
  97. sb = ''
  98. subj = ''
  99. #pp = row['OGTD'] + ' (' + row['ACC'] + ') '
  100. if row['SGTI'] != '':
  101. sb = row['SGTI']
  102. # Triplify the 'codice' -- should exist for every entry
  103. codice = ''
  104. if (row['NCTR'] != '' and row['NCTN'] != ''):
  105. codice = row['NCTR'] + row['NCTN']
  106. codiceP = ''
  107. if (row['AUTH'] != ''):
  108. codiceP = row['AUTH']
  109. place = ''
  110. if (row['PRVC'] != ''):
  111. place = row['PRVC']
  112. columnName = list(row)
  113. url = row['URL']
  114. # placeHolders
  115. datplaceHolder = museoCoords.prefix + url
  116. e1placeHolder = museoCoords.prefix + url + '_E1'
  117. e3placeHolder = museoCoords.prefix + url + 'E3'
  118. e10placeHolder = museoCoords.prefix + url + '_E10'
  119. e12placeHolder = museoCoords.prefix + url + '_E12'
  120. e13placeHolder = museoCoords.prefix + url + '_E13'
  121. e21placeHolder = museoCoords.prefix + url + '_InE21'
  122. e25placeHolder = museoCoords.prefix + url + '_E25'
  123. e34placeHolder = museoCoords.prefix + url + '_E34'
  124. e35placeHolder1 = museoCoords.prefix + url + '_E35'
  125. e42placeHolder = museoCoords.prefix + url + '_E42'
  126. e42CplaceHolder = museoCoords.prefix + url + '_E42_MPP'
  127. e65placeHolder = museoCoords.prefix + url + '_InE65'
  128. e73placeHolder = museoCoords.prefix + url + '_E73'
  129. e74placeHolder = museoCoords.prefix + url + '_E74'
  130. if (codice != ''):
  131. line = triple(datplaceHolder, cidocCoords.prefix + 'P1_is_identified_by', e42placeHolder) + closeLine
  132. output.write(line)
  133. line = triple(e42placeHolder, nsCoords.prefix + 'type',
  134. cidocCoords.prefix + 'E42_Identifier') + closeLine
  135. output.write(line)
  136. line = triple(e42placeHolder,
  137. schemaCoords.prefix + 'label',
  138. '\"0' + codice + '\"') + closeLine
  139. output.write(line)
  140. ###
  141. line = triple(e42placeHolder,
  142. cidocCoords.prefix + 'P2_has_type',
  143. '\"Codice univoco del bene (NCT)\"') + closeLine
  144. output.write(line)
  145. # Write E22 Man Made Object & E73 Information Object -- should exist for every entry?
  146. line = triple(datplaceHolder, nsCoords.prefix + 'type',
  147. cidocCoords.prefix + 'E22_Man-Made_Object') + closeLine
  148. output.write(line)
  149. line = triple(datplaceHolder, schemaCoords.prefix + 'label', '\"' + sb + '\"') + closeLine
  150. output.write(line)
  151. line = triple(datplaceHolder, cidocCoords.prefix + 'P128_carries', e73placeHolder) + closeLine
  152. output.write(line)
  153. line = triple(e73placeHolder, nsCoords.prefix + 'type',
  154. cidocCoords.prefix + 'E73_Information_Object') + closeLine
  155. output.write(line)
  156. ss = ''
  157. if row['SGTI'] != '':
  158. ss = row['SGTI']
  159. else:
  160. ss = 'senza titolo'
  161. line = triple(e73placeHolder, schemaCoords.prefix + 'label',
  162. '\"Opera d\'arte raffigurante ' + ss + '\"') + closeLine
  163. output.write(line)
  164. # E73 - P2 - E55
  165. tt = ''
  166. typeLabel = ''
  167. if row['OGTD'] == 'dipinto':
  168. tt = aatCoords.prefix + "300033618"
  169. elif row['OGTD'] == 'rilievo':
  170. tt = aatCoords.prefix + "300047230"
  171. elif row['OGTD'] == 'polittico':
  172. tt = aatCoords.prefix + "300178235"
  173. elif row['OGTD'] == 'predella':
  174. tt = aatCoords.prefix + "300003745"
  175. line = triple(e73placeHolder,
  176. cidocCoords.prefix + 'P2_has_type',
  177. tt) + closeLine
  178. output.write(line)
  179. line = triple(tt, schemaCoords.prefix + 'label',
  180. '\"' + row['OGTD'] + '\"') + closeLine
  181. output.write(line)
  182. # E73 - P1 - E35
  183. if row['SGTT'] != '':
  184. line = triple(e73placeHolder, cidocCoords.prefix + 'P1_is_identified_by', e35placeHolder1) + closeLine
  185. output.write(line)
  186. line = triple(e35placeHolder1, nsCoords.prefix + 'type', cidocCoords.prefix + 'E35_Title') + closeLine
  187. output.write(line)
  188. line = triple(e35placeHolder1, schemaCoords.prefix + 'label', '\"' + row['SGTT'] + '\"') + closeLine
  189. output.write(line)
  190. # E22 - P62 - E1
  191. if row['SGTI'] != '':
  192. line = triple(datplaceHolder,
  193. cidocCoords.prefix + 'P62_depicts',
  194. e1placeHolder) + closeLine
  195. output.write(line)
  196. line = triple(e1placeHolder,
  197. nsCoords.prefix + 'type',
  198. cidocCoords.prefix + 'E1_CRM_Entity') + closeLine
  199. output.write(line)
  200. line = triple(e1placeHolder,
  201. schemaCoords.prefix + 'label', '\"' +
  202. row['SGTI'] + '\"') + closeLine
  203. output.write(line)
  204. line = triple(e1placeHolder,
  205. cidocCoords.prefix + 'P2_has_type',
  206. '\"Identificazione Iconografica\"') + closeLine
  207. output.write(line)
  208. # Attention: these triples are identified only for C100005 Museo di Palazzo Pretorio
  209. if row['ESC'] == 'C100005':
  210. line = triple(datplaceHolder,
  211. cidocCoords.prefix + 'P52_has_current_owner',
  212. '<https://palazzopretorio.prato.it/it/>') + closeLine
  213. output.write(line)
  214. line = triple('<https://palazzopretorio.prato.it/it/>',
  215. nsCoords.prefix + 'type',
  216. cidocCoords.prefix + 'E74_Group') + closeLine
  217. output.write(line)
  218. line = triple('<https://palazzopretorio.prato.it/it/>',
  219. schemaCoords.prefix + 'label',
  220. '\"Museo di Palazzo Pretorio\"') + closeLine
  221. output.write(line)
  222. currentLocation = ''
  223. # E22 - P54 - E53
  224. if row['LDCN'] != '':
  225. if row['LDCS'] != '':
  226. currentLocation = row['LDCS']
  227. else:
  228. currentLocation = currentLocation
  229. if row['LDCM'] != '':
  230. currentLocation = currentLocation + ', ' + row['LDCM']
  231. else:
  232. currentLocation = currentLocation
  233. if row['LDCN'] != '':
  234. currentLocation = currentLocation + ', ' + row['LDCN']
  235. else:
  236. currentLocation = currentLocation
  237. currentLocation = currentLocation + ', ' + row['PVCC'] + ' (' + row['PVCP'] + ')'
  238. line = triple(datplaceHolder, cidocCoords.prefix + 'P54_has_current_permanent_location',
  239. '\"' + currentLocation + '\"') + closeLine
  240. output.write(line)
  241. e12FplaceHolder = ''
  242. if row['DTSI'] != row['DTSF']:
  243. e12FplaceHolder = museoCoords.prefix + url + '_E12F'
  244. # Write E12 Production -- should exist for every entry?
  245. # E12 P108 E22
  246. line = triple(e12placeHolder, cidocCoords.prefix + 'P108_has_produced', datplaceHolder) + closeLine
  247. output.write(line)
  248. line = triple(e12placeHolder, nsCoords.prefix + 'type', cidocCoords.prefix + 'E12_Production') + closeLine
  249. output.write(line)
  250. # E73 P108i E12
  251. line = triple(e73placeHolder, cidocCoords.prefix + 'P108i_was_produced_by', e12placeHolder) + closeLine
  252. output.write(line)
  253. if e12FplaceHolder != '':
  254. line = triple(e12FplaceHolder, cidocCoords.prefix + 'P108_has_produced', datplaceHolder) + closeLine
  255. output.write(line)
  256. line = triple(e12FplaceHolder, nsCoords.prefix + 'type',
  257. cidocCoords.prefix + 'E12_Production') + closeLine
  258. output.write(line)
  259. line = triple(e12FplaceHolder, schemaCoords.prefix + 'label',
  260. '\"Fine produzione di ' + row['SGTI'] + '\"') + closeLine
  261. output.write(line)
  262. # E73 P108i E12
  263. line = triple(e73placeHolder, cidocCoords.prefix + 'P108i_was_produced_by', e12FplaceHolder) + closeLine
  264. output.write(line)
  265. # E12 P140i E13
  266. line = triple(e12FplaceHolder,
  267. cidocCoords.prefix + 'P140i_was_attributed_by',
  268. e13placeHolder) + closeLine
  269. output.write(line)
  270. # E12 P2
  271. line = triple(e12FplaceHolder,
  272. cidocCoords.prefix + 'P2_has_type',
  273. '\"Fine\"^^xsd:string') + closeLine
  274. output.write(line)
  275. line = triple(e12placeHolder,
  276. cidocCoords.prefix + 'P2_has_type',
  277. '\"Inizio\"^^xsd:string') + closeLine
  278. output.write(line)
  279. line = triple(e12placeHolder, schemaCoords.prefix + 'label',
  280. '\"Inizio produzione di ' + row['SGTI'] + '\"') + closeLine
  281. output.write(line)
  282. else:
  283. line = triple(e12placeHolder, schemaCoords.prefix + 'label',
  284. '\"Produzione di ' + row['SGTI'] + '\"') + closeLine
  285. output.write(line)
  286. tcl = []
  287. for name in columnName:
  288. if 'TCL' in name:
  289. tcl.append(name)
  290. # E12 - P7 - E53
  291. for el in tcl:
  292. i = 0
  293. if row[el] == 'luogo di produzione':
  294. pl = ''
  295. if i == 0:
  296. pl = row['PRVC']
  297. else:
  298. pl = row['PRVC' + i]
  299. line = triple(e12placeHolder,
  300. cidocCoords.prefix + 'P7_took_place_at',
  301. museoCoords.prefix + pl) + closeLine
  302. output.write(line)
  303. if e12FplaceHolder != '':
  304. line = triple(e12FplaceHolder,
  305. cidocCoords.prefix + 'P7_took_place_at',
  306. museoCoords.prefix + pl) + closeLine
  307. output.write(line)
  308. i = i + 1
  309. # E12 - PC14 - E21
  310. if row['AUTH'] != '':
  311. aut = get_aut_url(row['AUTH'])
  312. aut_url = aut[0]
  313. aut_role = aut[1]
  314. ll = row['AUTN'] + '_' + aut_role
  315. lab = ll.replace(' ', '')
  316. label = lab.replace(',', '')
  317. AuthorPlaceholder = autCoords.prefix + aut
  318. line = triple(museoCoords.prefix + '_' + label,
  319. cidocCoords.prefix + 'P01_has_domain',
  320. e12placeHolder) + closeLine
  321. output.write(line)
  322. if e12FplaceHolder != '':
  323. line = triple(museoCoords.prefix + '_' + label,
  324. cidocCoords.prefix + 'P01_has_domain',
  325. e12FplaceHolder) + closeLine
  326. output.write(line)
  327. if 'AUTH1' in columnName:
  328. if row['AUTH1'] != '':
  329. aut = get_aut_url(row['AUTH1'])
  330. aut_url = aut[0]
  331. aut_role = aut[1]
  332. ll = row['AUTN1'] + '_' + aut_role
  333. lab = ll.replace(' ', '')
  334. label = lab.replace(',', '')
  335. AuthorPlaceholder = autCoords.prefix + aut_url
  336. line = triple(museoCoords.prefix + '_' + label,
  337. cidocCoords.prefix + 'P01_has_domain',
  338. e12placeHolder) + closeLine
  339. output.write(line)
  340. if e12FplaceHolder != '':
  341. line = triple(museoCoords.prefix + '_' + label,
  342. cidocCoords.prefix + 'P01_has_domain',
  343. e12FplaceHolder) + closeLine
  344. output.write(line)
  345. # E12 - PC14 - E21
  346. if 'CMMN' in columnName:
  347. if row['CMMN'] != '':
  348. cc = row['CMMN']
  349. cm = cc.replace(' ', '')
  350. cmmn = cm.replace(',', '')
  351. cmmPlaceholder = museoCoords.prefix + '_' + cmmn
  352. line = triple(museoCoords.prefix + '_commit_' + cmmn,
  353. cidocCoords.prefix + 'P01_has_domain',
  354. e12placeHolder) + closeLine
  355. output.write(line)
  356. if e12FplaceHolder != '':
  357. line = triple(museoCoords.prefix + '_commit_' + cmmn,
  358. cidocCoords.prefix + 'P01_has_domain',
  359. e12FplaceHolder) + closeLine
  360. output.write(line)
  361. line = triple(museoCoords.prefix + '_commit_' + cmmn,
  362. nsCoords.prefix + 'type',
  363. cidocCoords.prefix + 'PC14_carried_out_by') + closeLine
  364. output.write(line)
  365. line = triple(museoCoords.prefix + '_commit_' + cmmn,
  366. schemaCoords.prefix + 'label',
  367. '\"' + row['CMMN'] + ' nel ruolo di committente\"') + closeLine
  368. output.write(line)
  369. line = triple(museoCoords.prefix + '_commit_' + cmmn,
  370. cidocCoords.prefix + 'P02_has_range',
  371. cmmPlaceholder) + closeLine
  372. output.write(line)
  373. line = triple(cmmPlaceholder,
  374. nsCoords.prefix + 'type',
  375. cidocCoords.prefix + 'E39_Actor') + closeLine
  376. output.write(line)
  377. line = triple(cmmPlaceholder,
  378. schemaCoords.prefix + 'label',
  379. '\"' + row['CMMN'] + '\"') + closeLine
  380. output.write(line)
  381. line = triple(museoCoords.prefix + '_commit_' + cmmn,
  382. cidocCoords.prefix + 'P14.1_in_the_role_of',
  383. museoCoords.prefix + '_client') + closeLine
  384. output.write(line)
  385. line = triple(museoCoords.prefix + '_client',
  386. nsCoords.prefix + 'type',
  387. cidocCoords.prefix + 'E55_Type') + closeLine
  388. output.write(line)
  389. line = triple(museoCoords.prefix + '_client',
  390. schemaCoords.prefix + 'label',
  391. '\"Committente\"') + closeLine
  392. output.write(line)
  393. # E12 - P4 - E52
  394. if row['DTSI'] != '':
  395. line = triple(e12placeHolder,
  396. cidocCoords.prefix + 'P4_has_time-span',
  397. museoCoords.prefix + row['DTSI']) + closeLine
  398. output.write(line)
  399. line = triple(museoCoords.prefix + row['DTSI'],
  400. nsCoords.prefix + 'type',
  401. cidocCoords.prefix + 'E52_Time-Span') + closeLine
  402. output.write(line)
  403. line = triple(museoCoords.prefix + row['DTSI'],
  404. schemaCoords.prefix + 'label',
  405. '\"' + row['DTSI'] + '\"') + closeLine
  406. output.write(line)
  407. if e12FplaceHolder != '':
  408. line = triple(e12FplaceHolder,
  409. cidocCoords.prefix + 'P4_has_time-span',
  410. museoCoords.prefix + row['DTSF']) + closeLine
  411. output.write(line)
  412. line = triple(museoCoords.prefix + row['DTSF'],
  413. nsCoords.prefix + 'type',
  414. cidocCoords.prefix + 'E52_Time-Span') + closeLine
  415. output.write(line)
  416. line = triple(museoCoords.prefix + row['DTSF'],
  417. schemaCoords.prefix + 'label',
  418. '\"' + row['DTSF'] + '\"') + closeLine
  419. output.write(line)
  420. tcl = []
  421. for name in columnName:
  422. if 'TCL' in name:
  423. tcl.append(name)
  424. j = 0
  425. for el in tcl:
  426. if row[el] != '':
  427. j = j + 1
  428. last = str(j - 1)
  429. n = len(tcl) - 1
  430. for i in range(n):
  431. k = str(i + 1)
  432. if i + 1 == 1:
  433. w = ''
  434. else:
  435. w = i
  436. f = str(w)
  437. if row['TCL' + k] != '':
  438. pastActor = ''
  439. newActor = ''
  440. pl = ''
  441. if row['PRCD' + k] != '':
  442. newActor = ' a ' + row['PRCD' + k]
  443. if row['PRCD' + f] != '':
  444. pastActor = ' da ' + row['PRCD' + f]
  445. pl = row['PRCD' + f].replace(' ', '')
  446. newe10placeHolder = museoCoords.prefix + url + "_E10_" + k
  447. line = triple(newe10placeHolder,
  448. cidocCoords.prefix + 'P30_transferred_custody_of',
  449. datplaceHolder) + closeLine
  450. output.write(line)
  451. line = triple(newe10placeHolder,
  452. nsCoords.prefix + 'type',
  453. cidocCoords.prefix + 'E10_Transfer_of_Custody') + closeLine
  454. output.write(line)
  455. line = triple(newe10placeHolder,
  456. schemaCoords.prefix + 'label',
  457. '\"Passaggio di ' + row['SGTI'] + pastActor +
  458. newActor + '\"') + closeLine
  459. output.write(line)
  460. if row['PRDI' + f] != '':
  461. timespan = row['PRDI' + f]
  462. tt = timespan.replace(' ', '')
  463. tp = tt.replace('.', '')
  464. ts = tp.replace('/', '')
  465. timespanPlaceholder = museoCoords.prefix + '_' + ts
  466. # E10 P4 E52
  467. line = triple(newe10placeHolder,
  468. cidocCoords.prefix + 'P4_has_time-span',
  469. timespanPlaceholder) + closeLine
  470. output.write(line)
  471. line = triple(timespanPlaceholder,
  472. nsCoords.prefix + 'type',
  473. cidocCoords.prefix + 'E52_Time-Span') + closeLine
  474. output.write(line)
  475. line = triple(timespanPlaceholder,
  476. schemaCoords.prefix + 'label',
  477. '\"' + timespan + '\"') + closeLine
  478. output.write(line)
  479. pastActorPlaceholder = museoCoords.prefix + '_' + pl
  480. newLoc = row['PRCD' + k].replace(' ', '')
  481. newActorPlaceholder = museoCoords.prefix + '_' + newLoc
  482. # E10 P26 E74 (moved to)
  483. if newActorPlaceholder != '':
  484. line = triple(newe10placeHolder,
  485. cidocCoords.prefix + 'P29_custody_received_by',
  486. newActorPlaceholder) + closeLine
  487. output.write(line)
  488. # E10 P27 E74
  489. pastActorLabel = row['PRCD' + f]
  490. line = triple(newe10placeHolder,
  491. cidocCoords.prefix + 'P28_custody_surrendered_by',
  492. pastActorPlaceholder) + closeLine
  493. output.write(line)
  494. line = triple(pastActorPlaceholder,
  495. nsCoords.prefix + 'type',
  496. cidocCoords.prefix + 'E39_Actor') + closeLine
  497. output.write(line)
  498. line = triple(pastActorPlaceholder,
  499. schemaCoords.prefix + 'label',
  500. '\"' + pastActorLabel + '\"') + closeLine
  501. output.write(line)
  502. line = triple(datplaceHolder,
  503. cidocCoords.prefix + 'P49_has_former_or_current_keeper',
  504. pastActorPlaceholder) + closeLine
  505. output.write(line)
  506. # E74 P74 E53
  507. pastResidenceLabel = row['PRVC' + f]
  508. pastResidencePlaceHolder = museoCoords.prefix + row['PRVC' + f]
  509. line = triple(pastActorPlaceholder,
  510. cidocCoords.prefix + 'P74_has_current_or_former_residence',
  511. pastResidencePlaceHolder) + closeLine
  512. output.write(line)
  513. ####
  514. pastActor = ''
  515. newActor = ''
  516. pl = ''
  517. if row['LDCN'] != '':
  518. newActor = ' a ' + row['LDCN']
  519. if row['PRCD' + last] != '':
  520. pastActor = ' da ' + row['PRCD' + last]
  521. pl = row['PRCD' + last].replace(' ', '')
  522. line = triple(e10placeHolder,
  523. cidocCoords.prefix + 'P30_transferred_custody_of',
  524. datplaceHolder) + closeLine
  525. output.write(line)
  526. line = triple(e10placeHolder,
  527. nsCoords.prefix + 'type',
  528. cidocCoords.prefix + 'E10_Transfer_of_Custody') + closeLine
  529. output.write(line)
  530. line = triple(e10placeHolder,
  531. schemaCoords.prefix + 'label',
  532. '\"Passaggio di ' + row['SGTI'] + pastActor +
  533. newActor + '\"') + closeLine
  534. output.write(line)
  535. if row['PRDU' + last] != '':
  536. timespan = row['PRDU' + last]
  537. tt = timespan.replace(' ', '')
  538. ts = tt.replace('/', '')
  539. timespanPlaceholder = museoCoords.prefix + '_' + ts
  540. # E10 P4 E52
  541. line = triple(e10placeHolder,
  542. cidocCoords.prefix + 'P4_has_time-span',
  543. timespanPlaceholder) + closeLine
  544. output.write(line)
  545. line = triple(timespanPlaceholder,
  546. nsCoords.prefix + 'type',
  547. cidocCoords.prefix + 'E52_Time-Span') + closeLine
  548. output.write(line)
  549. line = triple(timespanPlaceholder,
  550. schemaCoords.prefix + 'label',
  551. '\"' + timespan + '\"') + closeLine
  552. output.write(line)
  553. pastActorPlaceholder = museoCoords.prefix + '_' + pl
  554. newLocPlaceholder = e74placeHolder
  555. # E10 P26 E74 (moved to)
  556. if newLocPlaceholder != '':
  557. line = triple(e10placeHolder,
  558. cidocCoords.prefix + 'P29_custody_received_by',
  559. newLocPlaceholder) + closeLine
  560. output.write(line)
  561. # E10 P27 E74
  562. pastActorLabel = row['PRCD' + last]
  563. line = triple(e10placeHolder,
  564. cidocCoords.prefix + 'P28_custody_surrendered_by',
  565. pastActorPlaceholder) + closeLine
  566. output.write(line)
  567. line = triple(pastActorPlaceholder,
  568. nsCoords.prefix + 'type',
  569. cidocCoords.prefix + 'E39_Actor') + closeLine
  570. output.write(line)
  571. line = triple(pastActorPlaceholder,
  572. schemaCoords.prefix + 'label',
  573. '\"' + pastActorLabel + '\"') + closeLine
  574. output.write(line)
  575. line = triple(datplaceHolder,
  576. cidocCoords.prefix + 'P49_has_former_or_current_keeper',
  577. pastActorPlaceholder) + closeLine
  578. output.write(line)
  579. # E74 P74 E53
  580. pastResidenceLabel = row['PRVC' + last]
  581. pastResidencePlaceHolder = museoCoords.prefix + row['PRVC' + last]
  582. if row['PRVP' + last] != '':
  583. pastResidenceLabel = pastResidenceLabel + ' (' + row['PRVP' + last] + ')'
  584. if row['PRVR' + last] != '':
  585. pastResidenceLabel = pastResidenceLabel + ', ' + row['PRVR' + last]
  586. if row['PRVS' + last] != '':
  587. pastResidenceLabel = pastResidenceLabel + ', ' + row['PRVS' + last]
  588. line = triple(pastActorPlaceholder,
  589. cidocCoords.prefix + 'P74_has_current_or_former_residence',
  590. pastResidencePlaceHolder) + closeLine
  591. output.write(line)
  592. line = triple(pastResidencePlaceHolder,
  593. nsCoords.prefix + 'type',
  594. cidocCoords.prefix + 'E53_Place') + closeLine
  595. output.write(line)
  596. # E22 P44 E3
  597. if row['STCC'] != '':
  598. line = triple(datplaceHolder,
  599. cidocCoords.prefix + 'P44_has_condition',
  600. e3placeHolder) + closeLine
  601. output.write(line)
  602. line = triple(e3placeHolder,
  603. nsCoords.prefix + 'type',
  604. cidocCoords.prefix + 'E3_Condition_State') + closeLine
  605. output.write(line)
  606. line = triple(e3placeHolder,
  607. schemaCoords.prefix + 'label',
  608. '\"Condizione di: ' + row['SGTI'] + '\"') + closeLine
  609. output.write(line)
  610. line = triple(e3placeHolder,
  611. cidocCoords.prefix + 'P2_has_type',
  612. '\"' + row['STCC'] + '\"') + closeLine
  613. output.write(line)
  614. # E22 P65 E34
  615. if (row['ISRI'] != ''):
  616. line = triple(datplaceHolder,
  617. cidocCoords.prefix + 'P56_bears_feature',
  618. e25placeHolder) + closeLine
  619. output.write(line)
  620. line = triple(e25placeHolder,
  621. nsCoords.prefix + 'type',
  622. cidocCoords.prefix + 'E25_Man-Made_Feature') + closeLine
  623. output.write(line)
  624. line = triple(e25placeHolder,
  625. schemaCoords.prefix + 'label',
  626. '\"Iscrizione su ' + subj + '\"') + closeLine
  627. output.write(line)
  628. line = triple(e25placeHolder,
  629. cidocCoords.prefix + 'P128_carries',
  630. e34placeHolder) + closeLine
  631. output.write(line)
  632. line = triple(e34placeHolder,
  633. nsCoords.prefix + 'type',
  634. cidocCoords.prefix + 'E34_Inscription') + closeLine
  635. output.write(line)
  636. line = triple(e34placeHolder,
  637. schemaCoords.prefix + 'label',
  638. '\"Iscrizione: ' + row['ISRI'] + '\"') + closeLine
  639. output.write(line)
  640. pl = row['ISRI'].replace(' ', '-')
  641. pla = pl.replace('.', '')
  642. line = triple(e34placeHolder,
  643. cidocCoords.prefix + 'P3_has_note',
  644. '\"' + row['ISRI'] + '\"') + closeLine
  645. output.write(line)
  646. # E34 P2 E55
  647. if (row['ISRT'] != ''):
  648. rr = row['ISRT'].replace(' ', '')
  649. line = triple(e34placeHolder,
  650. cidocCoords.prefix + 'P2_has_type',
  651. '\"' + row['ISRT'] + '\"') + closeLine
  652. output.write(line)
  653. # E34 P72 E56
  654. if (row['ISRL'] != ''):
  655. line = triple(e34placeHolder,
  656. cidocCoords.prefix + 'P72_has_language',
  657. museoCoords.prefix + '_' + row['ISRL']) + closeLine
  658. output.write(line)
  659. line = triple(museoCoords.prefix + '_' + row['ISRL'],
  660. nsCoords.prefix + 'type',
  661. cidocCoords.prefix + 'E56_Language') + closeLine
  662. output.write(line)
  663. line = triple(museoCoords.prefix + '_' + row['ISRL'],
  664. schemaCoords.prefix + 'label',
  665. '\"' + row['ISRL'] + '\"') + closeLine
  666. output.write(line)
  667. if row['ISRS'] != '':
  668. line = triple(e34placeHolder,
  669. cidocCoords.prefix + 'P92i_was_brought_into_existence_by',
  670. e65placeHolder) + closeLine
  671. output.write(line)
  672. line = triple(e65placeHolder,
  673. nsCoords.prefix + 'type',
  674. cidocCoords.prefix + 'E65_Creation') + closeLine
  675. output.write(line)
  676. line = triple(e65placeHolder,
  677. schemaCoords.prefix + 'label',
  678. '\"Creazione dell\'Iscrizione ' + row['ISRI'] + '\"') + closeLine
  679. output.write(line)
  680. if row['ISRS']:
  681. ss = row['ISRS'].replace(' ', '')
  682. tecPlaceholder = museoCoords.prefix + url + '_' + ss
  683. line = triple(e65placeHolder,
  684. cidocCoords.prefix + 'P32_used_general_technique',
  685. tecPlaceholder) + closeLine
  686. output.write(line)
  687. line = triple(tecPlaceholder,
  688. nsCoords.prefix + 'type',
  689. cidocCoords.prefix + 'E55_Type') + closeLine
  690. output.write(line)
  691. line = triple(tecPlaceholder,
  692. schemaCoords.prefix + 'label',
  693. '\"' + row['ISRS'] + '\"') + closeLine
  694. output.write(line)
  695. if row['ISRP'] != '':
  696. line = triple(e25placeHolder,
  697. cidocCoords.prefix + 'P3_has_note',
  698. '\"' + row['ISRP'] + '\"^^xsd:string') + closeLine
  699. output.write(line)
  700. unit = ''
  701. if (row['MISU'] != ''):
  702. unit = row['MISU']
  703. valueA = ''
  704. valueL = ''
  705. if (row['MISA'] != ''):
  706. value = row['MISA']
  707. valueA = value.replace(',', 'v')
  708. if (row['MISL'] != ''):
  709. value = row['MISL']
  710. valueL = value.replace(',', 'v')
  711. # Altezza
  712. # E22 P43 E54
  713. if (row['MISA'] != ''):
  714. line = triple(datplaceHolder,
  715. cidocCoords.prefix + 'P43_has_dimension',
  716. museoCoords.prefix + url + '_Altezza') + closeLine
  717. output.write(line)
  718. line = triple(museoCoords.prefix + url + '_Altezza',
  719. nsCoords.prefix + 'type',
  720. cidocCoords.prefix + 'E54_Dimension') + closeLine
  721. output.write(line)
  722. line = triple(museoCoords.prefix + url + '_Altezza',
  723. schemaCoords.prefix + 'label',
  724. '\"Altezza: ' + row['MISA'] + row['MISU'] + '\"') + closeLine
  725. output.write(line)
  726. # E54 P90 E60
  727. line = triple(museoCoords.prefix + url + '_Altezza',
  728. cidocCoords.prefix + 'P90_has_value',
  729. '\"' + row['MISA'] + '\"^^xsd:integer') + closeLine
  730. output.write(line)
  731. # E54 P2 E55
  732. line = triple(museoCoords.prefix + url + '_Altezza',
  733. cidocCoords.prefix + 'P2_has_type',
  734. aatCoords.prefix + '300055644') + closeLine
  735. output.write(line)
  736. line = triple(aatCoords.prefix + '300055644',
  737. schemaCoords.prefix + 'label',
  738. '\"altezza\"') + closeLine
  739. output.write(line)
  740. # E54 P91 E58
  741. if (row['MISU'] != ''):
  742. line = triple(museoCoords.prefix + url + '_Altezza',
  743. cidocCoords.prefix + 'P91_has_unit',
  744. aatCoords.prefix + '300379098') + closeLine
  745. output.write(line)
  746. line = triple(aatCoords.prefix + '300379098',
  747. nsCoords.prefix + 'type',
  748. cidocCoords.prefix + 'E58_Measurement_Unit') + closeLine
  749. output.write(line)
  750. line = triple(aatCoords.prefix + '300379098',
  751. schemaCoords.prefix + 'label',
  752. '\"' + row['MISU'] + '\"') + closeLine
  753. output.write(line)
  754. # Larghezza
  755. # E22 P43 E54
  756. if (row['MISL'] != ''):
  757. line = triple(datplaceHolder,
  758. cidocCoords.prefix + 'P43_has_dimension',
  759. museoCoords.prefix + url + '_Larghezza') + closeLine
  760. output.write(line)
  761. line = triple(museoCoords.prefix + url + '_Larghezza',
  762. nsCoords.prefix + 'type',
  763. cidocCoords.prefix + 'E54_Dimension') + closeLine
  764. output.write(line)
  765. line = triple(museoCoords.prefix + url + '_Larghezza',
  766. schemaCoords.prefix + 'label',
  767. '\"Larghezza: ' + row['MISL'] + row['MISU'] + '\"') + closeLine
  768. output.write(line)
  769. # E54 P90 E60
  770. line = triple(museoCoords.prefix + url + '_Larghezza',
  771. cidocCoords.prefix + 'P90_has_value',
  772. '\"' + row['MISL'] + '\"^^xsd:integer') + closeLine
  773. output.write(line)
  774. # E54 P2 E55
  775. line = triple(museoCoords.prefix + url + '_Larghezza',
  776. cidocCoords.prefix + 'P2_has_type',
  777. aatCoords.prefix + '300055647') + closeLine
  778. output.write(line)
  779. line = triple(aatCoords.prefix + '300055647',
  780. schemaCoords.prefix + 'label',
  781. '\"larghezza\"') + closeLine
  782. output.write(line)
  783. # E54 P91 E58
  784. if (row['MISU'] != ''):
  785. line = triple(museoCoords.prefix + url + '_Larghezza',
  786. cidocCoords.prefix + 'P91_has_unit',
  787. aatCoords.prefix + '300379098') + closeLine
  788. output.write(line)
  789. line = triple(aatCoords.prefix + '300379098',
  790. nsCoords.prefix + 'type',
  791. cidocCoords.prefix + 'E58_Measurement_Unit') + closeLine
  792. output.write(line)
  793. line = triple(aatCoords.prefix + '300379098',
  794. schemaCoords.prefix + 'label',
  795. '\"' + row['MISU'] + '\"') + closeLine
  796. output.write(line)
  797. if row['MTC'] != '':
  798. mtcs = []
  799. if '/' in row['MTC']:
  800. mtcs = row['MTC'].split('/')
  801. else:
  802. mtcs.append(row['MTC'])
  803. for tc in mtcs:
  804. mtc = tc.lstrip()
  805. el = get_elem(mtc)
  806. if el[1] == 'MTC/M':
  807. line = triple(datplaceHolder,
  808. cidocCoords.prefix + 'P45_consists_of',
  809. aatCoords.prefix + el[0]) + closeLine
  810. output.write(line)
  811. line = triple(aatCoords.prefix + el[0],
  812. nsCoords.prefix + 'type',
  813. cidocCoords.prefix + 'E57_Material') + closeLine
  814. output.write(line)
  815. line = triple(aatCoords.prefix + el[0],
  816. schemaCoords.prefix + 'label',
  817. '\"' + mtc + '\"') + closeLine
  818. output.write(line)
  819. else: #E12 Production - P32 used technique - E55 Type
  820. line = triple(e12placeHolder,
  821. cidocCoords.prefix + 'P32_used_general_technique',
  822. aatCoords.prefix + el[0]) + closeLine
  823. output.write(line)
  824. if e12FplaceHolder != '':
  825. line = triple(e12FplaceHolder,
  826. cidocCoords.prefix + 'P32_used_general_technique',
  827. aatCoords.prefix + el[0]) + closeLine
  828. output.write(line)
  829. line = triple(aatCoords.prefix + el[0],
  830. nsCoords.prefix + 'type',
  831. cidocCoords.prefix + 'E55_Type') + closeLine
  832. output.write(line)
  833. line = triple(aatCoords.prefix + el[0],
  834. schemaCoords.prefix + 'label',
  835. '\"' + mtc + '\"') + closeLine
  836. output.write(line)
  837. # E12 P140i E13
  838. if row['AUTM'] != '':
  839. mot = row['AUTM'].replace(' ', '_')
  840. e55placeHolder = museoCoords.prefix + url + '_' + mot
  841. line = triple(e12placeHolder,
  842. cidocCoords.prefix + 'P140i_was_attributed_by',
  843. e13placeHolder) + closeLine
  844. output.write(line)
  845. line = triple(e13placeHolder,
  846. nsCoords.prefix + 'type',
  847. cidocCoords.prefix + 'E13_Attribute_Assignment') + closeLine
  848. output.write(line)
  849. line = triple(e13placeHolder,
  850. schemaCoords.prefix + 'label',
  851. '\"Motivazione attribuzione\"') + closeLine
  852. output.write(line)
  853. line = triple(e13placeHolder,
  854. cidocCoords.prefix + 'P2_has_type',
  855. '\"' + row['AUTM'] + '\"') + closeLine
  856. output.write(line)
  857. aut = get_aut_url(row['AUTH'])
  858. aut_url = aut[0]
  859. AuthorPlaceholder = autCoords.prefix + aut_url
  860. line = triple(e13placeHolder,
  861. cidocCoords.prefix + 'P141_assigned',
  862. AuthorPlaceholder) + closeLine
  863. output.write(line)
  864. # E22 P44 E62
  865. if row['NSC'] != '':
  866. ph = row['NSC'].replace(' "', ' «')
  867. phr = ph.replace('"', '»')
  868. line = triple(datplaceHolder,
  869. cidocCoords.prefix + 'P3_has_note',
  870. '\"' + phr + '\"^^xsd:string') + closeLine
  871. output.write(line)
  872. iconclass = row['DESI']
  873. icon = iconclass.replace(' ', '')
  874. list_icon = []
  875. if ':' in icon:
  876. list_icon = icon.split(':')
  877. else:
  878. list_icon.append(icon)
  879. for ic in list_icon:
  880. url = 'http://iconclass.org/rdk/' + str(ic)
  881. html = urlopen(url).read()
  882. soup = BeautifulSoup(html, 'html.parser')
  883. # kill all script and style elements
  884. for script in soup(["script", "style"]):
  885. script.extract() # rip it out
  886. # get text
  887. text = soup.get_text()
  888. pretty = soup.prettify()
  889. ff = soup.find("div", {"id": "ic_current"})
  890. dd = ff.find("a", {"class", "ic_notation"})
  891. ss = dd.text
  892. x = ss.find(' ')
  893. icon_label = ss[x + 1:]
  894. ur = ic.replace("(", "%28")
  895. urr = ur.replace(")", "%29")
  896. line = triple(datplaceHolder,
  897. cidocCoords.prefix + 'P62_depicts',
  898. iconCoords.prefix + urr) + closeLine
  899. output.write(line)
  900. line = triple(iconCoords.prefix + urr,
  901. nsCoords.prefix + 'type',
  902. cidocCoords.prefix + 'E1_CRM_Entity') + closeLine
  903. output.write(line)
  904. # P2 Opera d'arte
  905. line = triple(datplaceHolder,
  906. cidocCoords.prefix + 'P2_has_type',
  907. '\"Opera d\'Arte\"') + closeLine
  908. output.write(line)
  909. output.write('\n')
  910. #
  911. #
  912. # Limit number of entries processed (if desired)
  913. if (ii > max_entries):
  914. break