Git Product home page Git Product logo

cognitum-octopus / cognipy Goto Github PK

View Code? Open in Web Editor NEW
46.0 10.0 10.0 136.08 MB

In-memory Graph Database and Knowledge Graph with Natural Language Interface, compatible with Pandas

Home Page: https://cognitum-octopus.github.io/cognipy/

License: Other

C# 36.03% Batchfile 0.04% Python 0.56% JavaScript 0.12% Prolog 35.54% Smarty 0.15% Makefile 0.01% CSS 0.01% Jupyter Notebook 27.42% Smalltalk 0.14%
owl jena rl-reasoner rdf graph-algorithms pandas python elk controlled-natural-language natural-language-interface

cognipy's Introduction

CogniPy

CogniPy for Pandas - In-memory Graph Database and Knowledge Graph with Natural Language Interface

Whats in the box

Reasoning, exploration of RDF/OWL, FluentEditor CNL files, with OWL/RL Reasoner (Jena) as well as SPARQL Graph queries (Jena) and visualization.

What you can do with this:

  1. Write your graph/ontology in Controlled Natural Language or import it from RDF/OWL
  2. Add reasoning rules/T-Box in Controlled Natural Language
  3. Import data using Pandas or scrap them from the Internet
  4. Draw the resulting, materialized graph
  5. Use SPARQL to execute graph query
  6. Use output Dataframe for further processing with Pandas

Getting started

Installation

Prerequisites:

  • If you are on Mac or Linux You MUST have mono installed on your system.
  • Graph drawing based on pydot that is dependent on GraphViz - you should try to download and install it manually. Or just conda install pydot graphviz
  • Tested with Anaconda
  • Tested on MacOS, Winows and Linux (Ubuntu)

Install cognipy on your system using :

pip install cognipy

Hello world program

In Jupyter you write:

from cognipy.ontology import Ontology #the ontology processing class
%%writefile hello.encnl
World says Hello.
Hello is a word.
onto = Ontology("cnl/file","hello.encnl")
print(onto.select_instances_of("a thing that says a word")[["says","Instance"]])

Output (Pandas DataFrame):

says Instance
0 Hello World

Examples

Example Jupyter notebooks that use CogniPy in several scenarios can be found in the Examples section

Cognipy documentation

Compiled documentation is stored on github pages here: Cognipy Documentation

Related research papers

  1. Semantic rules representation in controlled natural language in FluentEditor
  2. Collaborative Editing of Ontologies Using Fluent Editor and Ontorion
  3. Semantic OLAP with FluentEditor and Ontorion Semantic Excel Toolchain
  4. Ontology-aided software engineering
  5. Ontology of the Design Pattern Language for Smart Cities Systems

How to cite CogniPy

We would be grateful if scientific publications resulting from projects that make use of CogniPy would include the following sentence in the acknowledgments section: "This work was conducted using the CogniPy package, which is an open-source project maintained by Cognitum Services S.A. https://www.cognitum.eu"

Cognitum

Contributors

Open Source Libraries this project is build on

  1. IKVM
  2. CommandLineParser
  3. Newtonsoft.JSon
  4. ELK - ELK is an ontology reasoner that aims to support the OWL 2 EL profile. See http://elk.semanticweb.org/ for further information.
  5. HermiT - HermiT is a conformant OWL 2 DL reasoner that uses the direct semantics. It supports all OWL2 DL constructs and the datatypes required by the OWL 2 specification.
  6. Apache Jena - Jena is a Java framework for building semantic web applications. It provides tools and Java libraries to help you to develop semantic web and linked-data apps, tools and servers.
  7. OWLAPI

Building new version

nuget restore cognipy\CogniPy.sln
msbuild cognipy\CogniPy.sln /t:Rebuild /p:Configuration=Release /p:Platform="any cpu"
python setup.py bdist_wheel
python -m twine upload dist/* --verbose

FAQ

Why it is done this way?

The software emerged as an offspring of FluentEditor and therefore it has some common parts. One of them is the .net. We are planning to move these parts to java so whole stack will be more technology consistent. The convert_to_java branch already contains the project files converted automatically from .net to java. Anyway, manual crafting is now required to make it all work.

cognipy's People

Contributors

jswiatkowski avatar kaplan196883 avatar kcieslinski avatar mch90 avatar sonofsatoshi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

cognipy's Issues

Clean up CogniPySvr class

Analyze code in CogniPySvr class in terms of unused/untested methods since it's most exposed class to a user

FluentEditor is using encoding='utf-8-sig'

Fluent Editor is using 'utf-8-sig' encoding and it needs to be used when opening these files e.g.:
'''onto=Ontology("cnl/string",'\n'.join(open(fn,"rt",encoding='utf-8-sig').read() for fn in ["data/sample-data.encnl"]))'''
Please update documentation and examples

ParseException for `K9s is something.`

Using

onto = Ontology("cnl/file","hello.encnl",stop_on_error=True)

Works with

%%writefile hello.encnl
Kas is something.

Errors with

%%writefile hello.encnl
K9s is something.
---------------------------------------------------------------------------
ParseException                            Traceback (most recent call last)
Input In [3], in <cell line: 1>()
----> 1 onto = Ontology("cnl/file","hello.encnl",stop_on_error=True)

File /opt/conda/lib/python3.10/site-packages/cognipy/ontology.py:103, in Ontology.__init__(self, source, arg, verbose, evaluator, graph_attribute_formatter, stop_on_error)
    101 self._uid = cognipy_create()
    102 if source == "cnl/file":
--> 103     cognipy_call(self._uid, "LoadCnl", arg, loadAnnotations,
    104                  modalCheck, passParamsAsCnl, stop_on_error)
    105 elif source == "cnl/string":
    106     cognipy_call(self._uid, "LoadCnlFromString", arg,
    107                  loadAnnotations, modalCheck, passParamsAsCnl, stop_on_error)

File /opt/conda/lib/python3.10/site-packages/cognipy/interop.py:76, in cognipy_call(uid, cmd, *args)
     74 js="\r\n".join(ja)
     75 if fl.strip()=='@exception':
---> 76     raise translate_exception(json.loads(js))
     77 return json.loads(js)

ParseException: {'Errors': [{'Line': 0, 'Column': 0, 'Pos': 0, 'Context': 'K9s is something.\n', 'Hint': 'K^9s is something.'}]}

(running in a jupyther/minimal image with mono added)

Misleading error message

Providing path to non-existing encnl file the error is:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "venv/lib/python3.6/site-packages/cognipy/ontology.py", line 104, in __init__
    modalCheck, passParamsAsCnl, stop_on_error)
  File "venv/lib/python3.6/site-packages/cognipy/interop.py", line 71, in cognipy_call
    raise translate_exception(json.loads(js))
Exception: [

  "IllegalArgumentException",

  {

    "ClassName": "java.lang.IllegalArgumentException",

    "Message": "URI is not absolute",

    "Data": null,

    "InnerException": null,

    "HelpURL": null,

    "StackTraceString": "  at CogniPy.CogniPySvr.Load (CogniPy.ReferenceManager+WhatToLoad whatToLoad, System.String contentToLoad, CogniPy.CNL.DL.Paragraph impliAst, System.Boolean throwOnException, System.Boolean loadAnns, System.Boolean materialize, System.Boolean modalChecker) [0x00057] in <f8783c48f78341ba843524fd57b41eb3>:0 \n  at CogniPy.CogniPySvr.LoadCnl (System.String filename, CogniPy.CNL.DL.Paragraph impliAst, System.Boolean throwOnException, System.Boolean loadAnns, System.Boolean materialize, System.Boolean modalChecker) [0x00000] in <f8783c48f78341ba843524fd57b41eb3>:0 \n  at CogniPy.CogniPySvr.LoadCnl (System.String filename, System.Boolean loadAnnotations, System.Boolean materialize, System.Boolean modalChecker, System.Boolean throwOnError) [0x00000] in <f8783c48f78341ba843524fd57b41eb3>:0 \n  at (wrapper managed-to-native) System.Reflection.MonoMethod:InternalInvoke (System.Reflection.MonoMethod,object,object[],System.Exception&)\n  at System.Reflection.MonoMethod.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x00038] in <8f2c484307284b51944a1a13a14c0266>:0 ",

    "RemoteStackTraceString": null,

    "RemoteStackIndex": 0,

    "ExceptionMethod": null,

    "HResult": -2146233088,

    "Source": "CogniPyLib",

    "$0": {

      "$0": {

        "type": "java.lang.IllegalArgumentException, IKVM.OpenJDK.Core, Version=8.1.5717.0, Culture=neutral, PublicKeyToken=13235d27fcbfff58",

        "sig": null

      },

      "$1": {

        "$0": {

          "type": "java.lang.RuntimeException, IKVM.OpenJDK.Core, Version=8.1.5717.0, Culture=neutral, PublicKeyToken=13235d27fcbfff58",

          "sig": null

        },

        "$1": {

          "$0": {

            "type": "java.lang.Exception, IKVM.OpenJDK.Core, Version=8.1.5717.0, Culture=neutral, PublicKeyToken=13235d27fcbfff58",

            "sig": null

          },

          "$1": {

            "$0": {

              "type": "java.lang.Throwable, IKVM.OpenJDK.Core, Version=8.1.5717.0, Culture=neutral, PublicKeyToken=13235d27fcbfff58",

              "sig": null

            },

            "$1": "Ljava/lang/Throwable;",

            "$2": "Ljava/lang/String;",

            "$3": "[Ljava/lang/StackTraceElement;",

            "$4": "Ljava/util/List;",

            "$5": null,

            "$data": "FAFyASgAE2phdmEubGFuZy5UaHJvd2FibGXVxjUnOXe4ywMABEwABWNhdXNlARBMAA1kZXRhaWxNZXNzYWdlAQ1bAApzdGFja1RyYWNlARdMABRzdXBwcmVzc2VkRXhjZXB0aW9ucwIA"

          },

          "$data": "FAFyASAAE2phdmEubGFuZy5FeGNlcHRpb27Q/R8+GjscxAIAAAEA"

        },

        "$data": "FAFyAScAGmphdmEubGFuZy5SdW50aW1lRXhjZXB0aW9unl8GRwo0g+UCAAABAA=="

      },

      "$data": "FAFyAS8AImphdmEubGFuZy5JbGxlZ2FsQXJndW1lbnRFeGNlcHRpb261iXPTfWaPvAIAAAEA"

    },

    "$2": "URI is not absolute",

    "$3": [],

    "$4": [],

    "$data": "CgUA"

  }

]

To reproduce (on Ubuntu):

onto = Ontology("cnl/file","nonexisting.encnl")

Problem with the disjointness assertion

It seems there's an error with the disjointness operator in CogniPy.

The following assertions result with an inconsistency detected in the ontology:

Anything either 
  is a second-name,
  is a first-name or
  is a last-name
  or-something-else.

First-Name-1 is a first-name.
First-Name-1 is a last-name.

While the following assertions does not:

Anything either 
  is a first-name or
  is a last-name
  or-something-else.

First-Name-1 is a first-name.
First-Name-1 is a last-name.

insert_cnl, delete_cnl methods output

image

Names of methods suggests that you can insert/delete any CNL yet if you insert/delete incorrect cnl like subsumption in attached pic error occures. Maybe output can be prettier?

Union in range of the role

BTW there's a similar issue with assertions on relations.

Both these work:

No first-name is pesel-num.
Every first-name connects-with nothing-but thing that is a pesel-num.
First-Name-6 is a first-name.
First-Name-7 is a first-name.
First-Name-6 connects-with First-Name-7.
Causes error.

No first-name is last-name.
Every first-name connects-with nothing-but thing that is a last-name.
First-Name-6 is a first-name.
First-Name-7 is a first-name.
First-Name-6 connects-with First-Name-7.
Causes error too.

But:

No first-name is last-name.
No first-name is pesel-num.
Every first-name connects-with nothing-but thing that is a last-name and-or pesel-num.
First-Name-6 is a first-name.
First-Name-7 is a first-name.
First-Name-6 connects-with First-Name-7.
Does not cause an error.

@apohllo

Author
apohllo commented 4 days ago
The RDF/OWL conversions are as follows, for the first type of assertion:

<rdfs:subClassOf>
  <owl:Restriction>
    <owl:onProperty rdf:resource="http://www.cognitum.eu/onto#connectsWith" />
    <owl:allValuesFrom rdf:resource="http://www.cognitum.eu/onto#lastName" />
  </owl:Restriction>
</rdfs:subClassOf>

and for the second type:

<rdfs:subClassOf>
  <owl:Restriction>
    <owl:onProperty rdf:resource="http://www.cognitum.eu/onto#connectsWith" />
    <owl:allValuesFrom>
      <owl:Class>
        <owl:unionOf rdf:parseType="Collection">
          <rdf:Description rdf:about="http://www.cognitum.eu/onto#lastName" />
          <rdf:Description rdf:about="http://www.cognitum.eu/onto#peselNum" />
        </owl:unionOf>
      </owl:Class>
    </owl:allValuesFrom>
  </owl:Restriction>
</rdfs:subClassOf>

Reasoner loading error under Ubuntu - BOM and CR/LF problem

I tried the examples in a cognipy installation under Ubuntu. At first it seemed that everything works as expected, but suddenly I started to receive the following error:

Traceback (most recent call last):
  File "check_consistency.py", line 28, in <module>
    printReasoningInfo(onto)
  File "check_consistency.py", line 15, in printReasoningInfo
    info=onto.reasoningInfo()
  File "/usr/local/lib/python3.7/dist-packages/cognipy/ontology.py", line 299, in reasoningInfo
    self.super_concepts_of("a thing")
  File "/usr/local/lib/python3.7/dist-packages/cognipy/ontology.py", line 179, in super_concepts_of
    return cognipy_call(self._uid, "GetSuperConceptsOf", cnl, direct)
  File "/usr/local/lib/python3.7/dist-packages/cognipy/interop.py", line 71, in cognipy_call
    raise translate_exception(json.loads(js))
Exception: [

  "InvalidOperationException",
  {
    "ClassName": "System.InvalidOperationException",
    "Message": "The reasoner is not initialized. You need to call Load... before calling this method.",
    "Data": null,
    "InnerException": null,

    "HelpURL": null,

    "StackTraceString": "  at CogniPy.CogniPySvr.get_reasoner () [0x00008] in <f8783c48f78341ba843524fd57b41eb3>:0 \n  at CogniPy.CogniPySvr.Materialize () [0x0001b] in <f8783c48f78341ba843524fd57b41eb3>:0 \n  at CogniPy.CogniPySvr.GetSuperConceptsOf (System.String cnlName, System.Boolean direct) [0x00000] in <f8783c48f78341ba843524fd57b41eb3>:0 \n  at (wrapper managed-to-native) System.Reflection.RuntimeMethodInfo.InternalInvoke(System.Reflection.RuntimeMethodInfo,object,object[],System.Exception&)\n  at System.Reflection.RuntimeMethodInfo.Invoke (System.Object obj, System.Reflection.BindingFlags invokeAttr, System.Reflection.Binder binder, System.Object[] parameters, System.Globalization.CultureInfo culture) [0x0006a] in <533173d24dae460899d2b10975534bb0>:0 ",
    "RemoteStackTraceString": null,
    "RemoteStackIndex": 0,
    "ExceptionMethod": null,
    "HResult": -2146233079,
    "Source": "CogniPyLib"
  }
]

My python version is 3.7.12 and mono version is 6.12.0.122.
I will submit a Dockerfile which will allow to reproduce the error.

Update:
example.tar.gz

serialization error

If X connects-to something that connects-to Y then Y follows X.
follows->followed

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.