pystc


Namepystc JSON
Version 2.0.0 PyPI version JSON
download
home_pagehttps://github.com/toda-lab/pystc
SummaryA simple but extensible Python module for sentences
upload_time2024-03-28 15:24:16
maintainerNone
docs_urlNone
authorTakahisa Toda
requires_python<4.0,>=3.8
licenseMIT
keywords logic artificial intelligence
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            pystc: A simple but extensible Python module for sentences
==========================================================

Introduction
============
A *sentence* is an atomic sentence or a (compound) sentence.
An *atomic sentence* consists of a *predicate* and *constants* the predicate has
as its arguments.
There is no variable, no function.
The number of arguments of a predicate is called the *arity* of the predicate.
A compound sentence can be obtained by joining sentences with *connectives*.

``pystc`` is a simple but extensible Python module for sentences.
It provides functionality to define sentences as you like 
by adding constants, predicates, and connectives, whether logical or non-logical.
Constants, predicates, and connectives can be introduced by specifying
their names and how they are associated with other well-defined objects or
functions.

Installation
============

.. code:: shell-session

    $ pip install pystc

Usage
=====

Let us first create atomic sentences that can be built from a predicate ``=``
and constants ``T``, ``F``, 

.. code:: python

    from pystc import AtomicSentence

    # Let us add predicate "=" so that the arity for it is 2.
    AtomicSentence.add_predicate("=",2) 
    AtomicSentence.add_constant("T")
    AtomicSentence.add_constant("F")

    # Now, sentences are ready to create.
    s1 = AtomicSentence("=","T","T")
    s2 = AtomicSentence("=","T","F")
    s3 = AtomicSentence("=","F","T")

    assert str(s2) == "=(T,F)"
    assert AtomicSentence.read("=(T,F)") == s2

Let us next construct compound sentences.
A ``Sentence`` in ``pystc`` module is simply a recursive type defined to be:

.. code:: python

    Sentence = Union[str, AtomicSentence, Tuple["Sentence"]]

Although it is loosely defined for simplicity, 
sentences are implicitly expected to fall into one of the following cases:

1. An AtomicSentence object, say ``s2``.
1. The string representation of an AtomicSentence object, say ``"=(T,F)"``.
1. A tuple such that the initial entry is a connective name and the other entries are Sentence objects, say ``("&", s2, ("!", s2))``.

As the connective names ``"&"`` and ``"!"`` appears just above, 
let us introduce these connectives in the following codeblock
and inteprete sentences.

.. code:: python

    from pystc import SentenceConverter

    # Let us set an atomic sentence type and how each symbol is interpreted.
    SentenceConverter.set_atom_type(AtomicSentence)
    SentenceConverter.set_constant_destination("T", True)
    SentenceConverter.set_constant_destination("F", False)
    SentenceConverter.set_predicate_destination("=", lambda li,w: li[0]==li[1])
    SentenceConverter.set_connective_destination("&",lambda li,w: not False in li)
    SentenceConverter.set_connective_destination("|",lambda li,w: True in li)
    SentenceConverter.set_connective_destination("!",lambda li,w: not li[0])

    assert SentenceConverter.convert(s2) == False
    assert SentenceConverter.convert("=(T,F)") == False
    assert SentenceConverter.convert(("&", s2, ("!", s2))) == False
    assert SentenceConverter.convert(("&", "=(T,F)", ("!", s2))) == False

For another example of usage, let us convert sentences into strings in infix
notation.

.. code:: python

    # Clear all class variables
    SentenceConverter.clear()

    SentenceConverter.set_atom_type(AtomicSentence)
    SentenceConverter.set_constant_destination("T", "T")
    SentenceConverter.set_constant_destination("F", "F")
    SentenceConverter.set_predicate_destination("=", lambda li,w: f"{li[0]}={li[1]}")
    SentenceConverter.set_connective_destination("&",lambda li,w: "("+" & ".join(li)+")")
    SentenceConverter.set_connective_destination("|",lambda li,w: "("+" | ".join(li)+")")
    SentenceConverter.set_connective_destination("!",lambda li,w: "!"+li[0])

    assert SentenceConverter.convert("=(T,F)") == "T=F"
    assert SentenceConverter.convert(("&", s2, ("!", s2))) == "(T=F & !T=F)"

Let us not forget to clear class variables after everything is finished.

.. code:: python

    SentenceConverter.clear()
    AtomicSentence.clear()


Bugs/Requests/Discussions
=========================

Please report bugs and requests from `GitHub Issues <https://github.com/toda-lab/pystc/issues>`__ , and 
ask questions from `GitHub Discussions <https://github.com/toda-lab/pystc/discussions>`__ .

License
=======

Please see `LICENSE <https://github.com/toda-lab/pystc/blob/main/LICENSE>`__ .


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/toda-lab/pystc",
    "name": "pystc",
    "maintainer": null,
    "docs_url": null,
    "requires_python": "<4.0,>=3.8",
    "maintainer_email": null,
    "keywords": "logic, artificial intelligence",
    "author": "Takahisa Toda",
    "author_email": "toda@disc.lab.uec.ac.jp",
    "download_url": "https://files.pythonhosted.org/packages/21/db/940b6e86e9d3c6f7de1355a19a834e4d1705711ddbbcca95f3e45a914e12/pystc-2.0.0.tar.gz",
    "platform": null,
    "description": "pystc: A simple but extensible Python module for sentences\n==========================================================\n\nIntroduction\n============\nA *sentence* is an atomic sentence or a (compound) sentence.\nAn *atomic sentence* consists of a *predicate* and *constants* the predicate has\nas its arguments.\nThere is no variable, no function.\nThe number of arguments of a predicate is called the *arity* of the predicate.\nA compound sentence can be obtained by joining sentences with *connectives*.\n\n``pystc`` is a simple but extensible Python module for sentences.\nIt provides functionality to define sentences as you like \nby adding constants, predicates, and connectives, whether logical or non-logical.\nConstants, predicates, and connectives can be introduced by specifying\ntheir names and how they are associated with other well-defined objects or\nfunctions.\n\nInstallation\n============\n\n.. code:: shell-session\n\n    $ pip install pystc\n\nUsage\n=====\n\nLet us first create atomic sentences that can be built from a predicate ``=``\nand constants ``T``, ``F``, \n\n.. code:: python\n\n    from pystc import AtomicSentence\n\n    # Let us add predicate \"=\" so that the arity for it is 2.\n    AtomicSentence.add_predicate(\"=\",2) \n    AtomicSentence.add_constant(\"T\")\n    AtomicSentence.add_constant(\"F\")\n\n    # Now, sentences are ready to create.\n    s1 = AtomicSentence(\"=\",\"T\",\"T\")\n    s2 = AtomicSentence(\"=\",\"T\",\"F\")\n    s3 = AtomicSentence(\"=\",\"F\",\"T\")\n\n    assert str(s2) == \"=(T,F)\"\n    assert AtomicSentence.read(\"=(T,F)\") == s2\n\nLet us next construct compound sentences.\nA ``Sentence`` in ``pystc`` module is simply a recursive type defined to be:\n\n.. code:: python\n\n    Sentence = Union[str, AtomicSentence, Tuple[\"Sentence\"]]\n\nAlthough it is loosely defined for simplicity, \nsentences are implicitly expected to fall into one of the following cases:\n\n1. An AtomicSentence object, say ``s2``.\n1. The string representation of an AtomicSentence object, say ``\"=(T,F)\"``.\n1. A tuple such that the initial entry is a connective name and the other entries are Sentence objects, say ``(\"&\", s2, (\"!\", s2))``.\n\nAs the connective names ``\"&\"`` and ``\"!\"`` appears just above, \nlet us introduce these connectives in the following codeblock\nand inteprete sentences.\n\n.. code:: python\n\n    from pystc import SentenceConverter\n\n    # Let us set an atomic sentence type and how each symbol is interpreted.\n    SentenceConverter.set_atom_type(AtomicSentence)\n    SentenceConverter.set_constant_destination(\"T\", True)\n    SentenceConverter.set_constant_destination(\"F\", False)\n    SentenceConverter.set_predicate_destination(\"=\", lambda li,w: li[0]==li[1])\n    SentenceConverter.set_connective_destination(\"&\",lambda li,w: not False in li)\n    SentenceConverter.set_connective_destination(\"|\",lambda li,w: True in li)\n    SentenceConverter.set_connective_destination(\"!\",lambda li,w: not li[0])\n\n    assert SentenceConverter.convert(s2) == False\n    assert SentenceConverter.convert(\"=(T,F)\") == False\n    assert SentenceConverter.convert((\"&\", s2, (\"!\", s2))) == False\n    assert SentenceConverter.convert((\"&\", \"=(T,F)\", (\"!\", s2))) == False\n\nFor another example of usage, let us convert sentences into strings in infix\nnotation.\n\n.. code:: python\n\n    # Clear all class variables\n    SentenceConverter.clear()\n\n    SentenceConverter.set_atom_type(AtomicSentence)\n    SentenceConverter.set_constant_destination(\"T\", \"T\")\n    SentenceConverter.set_constant_destination(\"F\", \"F\")\n    SentenceConverter.set_predicate_destination(\"=\", lambda li,w: f\"{li[0]}={li[1]}\")\n    SentenceConverter.set_connective_destination(\"&\",lambda li,w: \"(\"+\" & \".join(li)+\")\")\n    SentenceConverter.set_connective_destination(\"|\",lambda li,w: \"(\"+\" | \".join(li)+\")\")\n    SentenceConverter.set_connective_destination(\"!\",lambda li,w: \"!\"+li[0])\n\n    assert SentenceConverter.convert(\"=(T,F)\") == \"T=F\"\n    assert SentenceConverter.convert((\"&\", s2, (\"!\", s2))) == \"(T=F & !T=F)\"\n\nLet us not forget to clear class variables after everything is finished.\n\n.. code:: python\n\n    SentenceConverter.clear()\n    AtomicSentence.clear()\n\n\nBugs/Requests/Discussions\n=========================\n\nPlease report bugs and requests from `GitHub Issues <https://github.com/toda-lab/pystc/issues>`__ , and \nask questions from `GitHub Discussions <https://github.com/toda-lab/pystc/discussions>`__ .\n\nLicense\n=======\n\nPlease see `LICENSE <https://github.com/toda-lab/pystc/blob/main/LICENSE>`__ .\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "A simple but extensible Python module for sentences",
    "version": "2.0.0",
    "project_urls": {
        "Homepage": "https://github.com/toda-lab/pystc",
        "Repository": "https://github.com/toda-lab/pystc"
    },
    "split_keywords": [
        "logic",
        " artificial intelligence"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "7f5cc98b04feb4c7dd749e7fa3804cf95d06fc9141d889b2f25b8b502a48ebb0",
                "md5": "d6374091a426e8e5dd5d5fe9a950bcb0",
                "sha256": "dec1011da7299b17b162ead59fa096e80f9c361fddcf99b150c943f9f135a783"
            },
            "downloads": -1,
            "filename": "pystc-2.0.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "d6374091a426e8e5dd5d5fe9a950bcb0",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": "<4.0,>=3.8",
            "size": 6489,
            "upload_time": "2024-03-28T15:24:14",
            "upload_time_iso_8601": "2024-03-28T15:24:14.484332Z",
            "url": "https://files.pythonhosted.org/packages/7f/5c/c98b04feb4c7dd749e7fa3804cf95d06fc9141d889b2f25b8b502a48ebb0/pystc-2.0.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "21db940b6e86e9d3c6f7de1355a19a834e4d1705711ddbbcca95f3e45a914e12",
                "md5": "3f32f1ad3e7bf1b5e3e5c7a3189dcfbc",
                "sha256": "f8303eba5b2058ee666c1c65cacf124c0c441c209d1ebcd3db66361f4cc0eb3e"
            },
            "downloads": -1,
            "filename": "pystc-2.0.0.tar.gz",
            "has_sig": false,
            "md5_digest": "3f32f1ad3e7bf1b5e3e5c7a3189dcfbc",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": "<4.0,>=3.8",
            "size": 5932,
            "upload_time": "2024-03-28T15:24:16",
            "upload_time_iso_8601": "2024-03-28T15:24:16.429642Z",
            "url": "https://files.pythonhosted.org/packages/21/db/940b6e86e9d3c6f7de1355a19a834e4d1705711ddbbcca95f3e45a914e12/pystc-2.0.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-03-28 15:24:16",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "toda-lab",
    "github_project": "pystc",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "tox": true,
    "lcname": "pystc"
}
        
Elapsed time: 0.55921s