annotate pyspark/n3Parser.py @ 0:e34cf1b6fe09 tip

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