Gremlin 在向图形添加边之前对余弦相似性分数进行排序

Gremlin 在向图形添加边之前对余弦相似性分数进行排序,gremlin,janusgraph,Gremlin,Janusgraph,我试图得到一个jaccard相似性的例子,发现它对余弦相似性有效,但想将创建的链接数量限制在前10个分数 我复习了一遍,但不知道如何跳过边缘创建部分对其进行排序,从而获得与上面链接相同的结果 根据上面的jaccard示例,到目前为止我已经想到了这一点: g.V(). match( __.as('v1').outE('RECOMMENDS').values('amount').fold().as('v1rec'), __.as('v1').V().as('v2'),

我试图得到一个jaccard相似性的例子,发现它对余弦相似性有效,但想将创建的链接数量限制在前10个分数

我复习了一遍,但不知道如何跳过边缘创建部分对其进行排序,从而获得与上面链接相同的结果

根据上面的jaccard示例,到目前为止我已经想到了这一点:

g.V().
    match(
     __.as('v1').outE('RECOMMENDS').values('amount').fold().as('v1rec'),
     __.as('v1').V().as('v2'),
     __.as('v2').outE('RECOMMENDS').values('amount').fold().as('v2rec'),
     __.as('v1').out().dedup().fold().as('v1n'),
     __.as('v2').out().dedup().fold().as('v2n')
    ).
    where('v1',lt('v2')).
         by(id).
    where('v1',neq('v2').and(without('v1n'))).
    where('v2',without('v1n')).
    project('v1','v2','n','d1','d2').
     by(select('v1')).
     by(select('v2')).
     by(
         select('v1rec','v2rec') <-- this does not work, can't get dot product from this
     ).
     by(coalesce(
            select('v1rec').
                unfold().
                math('_ ^ 2').
                sum(),
             constant(0))).
     by(coalesce(
            select('v2rec').
                unfold().
                math('_ ^ 2').
                sum(),
            constant(0))).
    filter(select('d1').is(gt(0))).
    filter(select('d2').is(gt(0))).
    project('v1','v2','cosine').
         by(select('v1')).
         by(select('v2')).
         by(math('n/(sqrt(d1)*sqrt(d2))')).
    sort{-it.cosine}.
    toList()[0..9].
    each {
         r -> g.V(r['v2']).as('v2').
         V(r['v1']).
         addE('PREDICTED_COSINE').
         to('v2').
         property('score', r['cosine']).
         toList()
    }
一行输出:

[代码>>={码>>{码>>{码>>{码>>{1=v[4240],v2=v[8320]n=210.0,d1=196.0,d2=225.0,d2=225.0},{1=v[4240[4240[[[[4240],{1=1=v[[4240],v2=v[[8320],n=210.0,n=210.0,d1=210.0,d1=1.0,d1=1=196.0,d1=1.0,d1=196.0,d1=196.0,d1=196.0,d2.0,D20,d2.0,D20,d2[8320],d2[8320],d2[20],n=182.0,n=0,n=182.0,n=0,n=0,d1.0,d1.0,d1.0,d1.0,n=39.0,d1=9.0,d2=169.0},{v1=v[4240],v2=v[8320],n=39.0,d1=9.0,d2=169.0},{v1=v[4240],v2=v[8320],n=45.0,d1=9.0,d2=225.0},{v1=v[4240],v2=v[8320],n=39.0,d1=9.0,d2=169.0},{v1=v[4240],v2=v[8320],n=39.0,d1=9.0,d2=169.0}


我的目标是将映射中的所有“n”、“d1”和“d2”值相加,这样我就可以计算出示例列表之外的每个键(例如{v1=v[4240],v2=v[8320])的相似度为sum(n)/(sqrt(sum(d1))*sqrt(sum(d2)),因此n将是210+182+182+45+39+39+820)。我想对一组图这样做,所以我没有一个具体的。现在明白了吗?

这就是我最后想到的:

g.V().
    match(
        __.as('v1').outE().as('e1'),
        __.as('v1').V().as('v2'),
        __.as('v2').outE().as('e2'),
        __.as('v1').out().dedup().fold().as('v1n'),
        __.as('v2').out().dedup().fold().as('v2n')
    ).
    where('v1',neq('v2').
        and(without('v1n'))).
    where('v2',without('v1n')).
    project('v1','v2','a1','a2').
        by(select('v1')).
        by(select('v2')).
        by(select('e1').by('amount')).
        by(select('e2').by('amount')).
    project('v1','v2','n','d1','d2').
        by(select('v1')).
        by(select('v2')).
        by(math('a1 * a2')).
        by(math('a1 * a1')).
        by(math('a2 * a2')).
    group().
        by(select('v1','v2')).
        unfold().
    project('v1','v2','n','d1','d2').
        by(select(keys).select('v1')).
        by(select(keys).select('v2')).
        by(select(values).local(unfold().select('n').sum())).
        by(select(values).local(unfold().select('d1').sum())).
        by(select(values).local(unfold().select('d2').sum())).
    project('v1','v2','c').
        by(select('v1')).
        by(select('v2')).
        by(math('n / (sqrt(d1) * sqrt(d2))')).
    sort{ -it.c }.
    toList()[0..9]

谢谢您的帮助。

您能提供更多详细信息吗?到底是什么不起作用?你有(小)样本图和预期结果吗?
g.V().
    match(
        __.as('v1').outE().as('e1'),
        __.as('v1').V().as('v2'),
        __.as('v2').outE().as('e2'),
        __.as('v1').out().dedup().fold().as('v1n'),
        __.as('v2').out().dedup().fold().as('v2n')
    ).
    where('v1',neq('v2').
        and(without('v1n'))).
    where('v2',without('v1n')).
    project('v1','v2','a1','a2').
        by(select('v1')).
        by(select('v2')).
        by(select('e1').by('amount')).
        by(select('e2').by('amount')).
    project('v1','v2','n','d1','d2').
        by(select('v1')).
        by(select('v2')).
        by(math('a1 * a2')).
        by(math('a1 * a1')).
        by(math('a2 * a2')).
    group().
        by(select('v1','v2')).
        unfold().
    project('v1','v2','n','d1','d2').
        by(select(keys).select('v1')).
        by(select(keys).select('v2')).
        by(select(values).local(unfold().select('n').sum())).
        by(select(values).local(unfold().select('d1').sum())).
        by(select(values).local(unfold().select('d2').sum())).
    project('v1','v2','c').
        by(select('v1')).
        by(select('v2')).
        by(math('n / (sqrt(d1) * sqrt(d2))')).
    sort{ -it.c }.
    toList()[0..9]