<jupyter_start><jupyter_text>1. Extract object properties<jupyter_code>def run_query(query):
    sparql = SPARQLWrapper("http://dbpedia.org/sparql")
    sparql.setQuery(query)
    sparql.setReturnFormat(JSON)
    results = sparql.query().convert()
    results_df = pd.io.json.json_normalize(results["results"]["bindings"])
    return results_df
#Extract object properties
query = """
    SELECT DISTINCT ?prop  ?label
    WHERE {
     ?prop a owl:ObjectProperty .
     ?prop rdfs:label ?label .
     FILTER(langmatches(lang(?label), 'en'))
     FILTER NOT EXISTS {?prop rdfs:domain owl:Thing}
    }
"""
results_df = run_query(query)
object_properties = results_df[["prop.value", "label.value"]]
object_properties.columns = ["DBpediaObjProp", "DBPediaPropLabel"]
object_properties = object_properties[object_properties["DBpediaObjProp"].str.contains("http")]
object_properties.head()
object_properties_count = len(object_properties)
print("Queried {} object properties from DBpedia.".format(object_properties_count))
object_properties_file = "../Data/interim/DBpedia/properties/DBpedia_object_properties.csv"
object_properties.to_csv(object_properties_file, index = False)<jupyter_output><empty_output><jupyter_text>2. DL-Learner  2.1. Functions<jupyter_code>def _run_dllearner(results_file, object_property):
    proc = Popen(["C:/_Andreea/HiwiJob/RDF2Vec/dllearner-1.3.0/bin/enrichment.bat", 
                       "-e", "http://dbpedia.org/sparql", 
                       "-g", "http://dbpedia.org",
                       "-r", object_property,
                       "-o", results_file,
                       "-f", "n-triples",
                       "-t", "0"],
                    stderr = PIPE, stdout = PIPE)
    out, err = proc.communicate()
def _get_symmetry_transitivity(file):
    symmetric_statement = "Symmetric: <"
    transitive_statement = "Transitive: <"
    symmetry_score = None
    transitivity_score = None
    with open(file, encoding="utf-8", mode="rt") as f:
        for line in f:
            if symmetric_statement in line:
                next_line = next(f)
                symmetry_score = float(next_line.split("^^")[0].split('"')[-2])
            if transitive_statement in line:
                next_line = next(f)
                transitivity_score = float(next_line.split("^^")[0].split('"')[-2])
    return symmetry_score, transitivity_score
def _get_inverses(file):
    inverses_dict = dict()
    inverse_statement = "InverseOf"
    with open(file, encoding="utf-8", mode="rt") as f:
        for line in f:
            if inverse_statement in line:
                inverse = line.split("InverseOf <")[-1].split(">")[0]
                next_line = next(f)
                inverse_score = float(next_line.split("^^")[0].split('"')[-2])
                inverses_dict[inverse] = inverse_score
    return inverses_dict
def _get_subproperties(file):
    subproperties_dict = dict()
    subproperty_statement = "SubPropertyOf"
    with open(file, encoding="utf-8", mode="rt") as f:
        for line in f:
            if subproperty_statement in line:
                subproperty = line.split("SubPropertyOf <")[-1].split(">")[0]
                next_line = next(f)
                subproperty_score = float(next_line.split("^^")[0].split('"')[-2])
                subproperties_dict[subproperty] = subproperty_score
    return subproperties_dict<jupyter_output><empty_output><jupyter_text>2.2. Run DL-Learner<jupyter_code>symmetries = defaultdict()
transitivities = defaultdict()
inverses = defaultdict()
tmp_folder = "../Data/interim/DBpedia/properties/DLLearner/"
if not os.path.isdir(tmp_folder):
    os.mkdir(tmp_folder)
with tqdm(desc="Running DLLearner for object properties: ", total = object_properties_count, unit = "properties") as pbar:
    for object_property in list(object_properties["DBpediaObjProp"]):
        object_property_name = object_property.split("/")[-1]
        results_file = tmp_folder + object_property_name + ".nt"

        # Run the DL-Learner
        _run_dllearner(results_file, object_property)

        if os.path.isfile(results_file):
            # Get symmetry and transitivity scores
            symmetry_score, transitivity_score = _get_symmetry_transitivity(results_file)
            if symmetry_score is not None:
                symmetries[object_property] = symmetry_score
            if transitivity_score is not None:
                transitivities[object_property] = transitivity_score

            # Get inverses and corresponding 
            inverses_dict = _get_inverses(results_file)
            if inverses_dict:
                inverses_df = pd.DataFrame(list(inverses_dict.items()), columns=["InverseProperty", "InverseScore"])
                inverses[object_property] = inverses_df       

            # Delete temporary results file
            os.remove(results_file)

        pbar.update(1)<jupyter_output>Running DLLearner for object properties: 100%|█| 1105/1105 [89:50:47<00:00, 12.73s/properties]<jupyter_text>2.3. Save results<jupyter_code>symmetries_df = pd.DataFrame(list(symmetries.items()), columns=["Property", "SymmetryScore"])
symmetries_df.sort_values(ascending=False, by=["SymmetryScore"], inplace=True)
symmetries_df.head()
symmetries_file = "../Data/interim/DBpedia/properties/DLLearner/all_symmetries.csv"
symmetries_df.to_csv(symmetries_file, index = False)
transitivities_df = pd.DataFrame(list(transitivities.items()), columns=["Property", "TransitivityScore"])
transitivities_df.sort_values(ascending=False, by=["TransitivityScore"], inplace=True)
transitivities_df.head()
transitivities_file = "../Data/interim/DBpedia/properties/DLLearner/all_transitivities.csv"
transitivities_df.to_csv(transitivities_file, index = False)
for key in inverses.keys():
    inverses[key].sort_values(ascending=False, by=["InverseScore"], inplace=True)
inverses_file = "../Data/interim/DBpedia/properties/DLLearner/all_inverses.pkl"
with open(inverses_file, "wb") as f:
    pickle.dump(inverses, f)<jupyter_output><empty_output><jupyter_text>3. Process results<jupyter_code>symmetric_properties_file = "../Data/interim/DBpedia/properties/DBpedia_dllearner_symmetric_properties.csv"
transitive_properties_file = "../Data/interim/DBpedia/properties/DBpedia_dllearner_transitive_properties.csv"
inverse_properties_file = "../Data/interim/DBpedia/properties/DBpedia_dllearner_inverse_properties.csv"<jupyter_output><empty_output><jupyter_text>3.1. Symmetric properties<jupyter_code>symmetric_properties = list(symmetries_df[symmetries_df["SymmetryScore"]>0.53]["Property"])
symmetric_properties.extend(["http://dbpedia.org/ontology/spouse",
                             "http://dbpedia.org/ontology/sisterStation",
                             "http://dbpedia.org/ontology/sisterCollege",
                             "http://dbpedia.org/ontology/sisterNewspaper"
                            ])
symmetric_properties_df = pd.DataFrame(symmetric_properties, columns = ["SymmetricProperty"])
symmetric_properties_df
symmetric_properties_df.to_csv(symmetric_properties_file, index=False)<jupyter_output><empty_output><jupyter_text>3.2. Transitive properties<jupyter_code>transitive_properties = list(transitivities_df[transitivities_df["TransitivityScore"]>0.45]["Property"])
transitive_properties_df = pd.DataFrame(transitive_properties, columns = ["TransitiveProperty"])
transitive_properties_df
transitive_properties_df.to_csv(transitive_properties_file, index=False)<jupyter_output><empty_output><jupyter_text>3.3. Inverse properties<jupyter_code>inverses_list = list()
for key in inverses.keys():
    inverses_list.append((key, inverses[key]["InverseProperty"].iloc[0], inverses[key]["InverseScore"].iloc[0]))
inverses_temp_df = pd.DataFrame(inverses_list, columns = ["Property", "InverseProperty", "InverseScore"])
inverses_temp_df.sort_values(ascending=False, by=["InverseScore"], inplace=True)
inverses_temp_df
inverse_properties = list()
inverse_properties.extend([
    ("http://dbpedia.org/ontology/successor", "http://dbpedia.org/ontology/predecessor"),
    ("http://dbpedia.org/ontology/rightChild", "http://dbpedia.org/ontology/leftChild"),
    ("http://dbpedia.org/ontology/parentCompany", "http://dbpedia.org/ontology/subsidiary"),
    ("http://dbpedia.org/ontology/childOrganisation", "http://dbpedia.org/ontology/parentOrganisation"),
    ("http://dbpedia.org/ontology/influenced", "http://dbpedia.org/ontology/influencedBy"),
    ("http://dbpedia.org/ontology/child", "http://dbpedia.org/ontology/parent"),
    ("http://dbpedia.org/ontology/previousInfrastructure", "http://dbpedia.org/ontology/subsequentInfrastructure"),
    ("http://dbpedia.org/ontology/branchFrom", "http://dbpedia.org/ontology/branchTo"),
    ("http://dbpedia.org/ontology/predecessor", "http://dbpedia.org/ontology/successor"),
    ("http://dbpedia.org/ontology/nextEvent", "http://dbpedia.org/ontology/previousEvent"),
    ("http://dbpedia.org/ontology/doctoralAdvisor", "http://dbpedia.org/ontology/doctoralStudent"),
    ("http://dbpedia.org/ontology/doctoralStudent", "http://dbpedia.org/ontology/doctoralAdvisor"),
    ("http://dbpedia.org/ontology/previousEvent", "http://dbpedia.org/ontology/nextEvent"),
    ("http://dbpedia.org/ontology/followingEvent", "http://dbpedia.org/ontology/previousEvent")
])
inverse_properties
inverse_properties_df = pd.DataFrame(inverse_properties, columns = ["Property", "InverseProperty"])
inverse_properties_df
inverse_properties_df.to_csv(inverse_properties_file, index=False)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13550960428256534, 'fraction_numerical': 0.0073475385745775165, 'mean_word_length': 4.203713817586019, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '260051', 'license': 'MIT', 'n_tokens_mistral': 3159, 'n_tokens_neox': 3046, 'n_words': 414, 'path': 'RDF2Vec_Experiments/DL-Learner_enrichment.ipynb', 'repo_name': 'andreeaiana/rdf2vec-materialization', 'size': '9527'}