dig-team / amie

Mavenized AMIE+Typing
Other
112 stars 19 forks source link

Clear output for AMIE rules #70

Open lgalarra opened 2 years ago

lgalarra commented 2 years ago

We should consider the output of the rules in different output formats, e.g., JSON. We could also clean the code to output all log messages to the standard error so that users can redirect the standard output to a file to get the rules.

wumirose commented 2 years ago

I have the rules in a data frame and additional information in a separate dictionary.

image

Right now, I am trying to decipher from the rules how can I obtain the actual instance of a and b

example: I have a associates_with b => a causes b

I want: fever associates_with temperature => fever causes temperature.

Any lead on this will be appreciated.

lgalarra commented 2 years ago

Hi,

All the utilities to deal with rules and their predictions are available under https://github.com/lajus/amie/tree/master/rules/src/main/java/amie/rules/eval. In particular have a look at the class Predictor in the file Predictor.java. This class takes a set of rules and draw samples of their predictions (it is possible to tell the class to obtain all predictions). Do not hesitate to ask if you have more questions or doubts.

Best, Luis

wumirose commented 2 years ago

@lgalarra Thanks for your help. Probably because of my little to no experience in Java and the basic idea of rule mining, I tried a few workarounds on the predictor class, but none seemed to work.

While running the AMIE+, I limited my predictions to only a 'treat' edge; eventually, I was left with 70 rules which contain up to a 3-hop rule set. I will appreciate a pointer on getting:

relation1(a.name, h.name) ^ relation2(h.name, b.name) => (a.name, relation,b.name)

from

relation1(a, h) ^ relation2(h, b) => (a, relation,b)

image
lgalarra commented 2 years ago

Hi,

It is true that with the migration to the integer-encoded in-memory DB, things are a bit more complicated. I think you can still build upon the predictor class. You could do something like:

KB kb = new KB();
// Load the KB with the data used to train the rules
int sampleSize = Integer.MAX_VALUE; // If you want all the predictions
List<Rule> rules = new ArrayList<Rule>(AMIEParser.rules(new File(<PATH_TO_FILE_WITH_ONE_RULE_PER_LINE>)));
Predictor pp = new Predictor(kb, sampleSize);
for(Rule rule: rules){
    Object predictions = predictor.generatePredictions(rule);
    Collection<IntTriple> newPredictions = predictor.samplePredictions(predictions, rule);
        for (IntTriple triple : newPredictions) {
                String subject = KB.unmap(triple.first());
                String predicate = KB.unmap(triple.second());
                String object = KB.unmap(triple.third());
                // .... Here are your triples
        }
}   

You will need to change the code of the class Predictor to make the private method samplePredictions public.

Best, Luis

wumirose commented 2 years ago

@lgalarra This is insightful; I will try some workarounds with the snippet. Thank you so much.