-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
169 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
import pandas as pd |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
name | ||
1 | ||
2 | ||
3 | ||
4 | ||
5 | ||
6 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
[] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
name | ||
neither | ||
A | ||
B | ||
both |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |