Skip to content

Commit

Permalink
update narsese sub-module
Browse files Browse the repository at this point in the history
  • Loading branch information
bowen-xu committed Jan 31, 2024
1 parent 41a1e70 commit 925ee0e
Show file tree
Hide file tree
Showing 28 changed files with 5,591 additions and 3 deletions.
160 changes: 160 additions & 0 deletions pynars/Narsese/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/#use-with-ide
.pdm.toml

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
21 changes: 21 additions & 0 deletions pynars/Narsese/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Bowen Xu

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
Empty file.
47 changes: 47 additions & 0 deletions pynars/Narsese/Parser/_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from .parser import parser
import re
re_parser = re.compile(r'[^<^>^=^"^&^|^!^.^?^@^~^%^;^\,^:^\/^\\^*^#^$^\[^\]^\{^\}^\(^\)^\ ]+')
re_parser.findall('<x-->lock>.')
# result = parser.parse('(&&, <#x --> lock>, <"a"-->"b">).')
result = parser.parse('<x-->lock>.')
print(result.pretty())

result = parser.parse('<"鸟" --> "动-物">. %0.9; 0.9%')
print(result.pretty())
result = parser.parse('<gull <-> swan>. %0.9; 0.9%')
print(result.pretty())
result = parser.parse('<swan --> (&,bird,swimmer)>. %0.9; 0.9%')
print(result.pretty())
result = parser.parse('<planetX --> {Mars,Pluto,Venus}>. %0.9; 0.9%')
print(result.pretty())
result = parser.parse('<(&,bird,swimmer) --> (&,animal,swimmer)>?')
print(result.pretty())
result = parser.parse('<(~, boy, girl) --> [strong]>.')
print(result.pretty())
result = parser.parse('<acid --> (/,reaction,_,base)>.')
print(result.pretty())
result = parser.parse('<(*,acid,base) --> reaction>.')
print(result.pretty())
result = parser.parse('<<$y --> bird> ==> <$y --> flyer>>.')
print(result.pretty())
result = parser.parse('<(&&,<$1 --> [chirping]>,<$1 --> [with_wings]>) ==> <$1 --> bird>>.')
print(result.pretty())
result = parser.parse('<<#x --> lock> ==> <<$y --> key> ==> <#x --> (/,open,$y,_)>>>.')
result = parser.parse('(&&,<#x --> lock>,<<$y --> key> ==> <#x --> (/,open,$y,_)>>).')
result = parser.parse('(&&,<?x --> lock>,<<$y --> key> ==> <#x --> (/,open,$y,_)>>)?')
print(result.pretty())
result = parser.parse('<<(*, $y, door_101) --> open> =\> <(*, $y, key_101) --> hold>>.')
print(result.pretty())
result = parser.parse('<(*,John,room_101) --> enter>. :\: %1.00;0.90%')
print(result.pretty())
result = parser.parse('<<(*,John,door_101) --> open></><(*,John,room_101) --> enter>>.')
print(result.pretty())
result = parser.parse('(&/,<(*,SELF,{t002}) --> hold>,<(*,SELF,{t001}) --> at>,(^open,{t001}))!')
print(result.pretty())
result = parser.parse('<SELF --> (/,at,_,{t003})>. :\:')
print(result.pretty())
result = parser.parse('(&/,<(*,SELF,{t002}) --> reachable>,(^pick,{t002}))!')
print(result.pretty())
result = parser.parse('(^doubt,{SELF},<a --> b>)!')
print(result.pretty())
print('done.')
15 changes: 15 additions & 0 deletions pynars/Narsese/Parser/_test_earley.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from lark import Lark
from pynars.Narsese.Parser.parser import TreeToNarsese
from pathlib import Path

filepath = Path(r'Narsese\Parser\narsese.lark')
with open(filepath, 'r') as f:
gramma = ''.join(f.readlines())
lark = Lark(grammar=gramma, parser='lalr')
content = lark.parse(r'$0.90;0.90;0.9$ <robin-->bird>.')

lark = Lark(grammar=gramma, parser='earley')
content = lark.parse(r'$0.90;0.90;0.9$ <robin-->bird>.')


print('done.')
139 changes: 139 additions & 0 deletions pynars/Narsese/Parser/narsese.lark
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
?start: task

task : [budget] sentence // (* task to be processed *)
?sentence.0 : (term_nonvar|statement) "." [tense] [truth] -> judgement // (* judgement to be absorbed into beliefs *)
| (term_nonvar|statement) "?" [tense] -> question // (* question on truth-value to be answered *)
| (term_nonvar|statement) "!" [tense] [desire] -> goal // (* goal to be realized by operations *)
| (term_nonvar|statement) "@" [tense] -> quest // (* question on desire-value to be answered *)

?statement.0 : "<" term copula term ">" // (* two terms related to each other *)
| "(" term copula term ")" // (* two terms related to each other, new notation *)
// | term // (* a term can name a statement *)
| "(" op ("," term)* ")" -> statement_operation1 // (* an operation to be executed *)
| word "(" term ("," term)* ")" -> statement_operation2 // (* an operation to be executed, new notation *)
?copula : "-->" -> inheritance // (* inheritance *)
| "<->" -> similarity // (* similarity *)
| "{--" -> instance // (* instance *)
| "--]" -> property // (* property *)
| "{-]" -> instance_property // (* instance-property *)
| "==>" -> implication // (* implication *)
| "=/>" -> predictive_implication // (* predictive implication *)
| "=|>" -> concurrent_implication // (* concurrent implication *)
| "=\>" -> retrospective_implication // (* retrospective implication *)
| "<=>" -> equivalence // (* equivalence *)
| "</>" -> predictive_equivalence // (* predictive equivalence *)
| "<|>" -> concurrent_equivalence // (* concurrent equivalence *)

?term : variable -> variable_term // (* an atomic variable term *)
| term_nonvar

?term_nonvar: interval
| word -> atom_term // (* an atomic constant term *)
| compound_term -> compound_term // (* a term with internal structure *)
| statement -> statement_term // (* a statement can serve as a term *)
| op



op : "^" word
interval: "+" NUMBER

?compound_term : set
| multi // (* with prefix or with infix operator *)
| single // (* with prefix or with infix operator *)
| ext_image // (* special case, extensional image *)
| int_image // (* special case, \ intensional image *)
| negation // (* negation *)

?set : int_set
| ext_set
// | list_set
?int_set : con_int_set term ("," term)* "]" -> set // (* intensional set *)
?ext_set : con_ext_set term ("," term)* "}" -> set // (* extensional set *)
// list_set: "(" "#" "," term ("," term)+ ")"

negation : con_negation term // (* negation *)
| "(" con_negation "," term ")" // (* negation, new notation *)
int_image : "(" con_int_image "," term ("," term)* ")" // (* intensional image *)
ext_image : "(" con_ext_image "," term ("," term)* ")" // (* extensional image *)
?multi : "(" con_multi "," term ("," term)+ ")" -> multi_prefix // (* with prefix operator *)
| "(" multi_infix_expr ")" // (* with infix operator *)
| "(" term ("," term)+ ")" -> multi_prefix_product// (* product, new notation *)
| "(" con_product "," term ("," term)* ")" -> multi_prefix // (* with prefix operator *)

?single : "(" con_single "," (term|multi_infix_expr) "," (term|multi_infix_expr) ")" -> single_prefix // (* with prefix operator *)
| "(" (term|multi_infix_expr) con_single (term|multi_infix_expr) ")" -> single_infix // (* with infix operator *)

?multi_infix_expr : multi_extint_expr
| multi_intint_expr
| multi_parallel_expr
| multi_sequential_expr
| multi_conj_expr
| multi_disj_expr
| multi_prod_expr

// precedence:
// "&" > "|" > "&|" > "&/" > "&&" > "||" > "*"
?multi_prod_expr : term6 ("*" term6)+
?term6 : (term5|multi_disj_expr)
?multi_disj_expr: term5 ("||" term5)+
?term5 : (term4|multi_conj_expr)
?multi_conj_expr: term4 ("&&" term4)+
?term4 : (term3|multi_sequential_expr)
?multi_sequential_expr: term3 ("&/" term3)+
?term3 : (term2|multi_parallel_expr)
?multi_parallel_expr: term2 ("&|" term2)+
?term2 : (term1|multi_intint_expr)
?multi_intint_expr : term1 ("|" term1)+
?term1 : (term|multi_extint_expr)
?multi_extint_expr : term ("&" term)+



?con_multi : "&&" -> con_conjunction // (* conjunction *)
| "||" -> con_disjunction // (* disjunction *)
| "&|" -> con_parallel_events // (* parallel events *)
| "&/" -> con_sequential_events // (* sequential events *)
| "|" -> con_intensional_intersection // (* intensional intersection *)
| "&" -> con_extensional_intersection // (* extensional intersection *)
con_product: "*" // (* product *)


?con_single : "-" -> con_extensional_difference // (* extensional difference *)
| "~" -> con_intensional_difference // (* intensional difference *)
?con_int_set: "[" // (* intensional set *)
?con_ext_set: "{" // (* extensional set *)

?con_negation : "--" // (* negation *)

?con_int_image : "\\" // (* intensional image *)
?con_ext_image : "/" // (* extensional image *)

?variable.0 : "$" word -> independent_var // (* independent variable *)
| "#" word -> dependent_var // (* dependent variable *)
| "?" word -> query_var // (* query variable in question *)

?tense : ":!" NUMBER ":" -> tense_time
| ":/:" -> tense_future // (* future event *)
| ":|:" -> tense_present // (* present event *)
| ":\:" -> tense_past // (* past event *)

?desire : truth // (* same format, different interpretations *)
truth : "%" frequency [";" confidence [";" k_evidence]] "%" // (* two numbers in [0,1]x(0,1) *)
budget.2: "$" priority [";" durability [";" quality]] "$" // (* three numbers in [0,1]x(0,1)x[0,1] *)

?word : string_raw | string // /[^\ ]+/ //(* unicode string *)
?priority : /([0]?\.[0-9]+|1\.[0]*|1|0)/ //(* 0 <= x <= 1 *)
?durability : /[0]?\.[0]*[1-9]{1}[0-9]*/ // (* 0 < x < 1 *)
?quality : /([0]?\.[0-9]+|1\.[0]*|1|0)/ // (* 0 <= x <= 1 *)
?frequency : /([0]?\.[0-9]+|1\.[0]*|1|0)/ // (* 0 <= x <= 1 *)
?confidence : /[0]?\.[0]*[1-9]{1}[0-9]*/ // (* 0 < x < 1 *)
?k_evidence: /[1-9]{1}[0-9]*/ // (* x > 0 *)

?string: /"[^"]+"/
?string_raw: /[^\-^\+^<^>^=^"^&^|^!^.^?^@^~^%^;^\,^:^\/^\\^*^#^$^\[^\]^\{^\}^\(^\)^\ ]+/

%import common.WS
%import common.SIGNED_INT -> NUMBER
// %import common.INT -> NATURAL_NUMBER
%ignore WS
Loading

0 comments on commit 925ee0e

Please sign in to comment.