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

Aula 12: WordNet | Introdução à Linguagem de Programação Python *** com NLTK

 Fonte -> https://youtu.be/0OCq31jQ9E4 A WordNet do Brasil -> http://www.nilc.icmc.usp.br/wordnetbr/ NLTK  synsets = dada uma palavra acha todos os significados, pode informar a língua e a classe gramatical da palavra (substantivo, verbo, advérbio) from nltk.corpus import wordnet as wn wordnet.synset(xxxxxx).definition() = descrição do significado É possível extrair hipernimia, hiponimia, antonimos e os lemas (diferentes palavras/expressões com o mesmo significado) formando uma REDE LEXICAL. Com isso é possível calcular a distância entre 2 synset dentro do grafo.  Veja trecho de código abaixo: texto = 'útil' print('NOUN:', wordnet.synsets(texto, lang='por', pos=wordnet.NOUN)) texto = 'útil' print('ADJ:', wordnet.synsets(texto, lang='por', pos=wordnet.ADJ)) print(wordnet.synset('handy.s.01').definition()) texto = 'computador' for synset in wn.synsets(texto, lang='por', pos=wn.NOUN):     print('DEF:',s

truth makers AND truth bearers - Palestra Giancarlo no SBBD

Dando uma googada https://iep.utm.edu/truth/ There are two commonly accepted constraints on truth and falsehood:     Every proposition is true or false.         [Law of the Excluded Middle.]     No proposition is both true and false.         [Law of Non-contradiction.] What is the difference between a truth-maker and a truth bearer? Truth-bearers are either true or false; truth-makers are not since, not being representations, they cannot be said to be true, nor can they be said to be false . That's a second difference. Truth-bearers are 'bipolar,' either true or false; truth-makers are 'unipolar': all of them obtain. What are considered truth bearers?   A variety of truth bearers are considered – statements, beliefs, claims, assumptions, hypotheses, propositions, sentences, and utterances . When I speak of a fact . . . I mean the kind of thing that makes a proposition true or false. (Russell, 1972, p. 36.) “Truthmaker theories” hold that in order for any truthbe

DGL-KE : Deep Graph Library (DGL)

Fonte: https://towardsdatascience.com/introduction-to-knowledge-graph-embedding-with-dgl-ke-77ace6fb60ef Amazon recently launched DGL-KE, a software package that simplifies this process with simple command-line scripts. With DGL-KE , users can generate embeddings for very large graphs 2–5x faster than competing techniques. DGL-KE provides users the flexibility to select models used to generate embeddings and optimize performance by configuring hardware, data sampling parameters, and the loss function. To use this package effectively, however, it is important to understand how embeddings work and the optimizations available to compute them. This two-part blog series is designed to provide this information and get you ready to start taking advantage of DGL-KE . Finally, another class of graphs that is especially important for knowledge graphs are multigraphs . These are graphs that can have multiple (directed) edges between the same pair of nodes and can also contain loops. The