Pular para o conteúdo principal

KGTK x Query using Context Meta-information (Quad)

Utilizando o toolkit KGTK em testes na máquina VM029

cd kgtk
conda activate kgtk-env

GRAPH=meta_inf.data
PATHS=path.tsv
FRIENDS=friends.tsv

Consultas ... arquivo meta_inf.kypher

kgtk query -i $GRAPH --match '()-[]->()'

kgtk query -i $GRAPH --match '(:Person)<-[t2:type]-(p)-[r]->(c)-[t1:type]->(:Country), (c_name)<-[n:name]-(c), (r)-[:date_of_start]->(v)' --where 'c_name =  "\"United Kingdom\"" AND v = "^2014-01-01"' --return 'r, p, c, v'

kgtk query -i $GRAPH --match '(:Person)<-[t:type]-(p)-[k:FRIENDS_WITH]->(f),(p)-[n1:name]->(p_name), (f)-[n2:name]->(f_name), (k)-[:date_of_start]->(v) ' --where 'v = "^2014-01-01"' --return 'p_name, f_name, v'

kgtk query -i $GRAPH --match '(:Person)<-[t:type]-(p)-[k:FRIENDS_WITH]->(f), (p)-[n1:name]->(p_name), (f)-[n2:name]->(f_name), (k)-[:date_of_start]->(v) ' --where 'v > "^2010-01-01"' --return 'p_name, f_name, v'

kgtk query -i $GRAPH --match '(:Person)<-[t1:type]-(p)-[k1:FRIENDS_WITH]->(f1), (:Person)<-[t2:type]-(p)-[k2:FRIENDS_WITH]->(f2), (p)-[n1:name]->(p_name), (f2)-[n2:name]->(f_name), (k1)-[:date_of_start]->(v1), (k2)-[:date_of_start]->(v2) ' --where 'v2 > v1' --return 'p_name, f_name, v2'

kgtk query -i $GRAPH --match '(:Person)<-[t1:type]-(p1)-[k1:FRIENDS_WITH]->(p3)-[t2:type]->(:Person), (:Person)<-[t3:type]-(p2)-[k1:FRIENDS_WITH]->(p4)-[t4:type]->(:Person), (p1)-[n1:name]->(p1_name), (p2)-[n2:name]->(p2_name), (p3)-[n3:name]->(p3_name), (p4)-[n4:name]->(p4_name), (k1)-[:date_of_start]->(v1), (k2)-[:date_of_start]->(v2) ' --where 'v2 = v1 AND p1 != p2' --return 'p1_name, p2_name, p3_name, p4_name, v1, v2' --force

kgtk query -i $GRAPH --match '(:Person)<-[t1:type]-(p1)-[l1:LIVING_IN]->(c1)-[t2:type]->(:Country), (:Person)<-[t3:type]-(p2)-[l2:LIVING_IN]->(c2)-[t4:type]->(:Country), (p1)-[n1:name]->(p1_name), (p2)-[n2:name]->(p2_name), (c1)-[n3:name]->(c1_name), (c2)-[n4:name]->(c2_name), (l1)-[:date_of_start]->(v), (l2)-[:date_of_start]->(v) ' --where 'p1 != p2' --return 'p1_name, p2_name, c1_name, c2_name, v' --force

Porém a linguagem kypher não suporta consultas de caminho de tamanhos variáveis entre dois nós, como na tentativa abaixo (que não gera erro!!!).

kgtk query -i $GRAPH --match '(:Person)<-[t1:type]-(p1)-[k:FRIENDS_WITH*1..2]->(p2)-[t2:type]->(:Person), (k)-[:date_of_start]->(v)' --where 'v < "^2013-01-01"'  --return 'k, v, p1, p2'

Nesse tipo de consulta se faz necessário utilizar outro recurso do toolkit kgtk: o comando paths. A saída desse comando gera um arquivo de caminhos possíveis entre pares de nós e a partir desse arquivo é possível realizar o filtro dos qualificadores.

kgtk query -i $GRAPH --match '()-[:FRIENDS_WITH]->()' -o $FRIENDS

kgtk paths --max_hops 2 --path-file pairs.tsv --path-mode NONE --path-source source --path-target target -i $FRIENDS -o $PATHS --statistics-only

kgtk query -i $GRAPH -i $PATHS --match 'p: (pathIDx)-[pathSeqx]->(pathEdgex), m: (pathEdgex)-[:date_of_start]->(vx)' --where 'vx >= "^2013-01-01"' --return 'pathIDx, pathSeqx.label, pathEdgex, pathSeqx' -o not_path.tsv

kgtk query -i $GRAPH --as g -i $PATHS -i not_path.tsv --as n --match 'n: (pathIDx)-[pathSeqx]->(pathEdgex), p: (pathIDy)-[pathSeqy]->(pathEdgey), g: (pathEdgey)-[:date_of_start]->(vy)' --where 'pathIDx != pathIDy' --return 'pathIDy, pathSeqy.label, pathEdgey, pathSeqy, vy' --force --order-by 'pathIDy, pathSeqy.label, pathEdgey, pathSeqy'

Mas essa solução funcionou pontualmente, principalmente a última query, pq só tem um caminho inválido

Consegui resolver esse caso da seguinte forma (filtrando as arestas antes de usar o path)

kgtk query -i $GRAPH --match '(n1)-[pathEdgex:FRIENDS_WITH]->(n2), (pathEdgex)-[:date_of_start]->(vx)' --where 'vx > "^2013-01-01"' --return 'pathEdgex, n1, pathEdgex.label , n2' -o query_path.tsv

kgtk paths --max_hops 2 --path-file pairs.tsv --path-mode NONE --path-source source --path-target target -i query_path.tsv -o $PATHS --statistics-only


Dados ... meta_inf.data


id    node1    label    node2
c1e1    c1    type    Country
c1e2    c1    name    "Germany"
c1e3    c1    language    "German"
c1e4    c1    continent    "Europe"
c1e5    c1    population    83000000
c2e1    c2    type    Country
c2e2    c2    name    "France"
c2e3    c2    language    "French"
c2e4    c2    continent    "Europe"
c2e5    c2    population    67000000
c3e1    c3    type    Country
c3e2    c3    name    "United Kingdom"
c3e3    c3    language    "English"
c3e4    c3    continent    "Europe"
c3e5    c3    population    66000000
p1e1    p1    type    Person
p1e2    p1    name    "John"
p2e1    p2    type    Person
p2e2    p2    name    "Harry"
p3e1    p3    type    Person
p3e2    p3    name    "Anna"
lp1c1    p1    LIVING_IN    c1
lp2c1    p2    LIVING_IN    c1
lp3c3    p3    LIVING_IN    c3
lp3c1    p3    LIVING_IN    c1
wp1c3    p1    WORKING_IN    c3
wp2c1    p2    WORKING_IN    c1
fp1p2    p1    FRIENDS_WITH    p2
fp3p1    p3    FRIENDS_WITH    p1
fp3p2    p3    FRIENDS_WITH    p2
lp1c1q1    lp1c1    date_of_start    ^2013-01-01
lp2c1q1    lp2c1    date_of_start    ^2014-01-01
lp3c3q1    lp3c3    date_of_start    ^2016-01-01
lp3c1q1    lp3c1    date_of_start    ^2014-01-01
wp1c3q1    wp1c3    date_of_start    ^2014-01-01
wp2c1q1    wp2c1    date_of_start    ^2014-01-01
fp1p2q1    fp1p2    date_of_start    ^2011-01-01
fp3p1q1    fp3p1    date_of_start    ^2012-01-01
fp3p2q1    fp3p2    date_of_start    ^2014-01-01

 

Comentários

Postagens mais visitadas deste blog

Connected Papers: Uma abordagem alternativa para revisão da literatura

Durante um projeto de pesquisa podemos encontrar um artigo que nos identificamos em termos de problema de pesquisa e também de solução. Então surge a vontade de saber como essa área de pesquisa se desenvolveu até chegar a esse ponto ou quais desdobramentos ocorreram a partir dessa solução proposta para identificar o estado da arte nesse tema. Podemos seguir duas abordagens:  realizar uma revisão sistemática usando palavras chaves que melhor caracterizam o tema em bibliotecas digitais de referência para encontrar artigos relacionados ou realizar snowballing ancorado nesse artigo que identificamos previamente, explorando os artigos citados (backward) ou os artigos que o citam (forward)  Mas a ferramenta Connected Papers propõe uma abordagem alternativa para essa busca. O problema inicial é dado um artigo de interesse, precisamos encontrar outros artigos relacionados de "certa forma". Find different methods and approaches to the same subject Track down the state of the art rese...

Knowledge Graphs as a source of trust for LLM-powered enterprise question answering - Leitura de Artigo

J. Sequeda, D. Allemang and B. Jacob, Knowledge Graphs as a source of trust for LLM-powered enterprise question answering, Web Semantics: Science, Services and Agents on the World Wide Web (2025), doi: https://doi.org/10.1016/j.websem.2024.100858. 1. Introduction These question answering systems that enable to chat with your structured data hold tremendous potential for transforming the way self service and data-driven decision making is executed within enterprises. Self service and data-driven decision making in organizations today is largly made through Business Intelligence (BI) and analytics reporting. Data teams gather the original data, integrate the data, build a SQL data warehouse (i.e. star schemas), and create BI dashboards and reports that are then used by business users and analysts to answer specific questions (i.e. metrics, KPIs) and make decisions. The bottleneck of this approach is that business users are only able to answer questions given the views of existing dashboa...

Knowledge Graph Toolkit (KGTK)

https://kgtk.readthedocs.io/en/latest/ KGTK represents KGs using TSV files with 4 columns labeled id, node1, label and node2. The id column is a symbol representing an identifier of an edge, corresponding to the orange circles in the diagram above. node1 represents the source of the edge, node2 represents the destination of the edge, and label represents the relation between node1 and node2. >> Quad do RDF, definir cada tripla como um grafo   KGTK defines knowledge graphs (or more generally any attributed graph or hypergraph ) as a set of nodes and a set of edges between those nodes. KGTK represents everything of meaning via an edge. Edges themselves can be attributed by having edges asserted about them, thus, KGTK can in fact represent arbitrary hypergraphs. KGTK intentionally does not distinguish attributes or qualifiers on nodes and edges from full-fledged edges, tools operating on KGTK graphs can instead interpret edges differently if they so desire. In KGTK, e...