Skip to content

Commit

Permalink
style: 🎨 Align the code style into "lower camel case" (for names of v…
Browse files Browse the repository at this point in the history
…ariable and function)
  • Loading branch information
ARCJ137442 committed Sep 24, 2023
1 parent 91c489a commit d6b9c6d
Show file tree
Hide file tree
Showing 5 changed files with 560 additions and 564 deletions.
2 changes: 1 addition & 1 deletion .vscode/launch.json
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@
]
},
{
"name": "Python: Console Experimental Features",
"name": "Python: ex-console for experiments",
"type": "python",
"request": "launch",
"module": "Experiments.ExConsole.main",
Expand Down
144 changes: 72 additions & 72 deletions Experiments/ExConsole/data2nal.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,14 @@ def SIGN_TYPE_NAMES(type): return type.__name__
# Preset identification


def isBasicType(obj: any) -> bool:
def is_basic_type(obj: any) -> bool:
'''Determining whether a type is an "immutable type"
Note: Strings are counted'''
t = type(obj)
return obj == None or t == bool or t == int or t == float or t == str


def verifyTermName(name: str) -> str | None:
def verify_term_name(name: str) -> str | None:
try:
return (
name
Expand All @@ -43,169 +43,169 @@ def verifyTermName(name: str) -> str | None:
# term construction


def termRel(A: str, B: str) -> str:
def term_rel(A: str, B: str) -> str:
'''Construct the term "Relationship between A and B"'''
return f'(*,{A},{B})'


def termName(A: any) -> str:
def term_name(A: any) -> str:
'''Build term "object name"'''
t: type = type(A)
# test whether the object can be directly accepted as a lexical item, if it is attached, if not, no
instanceWrapper: str = '%s' if isBasicType(A) else '{%s}'
instance_wrapper: str = '%s' if is_basic_type(A) else '{%s}'
# first format encapsulation: Fill "class name _ value /id"
initName: str = verifyTermName(f'{t.__name__}_{str(A)}')
init_name: str = verify_term_name(f'{t.__name__}_{str(A)}')
# check: Whether the filled term is valid (cause: the array "first pass, second fail" problem)
initName = verifyTermName(
initName if initName else f'{t.__name__}_{id(A)}')
init_name = verify_term_name(
init_name if init_name else f'{t.__name__}_{id(A)}')
# second format encapsulation: Specifies whether an instance term is required
finalName: str = instanceWrapper % initName
final_name: str = instance_wrapper % init_name
# return
return finalName
return final_name

# sentence construction


def sentenceInh(A: str, B: str, punct: Punctuation | str = Punctuation.Judgement) -> str:
def sentence_inh(A: str, B: str, punct: Punctuation | str = Punctuation.Judgement) -> str:
'''Building relationships "A is B"'''
return f'<{A} --> {B}>{punct.value if isinstance(punct,Punctuation) else punct}' # <A --> B> default with "."


def sentenceRel(A: str, r: str, B: str, punct: Punctuation = Punctuation.Judgement) -> str:
def sentence_rel(A: str, r: str, B: str, punct: Punctuation = Punctuation.Judgement) -> str:
'''Construct relation "ArB" i.e. "The relation between A and B" is r"'''
return sentenceInh(termRel(A, B), f'{r}', punct=punct) # <(*,A,B) --> r>. The "relationship" between A and B is r
return sentence_inh(term_rel(A, B), f'{r}', punct=punct) # <(*,A,B) --> r>. The "relationship" between A and B is r


def sentenceTypeSign(name: str, type: type, punct: Punctuation = Punctuation.Judgement) -> str:
def sentence_type_sign(name: str, type: type, punct: Punctuation = Punctuation.Judgement) -> str:
'''Name the name based on the type of the object'''
return sentenceInh(name, SIGN_TYPE_NAMES(type), punct=punct) # name --> type
return sentence_inh(name, SIGN_TYPE_NAMES(type), punct=punct) # name --> type


def sentenceTriRel(obj: str, key: str, value: str, relation: str = SIGN_RELATION_OBJ_PROPERTY_VALUE,
punct: Punctuation = Punctuation.Judgement) -> str:
def sentence_tri_rel(obj: str, key: str, value: str, relation: str = SIGN_RELATION_OBJ_PROPERTY_VALUE,
punct: Punctuation = Punctuation.Judgement) -> str:
'''Building a ternary relationship "Object [key] = value"
Example: (*,{object},(*,{attribute},{value})) --> objectAttributeValue
Example: (*,{object},(*,{attribute},{value})) --> object_attribute_value
'''
return sentenceRel(obj, relation, termRel(key, value), punct=punct) # (*,{obj},(*,{key},{value})) --> relation
return sentence_rel(obj, relation, term_rel(key, value), punct=punct) # (*,{obj},(*,{key},{value})) --> relation

# Main conversion module #

# basic


# Use kwargs to automatically pass "punctuation" information
def none2NAL(n: None, specialName: str, **kwargs) -> list[str]:
def none2NAL(n: None, special_name: str, **kwargs) -> list[str]:
'''None to NAL'''
return [sentenceTypeSign(specialName, type(n), **kwargs)]
return [sentence_type_sign(special_name, type(n), **kwargs)]


def bool2NAL(b: str, specialName: str, **kwargs) -> list[str]:
def bool2NAL(b: str, special_name: str, **kwargs) -> list[str]:
'''boolean to NAL'''
result: list[str] = [sentenceTypeSign(
specialName, bool, **kwargs)] # preset type identification
result: list[str] = [sentence_type_sign(
special_name, bool, **kwargs)] # preset type identification
return result


def int2NAL(i: int, specialName: str, **kwargs) -> list[str]:
def int2NAL(i: int, special_name: str, **kwargs) -> list[str]:
'''Integer to NAL
TODO: Build an integer system'''
result: list[str] = [sentenceTypeSign(
specialName, int, **kwargs)] # preset type identification
result: list[str] = [sentence_type_sign(
special_name, int, **kwargs)] # preset type identification
return result


def float2NAL(f: float, specialName: str, **kwargs) -> list[str]:
def float2NAL(f: float, special_name: str, **kwargs) -> list[str]:
'''Floating-point number becomes NAL
TODO: Build a floating point number system and combine it with an integer system'''
result: list[str] = [sentenceTypeSign(
specialName, float, **kwargs)] # preset type identification
result: list[str] = [sentence_type_sign(
special_name, float, **kwargs)] # preset type identification
return result


def str2NAL(s: str, specialName: str, **kwargs) -> list[str]:
def str2NAL(s: str, special_name: str, **kwargs) -> list[str]:
'''Set to NAL
TODO: How does distinguish between "name" and "value" of a string?'''
# tests whether a string can be directly accepted as a term, appended if it is, unappended if it is not
# Try to use the string itself as a name for easy identification
finalName = verifyTermName(s)
finalName: str = specialName + \
(f'「{s}」' if finalName else '') # Include itself if you can, do not add information otherwise
final_name = verify_term_name(s)
final_name: str = special_name + \
(f'「{s}」' if final_name else '') # Include itself if you can, do not add information otherwise
# preset type identification
result: list[str] = [sentenceTypeSign(finalName, str, **kwargs)]
result: list[str] = [sentence_type_sign(final_name, str, **kwargs)]
return result

# containers


def set2NAL(s: set, specialName: str, **kwargs) -> list[str]:
def set2NAL(s: set, special_name: str, **kwargs) -> list[str]:
'''Set to NAL
Use the relative item "belong"
Import a collection name as the concept name, and then convert all elements within it to NAL
Return: A list of multiple NAL statements (easy to split)'''
result: list[str] = [sentenceTypeSign(
specialName, set, **kwargs)] # preset type identification
result: list[str] = [sentence_type_sign(
special_name, set, **kwargs)] # preset type identification
for item in s: # TODO: Implementing recursive logic @auto2NAL(item)
result.append(sentenceRel(termName(item),
SIGN_RELATION_BELONG, specialName, **kwargs))
result.append(sentence_rel(term_name(item),
SIGN_RELATION_BELONG, special_name, **kwargs))
return result


def lisTuple2NAL(array: list | tuple, specialName: str, **kwargs) -> list[str]:
def lis_tuple2NAL(array: list | tuple, special_name: str, **kwargs) -> List[str]:
'''List/tuple to NAL: Lists whose keys are integers'''
result: list[str] = [sentenceTypeSign(
specialName, type(array), **kwargs)] # preset type identification
result: list[str] = [sentence_type_sign(
special_name, type(array), **kwargs)] # preset type identification
for i in range(len(array)):
# get element
item: any = array[i]
iName: str = termName(i)
itemName: str = termName(item)
iName: str = term_name(i)
item_name: str = term_name(item)
# pre-add definitions for keys and values
result.extend(auto2NAL(item, itemName, **kwargs))
result.extend(auto2NAL(item, item_name, **kwargs))
result.extend(auto2NAL(i, iName, **kwargs))
# bind objects, keys, and values
result.append(sentenceTriRel(
specialName,
result.append(sentence_tri_rel(
special_name,
iName,
itemName,
**kwargs)) # list[integerIndex] = value
item_name,
**kwargs)) # list[integer_index] = value
return result


def dict2NAL(d: dict, specialName: str, **kwargs) -> list[str]:
def dict2NAL(d: dict, special_name: str, **kwargs) -> list[str]:
'''dict to NAL
#! In fact, JSON is a dictionary, to achieve the dictionary↔NAL conversion, which is equivalent to NARS can read JSON
TODO models dictionaries using an index system'''
result: list[str] = [sentenceTypeSign(
specialName, dict, **kwargs)] # preset type identification
result: list[str] = [sentence_type_sign(
special_name, dict, **kwargs)] # preset type identification
for key, value in d.items():
keyName: str = termName(key)
valueName: str = termName(value)
key_name: str = term_name(key)
value_name: str = term_name(value)
# pre-add definitions for keys and values
result.extend(auto2NAL(key, keyName, **kwargs))
result.extend(auto2NAL(value, valueName, **kwargs))
result.extend(auto2NAL(key, key_name, **kwargs))
result.extend(auto2NAL(value, value_name, **kwargs))
# bind objects, keys, and values
result.append(sentenceTriRel(
specialName,
keyName,
valueName,
**kwargs)) # dictionary[immutableDataIndex] = value
result.append(sentence_tri_rel(
special_name,
key_name,
value_name,
**kwargs)) # dictionary[immutable_data_index] = value
return result


def type2NAL(t: type, specialName: str, **kwargs) -> list[str]:
def type2NAL(t: type, special_name: str, **kwargs) -> list[str]:
'''Type to NAL
The "type" itself also needs to become NAL'''
return [sentenceTypeSign(specialName, type, **kwargs)] # only type notations
return [sentence_type_sign(special_name, type, **kwargs)] # only type notations

# default values


def default2NAL(obj: any, specialName: str, **kwargs) -> list[str]:
def default2NAL(obj: any, special_name: str, **kwargs) -> list[str]:
'''Other objects become NAL
Temporarily occupy the one place, will later be separated from many types'''
print(f'WARNING: unsupported object {obj} with type {type(obj)}')
# only type notations
return [sentenceTypeSign(specialName, type, **kwargs)]
return [sentence_type_sign(special_name, type, **kwargs)]

# Main function: Integrate all parts #

Expand All @@ -216,8 +216,8 @@ def default2NAL(obj: any, specialName: str, **kwargs) -> list[str]:
float: float2NAL,
bool: bool2NAL,
str: str2NAL,
list: lisTuple2NAL,
tuple: lisTuple2NAL,
list: lis_tuple2NAL,
tuple: lis_tuple2NAL,
set: set2NAL,
dict: dict2NAL,
type: type2NAL,
Expand All @@ -230,14 +230,14 @@ def auto2NAL(obj: any, name: str = None, punct: Punctuation = Punctuation.Judgem
Automatically identify the object type and call the corresponding conversion function'''
# get name
# If no, automatically generate
name = name if name else termName(obj)
name = name if name else term_name(obj)
# get type
t = type(obj)
# select & Generate, with default values (all parameters must accept a "Narsese parsed name")
narsSentences: list[str] = CONVERT_FUNCTIONS.get(
nars_sentences: list[str] = CONVERT_FUNCTIONS.get(
t, CONVERT_FUNCTIONS[None])(obj, name, punct=punct)
# verify
for sentence in narsSentences:
for sentence in nars_sentences:
parser.parse(sentence)
# return
return narsSentences
return nars_sentences
Loading

0 comments on commit d6b9c6d

Please sign in to comment.