Skip to content

Commit

Permalink
Add feature generation
Browse files Browse the repository at this point in the history
  • Loading branch information
LukasZahradnik committed Aug 27, 2024
1 parent bf87103 commit 21b8d03
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
2 changes: 1 addition & 1 deletion neuralogic/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.7.19"
__version__ = "0.7.20"
10 changes: 10 additions & 0 deletions neuralogic/core/constructs/java_objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,16 @@ def atom_to_clause(self, atom):

return self.clause(literal_array)

def to_clause(self, atoms):
literal_array = self.literal[len(atoms)]

for i, atom in enumerate(atoms):
terms = [self.get_term(term, self.variable_factory) for term in atom.terms]
predicate_name = f"_{atom.predicate.name}" if atom.predicate.hidden else atom.predicate.name

literal_array[i] = self.literal(predicate_name, atom.negated, terms)
return self.clause(literal_array)

def get_generic_relation(self, relation_class, relation, variable_factory, default_weight=None, is_example=False):
predicate = self.get_predicate(relation.predicate)

Expand Down
17 changes: 17 additions & 0 deletions neuralogic/dataset/logic.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from typing import Optional, List, Union, Sequence

import jpype

from neuralogic.core.constructs.java_objects import JavaFactory
from neuralogic.core.constructs.relation import BaseRelation
from neuralogic.core.constructs.rule import Rule
from neuralogic.dataset.base import BaseDataset
Expand Down Expand Up @@ -99,3 +102,17 @@ def set_examples(self, examples: List):

def set_queries(self, queries: List):
self._queries = queries

def generate_features(self, feature_depth: int = 1, count_groundings: bool = True):
java_factory = JavaFactory()
clause = jpype.java.util.ArrayList([java_factory.to_clause(sample.example) for sample in self.samples])

namespace = "cz.cvut.fel.ida.logic.features.generation"

jpype.JClass(f"{namespace}.FeatureGenerationSettings").COUNT_GROUNDINGS = count_groundings
features = jpype.JClass(f"{namespace}.FeatureGenerator").generateFeatures(clause, feature_depth)

table = [[int(i) for i in feats] for feats in features.table]
clauses = [str(clause) for clause in features.features]

return table, clauses
Binary file modified neuralogic/jar/NeuraLogic.jar
Binary file not shown.

0 comments on commit 21b8d03

Please sign in to comment.