Skip to content

Commit

Permalink
Initial work on quantifier example
Browse files Browse the repository at this point in the history
  • Loading branch information
haberchr committed Oct 31, 2023
1 parent bada161 commit ac25c5b
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/examples/learn_quant/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import pandas as pd
3 changes: 3 additions & 0 deletions src/examples/learn_quant/grammar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from altk.language.grammar import Grammar, Rule

quantifiers_grammar = Grammar.from_yaml("../grammar.yml")
91 changes: 91 additions & 0 deletions src/examples/learn_quant/grammar.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
start: bool
rules:
# boolean / propositional logic rules
- lhs: bool
rhs:
- bool
- bool
name: "and"
function: |
lambda p1 , p2 : p1 and p2
- lhs: bool
rhs:
- bool
- bool
name: "or"
function: |
lambda p1 , p2 : p1 or p2
- lhs: bool
rhs:
- bool
name: "not"
function: |
lambda p : not p
# set logic rules
- lhs: set
rhs:
- set
- set
name: "union"
function: |
lambda s1, s2 : s1 | s2
- lhs: set
rhs:
- set
- set
name: "intersection"
function: |
lambda s1, s2 : s1 & s2
- lhs: set
rhs:
- set
- set
name: "difference"
function: |
lambda s1, s2 : s1 - s2
- lhs: set
rhs:
- int
- set
name: "index"
function: |
lambda i, s: set(x for x in s if x.index == i)
- lhs: int
rhs:
- set
name: "cardinality"
function: |
lambda s: s.len()
- lhs: bool
rhs:
- set
- set
name: "subset_eq"
function: |
lambda s1, s2: s1.issubset(s2)
# integer logical rules
- lhs: bool
rhs:
- int
- int
name: "equals"
function: |
lambda i1, i2: i1 == i2
- lhs: bool
rhs:
- int
- int
name: "greater_than"
function: |
lambda i1, i2: i1 > i2
# primitive
- lhs: set
rhs:
name: "A"
function: |
lambda point: point.pertinence in ['A', "both"]
- lhs: set
rhs:
name: "B"
function: |
lambda point: point.pertinence in ['B', "both"]
7 changes: 7 additions & 0 deletions src/examples/learn_quant/index.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
name
1
2
3
4
5
6
11 changes: 11 additions & 0 deletions src/examples/learn_quant/meaning.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import pandas as pd
from altk.language.semantics import Universe

referent_index = pd.read_csv("../index.csv")
referent_pertinence = pd.read_csv("../pertinence.csv")

referents = referent_index.merge(referent_pertinence, how='cross')
referents.columns = ['name', 'pertinence']

universe = Universe.from_dataframe(referents)

1 change: 1 addition & 0 deletions src/examples/learn_quant/outputs/generated_expressions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]
5 changes: 5 additions & 0 deletions src/examples/learn_quant/pertinence.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name
neither
A
B
both
38 changes: 38 additions & 0 deletions src/examples/learn_quant/scripts/generate_expressions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from yaml import dump

try:
from yaml import CDumper as Dumper
except ImportError:
from yaml import Dumper

from examples.learn_quant.grammar import quantifiers_grammar
from examples.learn_quant.meaning import universe as quantifiers_universe

import os
print(os.getcwd())


if __name__ == "__main__":
expressions_by_meaning = quantifiers_grammar.get_unique_expressions(
5,
max_size=2 ** len(quantifiers_universe),
unique_key=lambda expr: expr.evaluate(quantifiers_universe),
compare_func=lambda e1, e2: len(e1) < len(e2),
)
print(quantifiers_universe)

# filter out the trivial meaning, results in NaNs
# iterate over keys, since we need to change the dict itself
for meaning in list(expressions_by_meaning.keys()):
if len(meaning.referents) == 0:
del expressions_by_meaning[meaning]

with open("../outputs/generated_expressions.yml", "w") as outfile:
dump(
[
expressions_by_meaning[meaning].to_dict()
for meaning in expressions_by_meaning
],
outfile,
Dumper=Dumper,
)
12 changes: 12 additions & 0 deletions src/examples/learn_quant/scripts/generation_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from yaml import dump

try:
from yaml import CDumper as Dumper
except ImportError:
from yaml import Dumper

from examples.learn_quant.grammar import quantifiers_grammar
from examples.learn_quant.meaning import universe as quantifiers_universe

if __name__ == "__main__":
quantifiers_grammar.generate()

0 comments on commit ac25c5b

Please sign in to comment.