annotate dml-cla/python/n3Parser.py @ 0:718306e29690 tip

commiting public release
author Daniel Wolff
date Tue, 09 Feb 2016 21:05:06 +0100
parents
children
rev   line source
Daniel@0 1 # Part of DML (Digital Music Laboratory)
Daniel@0 2 # Copyright 2014-2015 AUTHOR_AFFILIATION
Daniel@0 3
Daniel@0 4 # This program is free software; you can redistribute it and/or
Daniel@0 5 # modify it under the terms of the GNU General Public License
Daniel@0 6 # as published by the Free Software Foundation; either version 2
Daniel@0 7 # of the License, or (at your option) any later version.
Daniel@0 8 #
Daniel@0 9 # This program is distributed in the hope that it will be useful,
Daniel@0 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
Daniel@0 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
Daniel@0 12 # GNU General Public License for more details.
Daniel@0 13 #
Daniel@0 14 # You should have received a copy of the GNU General Public
Daniel@0 15 # License along with this library; if not, write to the Free Software
Daniel@0 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Daniel@0 17
Daniel@0 18 from rdflib import Graph
Daniel@0 19 from rdflib.plugins.parsers.notation3 import BadSyntax
Daniel@0 20 import warnings
Daniel@0 21 import codecs
Daniel@0 22 import platform
Daniel@0 23
Daniel@0 24 # Load and parse an n3 file
Daniel@0 25 def get_rdf_graph_from_n3(n3_file_uri):
Daniel@0 26
Daniel@0 27 graph = Graph()
Daniel@0 28
Daniel@0 29 try:
Daniel@0 30 graph.parse(n3_file_uri, format="n3")
Daniel@0 31 except UnicodeDecodeError:
Daniel@0 32
Daniel@0 33 n3_file_str = uri2path(n3_file_uri)
Daniel@0 34 n3_file_iso = codecs.open(n3_file_str, 'r', "iso-8859-1")
Daniel@0 35
Daniel@0 36 # check if n3 is valid and parse
Daniel@0 37 # repair if necessary
Daniel@0 38 graph = parse_potentially_corrupt_n3(n3_file_iso.read())
Daniel@0 39
Daniel@0 40 except (AssertionError, BadSyntax):
Daniel@0 41
Daniel@0 42 n3_file_str = uri2path(n3_file_uri)
Daniel@0 43 n3_file = open(n3_file_str, 'r')
Daniel@0 44 graph = parse_potentially_corrupt_n3(n3_file.read())
Daniel@0 45
Daniel@0 46 return graph
Daniel@0 47
Daniel@0 48 # can parse truncated n3
Daniel@0 49 def parse_potentially_corrupt_n3(content):
Daniel@0 50 feature_graph = Graph()
Daniel@0 51 # test if file is complete.
Daniel@0 52 # if not, delete the last corrupted entry
Daniel@0 53 if not '.' in content[-4:]:
Daniel@0 54 warnings.warn("Incomplete rdf file, ignoring last entry")
Daniel@0 55 # we find the last correct event
Daniel@0 56 lastentry = content.rfind(':event')
Daniel@0 57 feature_graph.parse(data=content[:lastentry], format="n3")
Daniel@0 58 else:
Daniel@0 59 feature_graph.parse(data=content, format="n3")
Daniel@0 60
Daniel@0 61 return feature_graph
Daniel@0 62
Daniel@0 63 # returns filepath from url
Daniel@0 64 def uri2path(n3_file_uri):
Daniel@0 65
Daniel@0 66 n3_file_uri_str = n3_file_uri.__str__()
Daniel@0 67
Daniel@0 68 # Assume that n3_file_uri_str starts with 'file://' - we need to remove that
Daniel@0 69 if 'Win' in platform.system():
Daniel@0 70 FILE_URI_START_INDEX = 8
Daniel@0 71 else:
Daniel@0 72 FILE_URI_START_INDEX = 7
Daniel@0 73
Daniel@0 74 n3_file_str = n3_file_uri_str[FILE_URI_START_INDEX:len(n3_file_uri_str)]
Daniel@0 75 return n3_file_str