forked from ppquadrat/DigThatLick
-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_styles.py
76 lines (55 loc) · 1.93 KB
/
add_styles.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
# Python3
""" Import styles from csv
Polina Proutskova, October 2019
"""
###########################################################
# Data paths
if PROPERTY_PREFIX == "je":
RDFfile = "TTL/JE_solos.ttl"
RDFnewfile = "TTL/JE_styles.ttl"
else:
RDFfile = "TTL/ILL_solos.ttl"
RDFnewfile = "TTL/ILL_styles.ttl"
STYLESfile = "DATA/styles.csv"
###########################################################
# general import
import csv
import os
import re
import json
import dtlutil
# logging
import logging
MIN_LEVEL = logging.DEBUG
dtlutil.setup_log(MIN_LEVEL)
##############################################################
# read in rdf graph
import rdflib
from rdflib.graph import Graph, Store, URIRef, Literal, BNode
from rdflib.namespace import Namespace, RDFS
from rdflib import plugin
from rdflib.plugins import sparql
from rdflib import Namespace
from rdflib.namespace import RDF, FOAF, RDFS, DC, XSD
MO, TL, EVENT, OLO, DTL, initNs = dtlutil.init_namespaces()
g = dtlutil.create_graph()
dtlutil.read_in_rdf(g, RDFfile)
logging.debug("\ngraph has %i triples", len(g))
##############################################################
logging.info("\nReading styles from %s", STYLESfile)
with open(STYLESfile, 'r') as csvfile:
csvreader = csv.reader(csvfile, delimiter=',')
count = 0
for row in csvreader:
if len(row) > 0:
fprint = row[0]
style = row[1]
signalURI = g.value(subject=None, predicate=DTL.fingerprint_short, \
object=Literal(fprint), default=None, any=True)
if signalURI != None:
g.add( (signalURI, DTL.style, Literal(style)) )
track = g.value(signalURI, MO.published_as)
title = g.value(track, DC.title)
logging.debug("style %s added to the signal of track %s", style, title)
logging.debug("\ngraph has %i triples", len(g))
dtlutil.write_rdf(g, RDFnewfile)