PYKE rules to group-by similar statements in a fact base

110 views
Skip to first unread message

sam helsen

unread,
May 10, 2016, 12:13:36 PM5/10/16
to PyKE
Hi,

I have a function that generates PYKE statements and I would like to group-by all the similar statements.

For example,

I have a Fact base: 

ent_rel(id, first_name, second_name, count_of_occurance)
ent_rel(1, sam, helsen,2)
ent_rel(1, sam, dunkin,1)
ent_rel(1, pirate, sam,2)

ent_rel(2, van, helsen,2)
ent_rel(2, sam, helsen,2)
ent_rel(2, pirate, bay,1)
ent_rel(2, van, burger,1)

ent_rel(3, burger, house,1)
ent_rel(3, sam, helsen,1)

I would like an output like:
ent_rel1(sam, helsen,5)
ent_rel1(sam, dunkin,1)
ent_rel1(pirate, sam,2)
ent_rel1(pirate, bay,1)
ent_rel1(van, helsen,2)
ent_rel1(van, burger,1)
ent_rel1(burger, house,1)

Is their any PYKE group-by way to do it? 
Referring the Family example of PYKE, I tried writing recurring rules, but couldn't make it work for this case
I'd appreciate any help.

Thank you in advance

sam helsen

unread,
May 11, 2016, 8:25:36 AM5/11/16
to PyKE
Hi, 

Looking at different solutions from PYKE example base, I have solved this problem. However, I would like to know if the solution, which I propose would be viable for large datasets.

I have achieved the result using two fact bases. Please find my solution below.

UnqNames
    foreach
        doc.ent_rel($_,$f,$s,$_)
    assert
        doc.UnqNames($f,$s)

Ent_n_Occurance
    foreach
    doc.UnqNames($f, $s)
        python tot = 0
        forall
            doc.ent_rel($_, $f, $s, $n)
            python tot += $n
        $tot = tot
    assert
    doc.ent_rel1( $f, $s, $tot)

Output:

ent_rel1('sam', 'helsen', 5)
ent_rel1('sam', 'dunkin', 1)
ent_rel1('pirate', 'sam', 2)
ent_rel1('vam', 'helsen', 2)
ent_rel1('pirate', 'bay', 1)
ent_rel1('vam', 'burger', 1)
ent_rel1('burger', 'house', 1)

Please post other solutions......
Reply all
Reply to author
Forward
0 new messages