Package: rdfdf
rdfdfrdfdf - Functionality for rule-basedpandas.DataFrame-rdflib.Graphconversion.For representation of tabular data in RDF see Allemang, Hendler: Semantic Web for the Working Ontologist. 2011, 40ff.This project is in an early stage of development and should be used with caution.Requirementspython >= 3.10UsageFor now rdfdf provides aDFGraphConverterclass for rule-basedpandas.DataFrametordflib.Graph conversion.Template-based conversion functionality might be available in the future.DFGraphConverterUnlikerdfpandaswhich requires URIRefs as column headers (and otherwise just creates invalid RDF with e.g. literals as predicates), rdfdfcomputesURIRefs (or Literals for triple objects) based on rules.DFGraphConverteriterates over a dataframe and constructs RDF triples by constructing a generator of subgraphs ('field graphs') and then merging all subgraphs with anrdflib.Graphcomponent.Subgraphs are generated byfor every rowfor every rule incolumn_ruleslooking up thecolumn_ruleskey for the current row and calling the correspondingcolumn_rulesvalue.column_rulesvalues must be callables which are responsible for generating and returning a graph for merging. Note that rules actually don't need to return an instance ofrdflib.Graph(e.g. if a rule just accessesDFgraphConverter.store; for an example of state sharing between rules seebelow), in which case the result is skipped in the generator.column_rulesvalues must be callables of arity 3;
for every field for which a rule appliesthe subject field value (specified in thesubject_columnparameter and possibly computed bysubject_ruleofDFGraphConverter),the object field value (i.e. the current field value) andDFGraphConverter.store(a class level attribute for state sharing between rulesandDFGraphConverterinstances)get passed to the respective rule callable (see examples below).Parameters:dataframe: A pandas.DataFrame to be converted.subject_column: Selects a table column by name to be regarded as the column of triple subjects.subject_rule: Optional; either aCallable[[str], URIRef]or anrdflib.Namespacewhich gets applied to every field of the subject_column;
if supplied,subject_fieldin thecolumn_ruleswill be whatsubject_rulecomputes it to be; otherwisesubject_fieldwill be just the raw field value of the currentsubject_columnand must be handled manually in order to be become a valid triple subject (i.e. a URIRef).column_rules: A mapping of column names to callables responsible for creating subgraphs ('field graphs').graph: Optional; allows to set the internal rdflib.Graph component.Examples:Simple exampleimportpandasaspdfromrdfdf.rdfdfimportDFGraphConverterfromrdflibimportURIRef,Graph,Namespace,Literalfromrdflib.namespaceimportRDF# namespace definitionsCRM=Namespace("http://www.cidoc-crm.org/cidoc-crm/")# bind namespace to graph componentnsgraph=Graph()nsgraph.bind("crm",CRM)# create a simple dataframetable=[{"id":"rem","full_title":"Reference corpus Middle High German"}]df=pd.DataFrame(data=table)# rulesdeffull_title_rule(subject_field,object_field,store):title_uri=URIRef(f"https://{subject_field}.clscor.io/entity/corpus/title/full")corpus_uri=URIRef(f"https://{subject_field}.clscor.io/entity/corpus")triples=[(title_uri,RDF.type,CRM.E41_Appellation),(title_uri,CRM.P1_identifies,corpus_uri),# inverse(corpus_uri,CRM.P1_is_identified_by,title_uri),(title_uri,CRM.P2_has_type,URIRef("https://core.clscor.io/entity/type/title/full")),(title_uri,CRM['190_has_symbolic_content'],Literal(object_field)),]graph=Graph()fortripleintriples:graph.add(triple)returngraphcolumn_rules={"full_title":full_title_rule}dfgraph=DFGraphConverter(dataframe=df,subject_column="id",column_rules=column_rules,graph=nsgraph)graph=dfgraph.to_graph()print(graph.serialize(format="ttl"))Output:@prefixcrm:<http://www.cidoc-crm.org/cidoc-crm/>.<https://rem.clscor.io/entity/corpus>crm:P1_is_identified_by<https://rem.clscor.io/entity/corpus/title/full>.<https://rem.clscor.io/entity/corpus/title/full>acrm:E41_Appellation;crm:190_has_symbolic_content"Reference corpus Middle High German";crm:P1_identifies<https://rem.clscor.io/entity/corpus>;crm:P2_has_type<https://core.clscor.io/entity/type/title/full>.More involved exampleFor a more involved application ofrdfdf(including extensive state sharing between rules) see theCorTabscript.ContributionPlease feel free to open issues or pull requests.