Esempio n. 1
0
def subset_data(df, heads, threshold):
    if isinstance(heads, str):
        heads = [heads]
    data = df.copy()
    data = data.loc[(data["head"].isin(heads))
                    & (data["confidence"] > threshold), ]
    store = TripleStore()
    for idx, rel in data.iterrows():
        store.add_triple(rel["head"], rel["relation"], rel["tail"])

    return store
Esempio n. 2
0
def get_inspired():
    sparql = SPARQLWrapper("http://dbpedia.org/sparql")

    query_string = """
  SELECT ?name_pe1_en ?rel_en ?name_pe2_en
  WHERE {
    {
         SELECT ?name_p1 ?rel ?name_p2
         WHERE {
             ?p1 a foaf:Person .
             ?p1 dbo:influencedBy ?p2 .
             ?p2 a foaf:Person .
             ?p1 foaf:name ?name_p1 .
             ?p2 foaf:name ?name_p2 .
            dbo:influencedBy rdfs:label ?rel .
            }
         LIMIT 100
    }
    UNION
    {
         SELECT ?name_p1 ?rel ?name_p2
         WHERE {
            ?p1 a foaf:Person .
            ?p1 dbo:influenced ?p2 .
            ?p2 a foaf:Person .
            ?p1 foaf:name ?name_p1 .
            ?p2 foaf:name ?name_p2 .
            dbo:influenced rdfs:label ?rel .
        }
     LIMIT 100
    }
    FILTER ( LANG(?name_p1) = "en" && LANG(?rel) = "en" && LANG(?name_p2) = "en" )
    BIND ( STR(?name_p1) AS ?name_pe1_en )
    BIND ( STR(?rel) AS ?rel_en )
    BIND ( STR(?name_p2) AS ?name_pe2_en )
  }
  """

    sparql.setQuery(query_string)
    sparql.setReturnFormat(JSON)
    results = sparql.query().convert()
    store = TripleStore()
    for result in results["results"]["bindings"]:
        node1 = result["name_pe1_en"]["value"]
        link = result["rel_en"]["value"]
        node2 = result["name_pe2_en"]["value"]
        store.add_triple(node1, link, node2)
    return store
Esempio n. 3
0
def load_persons(recource, store:TripleStore=None):

  if store == None:
    store = TripleStore()

  sparql = SPARQLWrapper("http://dbpedia.org/sparql")
  # queryString = "SELECT * WHERE { ?s ?p ?o. }"
  target = f"<http://dbpedia.org/resource/{recource}>"
  query_string = "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> " \
                  "PREFIX dbo: <http://dbpedia.org/ontology/>"  \
                  "SELECT ?label ?birthPlace ?birthDate ?deathDate ?picture ?abstract " \
                  f"WHERE {{ {target} " \
                                                            "rdfs:label ?label ." \
                                                            f"OPTIONAL {{ {target} " \
                                                            "dbo:birthPlace ?birthPlace;" \
                                                            "dbo:birthDate ?birthDate;" \
                                                            "dbo:deathDate ?deathDate;" \
                                                            "dbo:abstract ?abstract;" \
                                                            "dbo:thumbnail ?picture ." \
                                                            "}" \
        "filter langMatches( lang(?label), 'EN' ) }" \
        "LIMIT 1"
  # st.write(query_string)
  sparql.setQuery(query_string)
  # sparql = SPARQLWrapper("http://dbpedia.org/sparql/resource/Asturias")
  # sparql.setReturnFormat(N3)
  sparql.setReturnFormat(JSON)
 #  sparql.setQuery(queryString)
  results = sparql.query().convert()
  abstract = ""
  picture = ""
  for result in results["results"]["bindings"]:
    subj = result["label"]["value"]
    if "picture" in result:
      picture = result["picture"]["value"]
    if "abstract" in result:
      abstract = result["abstract"]["value"]
    # pic, picture = circle_image(picture, size=(300, 300))
    for label in result:
      if not label == "picture" and not label == subj and not label == "abstract":
        pred = label
        if "http://dbpedia.org/resource/" in result[label]["value"]:
          obj = result[label]["value"].rsplit("/", 1)[1]
        else:
          obj = result[label]["value"]
        # st.write(subj, pred, obj)
        store.add_triple(subj, pred, obj, picture)
  return store, abstract
Esempio n. 4
0
def do_query(recource):

  sparql = SPARQLWrapper("http://dbpedia.org/sparql")
  # queryString = "SELECT * WHERE { ?s ?p ?o. }"

  query_string = "PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#> " \
                  "PREFIX dbo: <http://dbpedia.org/ontology/>"  \
                  "SELECT ?label ?birthPlace ?birthDate ?picture " \
                  f"WHERE {{ <http://dbpedia.org/resource/{recource}> " \
                                                            "rdfs:label ?label;" \
                                                            "dbo:birthPlace ?birthPlace;" \
                                                            "dbo:birthDate ?birthDate;" \
                                                            "dbo:thumbnail ?picture ." \
        "filter langMatches( lang(?label), 'EN' ) }" \
        "LIMIT 1"
  # st.write(query_string)
  sparql.setQuery(query_string)
  # sparql = SPARQLWrapper("http://dbpedia.org/sparql/resource/Asturias")
  # sparql.setReturnFormat(N3)
  sparql.setReturnFormat(JSON)
 #  sparql.setQuery(queryString)
  results = sparql.query().convert()

  store = TripleStore()

  for result in results["results"]["bindings"]:
    subj = result["label"]["value"]
    picture = result["picture"]["value"]
    picture = circle_image(picture, size=(300, 300))
    for label in result:
      if not label == "picture" and not label ==subj:
        pred = label
        if "http://dbpedia.org/resource/" in result[label]["value"]:
          obj = result[label]["value"].rsplit("/", 1)[1]
        else:
          obj = result[label]["value"]
        # st.write(subj, pred, obj)
        store.add_triple(subj, pred, obj, picture)
  return list(store.nodes_set), list(store.edges_set)
Esempio n. 5
0
def app():
    footer()
    st.title("Graph Example")
    st.sidebar.title("Welcome")
    query_type = st.sidebar.selectbox(
        "Query Tpye: ", ["Inspirationals", "Marvel"]
    )  # could add more stuff here later on or add other endpoints in the sidebar.
    config = Config(height=600,
                    width=700,
                    nodeHighlightBehavior=True,
                    highlightColor="#F7A7A6",
                    directed=True,
                    collapsible=True)

    if query_type == "Inspirationals":
        st.subheader("Inspirationals")
        with st.spinner("Loading data"):
            store = get_inspired()
            st.write("Nodes loaded: " + str(len(store.getNodes())))
        st.success("Done")
        agraph(list(store.getNodes()), (store.getEdges()), config)

    if query_type == "Marvel":
        #based on http://marvel-force-chart.surge.sh/
        with open("./marvel.json", encoding="utf8") as f:
            marvel_file = json.loads(f.read())
            marvel_store = TripleStore()
            for sub_graph in marvel_file["children"]:
                marvel_store.add_triple(marvel_file["name"],
                                        "has_subgroup",
                                        sub_graph["name"],
                                        picture=marvel_file["img"])
                for node in sub_graph["children"]:
                    node1 = node["hero"]
                    link = "blongs_to"
                    node2 = sub_graph["name"]
                    pic = node["img"]
                    marvel_store.add_triple(node1, link, node2, picture=pic)
            agraph(list(marvel_store.getNodes()), (marvel_store.getEdges()),
                   config)
def beliefgraph_to_triplestore(graph, nodes: set):
    store = TripleStore()
    for triplet in graph.filtered_triplets:
        if triplet.head in nodes:
            store.add_triple(triplet.head, triplet.relation, triplet.tail)
    return store
Esempio n. 7
0
    config = Config(height=500, width=700, nodeHighlightBehavior=True,
                    highlightColor="#F7A7A6", directed=True, collapsible=True)
=======
    config = Config(height=500,
                    width=700,
                    nodeHighlightBehavior=True,
                    highlightColor="#F7A7A6",
                    directed=True,
                    collapsible=True,
                    link={'labelProperty': 'label', 'renderLabel': True}
                    )
>>>>>>> 6f6a9624da414e66674de21213f6c8f387db8519
    if algo_type == "Shortest Path":
      if chosen_person_a != "" and chosen_person_b != "" and chosen_person_a != chosen_person_b:
        analysis_results = algos.shortest_path(chosen_person_a, chosen_person_b)
        sp_store = TripleStore()
        if len(analysis_results) > 0:
          for idx, connection in enumerate(analysis_results):
            n1 = connection.replace(" ", "_")
            if idx + 1 < len(analysis_results):
              n2 = analysis_results[idx + 1]
              sp_store.add_triple(connection, "knows", n2, "")
            sp_store, _ = load_persons(n1, sp_store)
          agraph(list(sp_store.getNodes()), (sp_store.getEdges()), config)
        else:
          st.write("None connections found. Try something else.")
      else:
        st.write("Choose choose different persons first.")
    else:
      agraph(list(store.getNodes()), (store.getEdges()), config)