from semantics import statements # namespaces skos = 'http://www.w3.org/2008/05/skos#' owl = 'http://www.w3.org/2002/07/owl#' rdf = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#' rdfs = 'http://www.w3.org/2000/01/rdf-schema#' # text params delimiter = '\t' linebreak = '\n' comment = '#' def abbreviate(node): """ Attempt to abbreviate URIs using namespace prefixes. """ abbreviation = str(node) abbreviation = abbreviation.replace(skos, 'skos:') abbreviation = abbreviation.replace(owl, 'owl:') abbreviation = abbreviation.replace(rdf, 'rdf:') abbreviation = abbreviation.replace(rdfs, 'rdfs:') return abbreviation def render(triple): """ Render a triple as a string. """ line = abbreviate(triple[0]) + delimiter + abbreviate(triple[1]) + delimiter + abbreviate(triple[2]) + linebreak return line # open file for text output outfile = open('triples.txt', 'w') # write out triples to file count = 1 for statement in statements(): outfile.write(comment+'S'+str(count)+linebreak) # comment out this line to remove statement numbers from output for triple in statement: outfile.write(render(triple)) count = count + 1