Convert Abstract Meaning Representation (AMR) into first-order logic
Project description
AMR Logic Converter
Convert Abstract Meaning Representation (AMR) to first-order logic statements.
This library is based on the ideas in the paper "Expressive Power of Abstract Meaning Representations", J. Bos, Computational Linguistics 42(3), 2016. Thank you to @jobos for the paper!
Installation
pip install amr-logic-converter
Usage
This library parses an AMR tree into first-order logic statements. An example of this is shown below:
from amr_logic_converter import AmrLogicConverter
converter = AmrLogicConverter()
AMR = """
(x / boy
:ARG0-of (e / giggle-01
:polarity -))
"""
logic = converter.convert(AMR)
print(logic)
# boy(x) ^ ¬(:ARG0(e, x) ^ giggle-01(e))
Programmatic logic manipulation
The output from the convert
method can be displayed as a string, but it can also be manipulated in Python. For instance, in the example above, we could also write:
converter = AmrLogicConverter()
AMR = """
(x / boy
:ARG0-of (e / giggle-01
:polarity -))
"""
expr = converter.convert(AMR)
type(expr) # <class 'amr_logic_converter.types.And'>
expr.args[0] # Atom(predicate=Predicate(symbol='boy', alignment=None), terms=(Constant(value='x', type='instance', alignment=None),))
Working with alignment markers
This library will parse alignment markers from AMR using the penman library, and will include Alignment objects from penman in Predicate
and Const
objects when available. For example, we can access alignment markers like below:
converter = AmrLogicConverter()
AMR = """
(x / boy~1
:ARG0-of (e / giggle-01~3
:polarity -))
"""
expr = converter.convert(AMR)
expr.args[0].alignment # Alignment((1,))
expr.args[1].body.args[1].alignment # Alignment((3,))
Existentially Quantifying all Instances
In "Expressive Power of Abstract Meaning Representations", all instances are wrapped by an existence quantifier. By default AmrLogicConverter
does not include these as it's likely not useful, but if you'd like to include them as in the paper you can pass the option existentially_quantify_instances=True
when constructing the AmrLogicConverter
as below:
converter = AmrLogicConverter(existentially_quantify_instances=True)
AMR = """
(x / boy
:ARG0-of (e / giggle-01
:polarity -))
"""
logic = converter.convert(AMR)
print(logic)
# ∃X(boy(X) ^ ¬∃E(:ARG0(E, X) ^ giggle-01(E)))
Coreference Hoisting
When an instance is coreferenced in multiple places in the AMR, it's necessary to hoisting the existential quantification of that variable high enough that it can still wrap all instances of that variable. By default, the existential quantifier will be hoisted to the level of the lowest common ancestor of all nodes in the AMR tree where an instance is coreferenced. However, in "Expressive Power of Abstract Meaning Representations", these coreferences are instead hoisted to the maximal possible scope, wrapping the entire formula. If you want this behavior, you can specify the option maximally_hoist_coreferences=True
when creating the AmrLogicConverter
instance. This is illustrated below:
AMR = """
(b / bad-07
:polarity -
:ARG1 (e / dry-01
:ARG0 (x / person
:named "Mr Krupp")
:ARG1 x))
"""
# default behavior, hoist only to the lowest common ancestor
converter = AmrLogicConverter(
existentially_quantify_instances=True,
)
logic = converter.convert(AMR)
print(logic)
# ¬∃B(bad-07(B) ∧ ∃E(∃X(:ARG1(B, E) ∧ person(X) ∧ :named(X, "Mr Krupp") ∧ dry-01(E) ∧ :ARG0(E, X) ∧ :ARG1(E, X))))
# maximally hoist coferences
converter = AmrLogicConverter(
existentially_quantify_instances=True,
maximally_hoist_coreferences=True,
)
logic = converter.convert(AMR)
print(logic)
# ∃X(¬∃B(bad-07(B) ∧ ∃E(:ARG1(B, E) ∧ dry-01(E) ∧ :ARG0(E, X) ∧ :ARG1(E, X))) ∧ person(X) ∧ :named(X, "Mr Krupp"))
Using Variables for Instances
If you want to use variables for each AMR instance instead of constants, you can pass the option use_variables_for_instances=True
when creating the AmrLogicConverter instance. When existentially_quantify_instances
is set, variable will always be used for instances regardless of this setting.
Misc Options
- By default variables names are capitalized, but you can change this by setting
capitalize_variables=False
. - By default, relations like
:ARG0-of(X, Y)
have their arguments flipped in logic and turned into:ARG0(Y, X)
. If you don't want this normalization to occur, you can disable this by settinginvert_relations=False
.
Contributing
Contributions are welcome! Please leave an issue in the Github repo if you find any bugs, and open a pull request with and fixes or improvements that you'd like to contribute. Ideally please include new test cases to verify any changes or bugfixes if appropriate.
This project uses poetry for dependency management and packaging, black for code formatting, flake8 for linting, and mypy for type checking.
License
This project is licenced under a MIT license.
Citation
If you use this software in your work, please cite the following:
@article{chanin2023neuro,
title={Neuro-symbolic Commonsense Social Reasoning},
author={Chanin, David and Hunter, Anthony},
journal={arXiv preprint arXiv:2303.08264},
year={2023}
}
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file amr_logic_converter-0.11.3.tar.gz
.
File metadata
- Download URL: amr_logic_converter-0.11.3.tar.gz
- Upload date:
- Size: 13.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4637c1bf07fb2922a2f740322824744d9be794a3479a40d8634de2c1d6b24404 |
|
MD5 | 98a4b01f3c66843be107d66c46957d16 |
|
BLAKE2b-256 | 4c4b527569aa7b36ec5f67236e4f575897d1ee2c408fe6af20680b98e1fe901d |
File details
Details for the file amr_logic_converter-0.11.3-py3-none-any.whl
.
File metadata
- Download URL: amr_logic_converter-0.11.3-py3-none-any.whl
- Upload date:
- Size: 15.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bfbf0e44cbfa1068c7f0763dc639e92608faec3921c3c6a2e1230405141adb1b |
|
MD5 | 1b3e38820a534fe775f5268933589455 |
|
BLAKE2b-256 | 3c7b425f1c72b82aa31ac355c86399b007ba3bb1ab2ff312b26da496f74ffc3d |