Skip to content

Commit

Permalink
Accept goals for initial processing
Browse files Browse the repository at this point in the history
  • Loading branch information
ccrock4t committed May 7, 2021
1 parent 5da36d3 commit 966f40a
Show file tree
Hide file tree
Showing 8 changed files with 158 additions and 163 deletions.
2 changes: 1 addition & 1 deletion Config.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
"""
TRUTH_EPSILON = 0.01 # minimum size that truth-value can be incremented/decremented
MAX_EVIDENTIAL_BASE_LENGTH = 20000 # maximum IDs to store documenting evidential base
MAX_INTERACTED_SENTENCES_LENGTH = 20000

"""
Default Input Task Values
Expand All @@ -51,6 +50,7 @@
DEFAULT_QUESTION_PRIORITY = 0.9
DEFAULT_QUESTION_DURABILITY = 0.9

DEFAULT_GOAL_FREQUENCY = 1.0
DEFAULT_GOAL_CONFIDENCE = 0.9
DEFAULT_GOAL_PRIORITY = 0.9
DEFAULT_GOAL_DURABILITY = 0.9
Expand Down
39 changes: 16 additions & 23 deletions NALGrammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,6 @@ def new_sentence_from_string(cls, sentence_string: str):
punctuation = NALSyntax.Punctuation.get_punctuation(punctuation_str)
assert (punctuation is not None), punctuation_str + " is not punctuation."

# todo add support for goals
assert (
punctuation == NALSyntax.Punctuation.Judgment or punctuation == NALSyntax.Punctuation.Question), " Currently only accepting Judgments and Questions."


# Find statement copula, subject string, and predicate string
statement = Statement.from_string(sentence_string[start_idx:end_idx + 1])

Expand All @@ -80,20 +75,28 @@ def new_sentence_from_string(cls, sentence_string: str):
middle_truth_val_idx = sentence_string.find(NALSyntax.StatementSyntax.TruthValDivider.value)
end_truth_val_idx = sentence_string.rfind(NALSyntax.StatementSyntax.TruthValMarker.value)

no_truth_value_found = start_truth_val_idx == -1 or end_truth_val_idx == -1 or start_truth_val_idx == end_truth_val_idx
if no_truth_value_found:
# No truth value, use default truth value
truth_value = TruthValue(Config.DEFAULT_JUDGMENT_FREQUENCY, Config.DEFAULT_JUDGMENT_CONFIDENCE)
else:
truth_value_found = not (start_truth_val_idx == -1 or end_truth_val_idx == -1 or start_truth_val_idx == end_truth_val_idx)
freq = None
conf = None
if truth_value_found:
# Parse truth value from string
freq = float(sentence_string[start_truth_val_idx + 1:middle_truth_val_idx])
conf = float(sentence_string[middle_truth_val_idx + 1:end_truth_val_idx])
truth_value = TruthValue(freq, conf)

if punctuation == NALSyntax.Punctuation.Judgment:
sentence = Judgment(statement, truth_value)
if freq is None:
# No truth value, use default truth value
freq = Config.DEFAULT_JUDGMENT_FREQUENCY
conf = Config.DEFAULT_JUDGMENT_CONFIDENCE
sentence = Judgment(statement, TruthValue(freq, conf))
elif punctuation == NALSyntax.Punctuation.Question:
sentence = Question(statement)
elif punctuation == NALSyntax.Punctuation.Goal:
if freq is None:
# No truth value, use default truth value
freq = Config.DEFAULT_GOAL_FREQUENCY
conf = Config.DEFAULT_GOAL_CONFIDENCE
sentence = Goal(statement, DesireValue(freq,conf))
else:
assert False,"Error: No Punctuation!"

Expand Down Expand Up @@ -128,7 +131,6 @@ def may_interact(cls,j1,j2):
"""
if j1 is None or j2 is None: return False
if j1.stamp.id == j2.stamp.id: return False
if j1 in j2.stamp.interacted_sentences: return False # don't need to check the inverse, since they are added mutually
if j1 in j2.stamp.evidential_base or j2 in j1.stamp.evidential_base: return False
if j1.stamp.evidential_base.has_evidential_overlap(j2.stamp.evidential_base): return False
return True
Expand Down Expand Up @@ -285,7 +287,7 @@ def __init__(self, self_sentence, occurrence_time=None):
self.occurrence_time = occurrence_time
self.sentence = self_sentence
self.evidential_base = EvidentialBase(self_sentence=self_sentence)
self.interacted_sentences = [] # list of sentence this sentence has already interacted with
self.from_conversion = False # is this sentence derived from Conversion?

def get_tense(self):
if self.occurrence_time is None:
Expand All @@ -299,15 +301,6 @@ def get_tense(self):
elif self.occurrence_time > current_cycle:
return NALSyntax.Tense.Future

def mutually_add_to_interacted_sentences(self, other_sentence):
self.interacted_sentences.append(other_sentence)
if len(self.interacted_sentences) > Config.MAX_INTERACTED_SENTENCES_LENGTH:
self.interacted_sentences.pop(0)

other_sentence.stamp.interacted_sentences.append(self.sentence)
if len(other_sentence.stamp.interacted_sentences) > Config.MAX_INTERACTED_SENTENCES_LENGTH:
other_sentence.stamp.interacted_sentences.pop(0)

class EvidentialBase:
"""
Stores history of how the sentence was derived
Expand Down
73 changes: 34 additions & 39 deletions NALInferenceRules.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,9 @@ def Revision(j1: NALGrammar.Sentence, j2: NALGrammar.Sentence):
assert (
j1.statement.term.get_formatted_string() == j2.statement.term.get_formatted_string()), "Cannot revise sentences for 2 different statements"

#todo handle occurrence_time
occurrence_time = None

# Get Truth Value
(wp1, w1, wn1), (wp2, w2, wn2) = getevidence_from2sentences(j1, j2)
result_truth = F_Revision(wp1=wp1,wn1=wn1,wp2=wp2,wn2=wn2)
Expand Down Expand Up @@ -297,7 +300,7 @@ def Choice(j1: NALGrammar.Sentence, j2: NALGrammar.Sentence):
subjpred2 = j2.subject_predicate

# Truth Value
(f1, c1), (f2, c2) = gettruthvalues_from2sentences(j1, j2)
(f1, c1), (f2, c2) = getevidentialvalues_from2sentences(j1, j2)

# Make the choice
if subjpred1 == subjpred2:
Expand Down Expand Up @@ -423,6 +426,7 @@ def Conversion(j: NALGrammar.Sentence):

# merge in the parent sentence's evidential base
result.stamp.evidential_base.merge_sentence_evidential_base_into_self(j)
result.stamp.from_conversion = True

return result

Expand Down Expand Up @@ -556,7 +560,7 @@ def IntensionalImage(j: NALGrammar.Sentence):

"""
======================================
==== (Strong Syllogistic Rules) ====
==== (First-order and Higher-order Syllogism) ====
======================================
"""

Expand Down Expand Up @@ -585,13 +589,13 @@ def Deduction(j1: NALGrammar.Sentence, j2: NALGrammar.Sentence):
j1.statement.get_predicate_term(),
j1.statement.get_copula())


if j1.punctuation == NALSyntax.Punctuation.Judgment and j2.punctuation == NALSyntax.Punctuation.Judgment:
result_truth= None
if j1.punctuation == NALSyntax.Punctuation.Judgment:
# Get Truth Value
(f1, c1), (f2, c2) = gettruthvalues_from2sentences(j1, j2)
(f1, c1), (f2, c2) = getevidentialvalues_from2sentences(j1, j2)
result_truth = F_Deduction(f1,c1,f2,c2)
result = NALGrammar.Judgment(result_statement, result_truth)
elif j1.punctuation == NALSyntax.Punctuation.Question or j2.punctuation == NALSyntax.Punctuation.Question:
elif j1.punctuation == NALSyntax.Punctuation.Question:
result = NALGrammar.Question(result_statement)

# merge in the parent sentences' evidential bases
Expand Down Expand Up @@ -654,12 +658,12 @@ def Analogy(j1: NALGrammar.Sentence, j2: NALGrammar.Sentence):
assert(False), "Error: Invalid inputs to nal_analogy: " + j1.get_formatted_string() + " and " + j2.get_formatted_string()

result = None
if j1.punctuation == NALSyntax.Punctuation.Judgment and j2.punctuation == NALSyntax.Punctuation.Judgment:
if j1.punctuation == NALSyntax.Punctuation.Judgment:
# Get Truth Value
(f1, c1), (f2, c2) = gettruthvalues_from2sentences(j1, j2)
(f1, c1), (f2, c2) = getevidentialvalues_from2sentences(j1, j2)
result_truth = F_Analogy(f1, c1, f2, c2)
result = NALGrammar.Judgment(result_statement, result_truth)
elif j1.punctuation == NALSyntax.Punctuation.Question or j2.punctuation == NALSyntax.Punctuation.Question:
elif j1.punctuation == NALSyntax.Punctuation.Question:
result = NALGrammar.Question(result_statement)

# merge in the parent sentences' evidential bases
Expand Down Expand Up @@ -719,13 +723,13 @@ def Resemblance(j1: NALGrammar.Sentence, j2: NALGrammar.Sentence):
assert (
False), "Error: Invalid inputs to nal_resemblance: " + j1.get_formatted_string() + " and " + j2.get_formatted_string()

if j1.punctuation == NALSyntax.Punctuation.Judgment and j2.punctuation == NALSyntax.Punctuation.Judgment:
if j1.punctuation == NALSyntax.Punctuation.Judgment:
# Truth Value
(f1, c1), (f2, c2) = gettruthvalues_from2sentences(j1, j2)
(f1, c1), (f2, c2) = getevidentialvalues_from2sentences(j1, j2)

result_truth = F_Resemblance(f1,c1,f2,c2)
result = NALGrammar.Judgment(result_statement, result_truth)
elif j1.punctuation == NALSyntax.Punctuation.Question or j2.punctuation == NALSyntax.Punctuation.Question:
elif j1.punctuation == NALSyntax.Punctuation.Question:
result = NALGrammar.Question(result_statement)

# merge in the parent sentences' evidential bases
Expand All @@ -734,14 +738,6 @@ def Resemblance(j1: NALGrammar.Sentence, j2: NALGrammar.Sentence):

return result


"""
======================================
++++ (Weak Syllogistic Rules) ++++
======================================
"""


def Abduction(j1: NALGrammar.Sentence, j2: NALGrammar.Sentence):
"""
Abduction (Weak syllogism)
Expand Down Expand Up @@ -770,13 +766,13 @@ def Abduction(j1: NALGrammar.Sentence, j2: NALGrammar.Sentence):
j1.statement.get_subject_term(),
j1.statement.get_copula())

if j1.punctuation == NALSyntax.Punctuation.Judgment and j2.punctuation == NALSyntax.Punctuation.Judgment:
if j1.punctuation == NALSyntax.Punctuation.Judgment:
# Get Truth Value
(f1, c1), (f2, c2) = gettruthvalues_from2sentences(j1, j2)
(f1, c1), (f2, c2) = getevidentialvalues_from2sentences(j1, j2)

result_truth = F_Abduction(f1,c1,f2,c2)
result = NALGrammar.Judgment(result_statement, result_truth)
elif j1.punctuation == NALSyntax.Punctuation.Question or j2.punctuation == NALSyntax.Punctuation.Question:
elif j1.punctuation == NALSyntax.Punctuation.Question:
result = NALGrammar.Question(result_statement)

# merge in the parent sentences' evidential bases
Expand Down Expand Up @@ -814,7 +810,7 @@ def Induction(j1: NALGrammar.Sentence, j2: NALGrammar.Sentence):

if j1.punctuation == NALSyntax.Punctuation.Judgment and j2.punctuation == NALSyntax.Punctuation.Judgment:
# Get Truth Value
(f1, c1), (f2, c2) = gettruthvalues_from2sentences(j1, j2)
(f1, c1), (f2, c2) = getevidentialvalues_from2sentences(j1, j2)

result_truth = F_Induction(f1,c1,f2,c2)
result = NALGrammar.Judgment(result_statement, result_truth)
Expand Down Expand Up @@ -856,7 +852,7 @@ def Exemplification(j1: NALGrammar.Sentence, j2: NALGrammar.Sentence):

if j1.punctuation == NALSyntax.Punctuation.Judgment and j2.punctuation == NALSyntax.Punctuation.Judgment:
# Get Truth Value
(f1, c1), (f2, c2) = gettruthvalues_from2sentences(j1, j2)
(f1, c1), (f2, c2) = getevidentialvalues_from2sentences(j1, j2)
result_truth = F_Exemplification(f1,c1,f2,c2)
result = NALGrammar.Judgment(result_statement, result_truth)
elif j1.punctuation == NALSyntax.Punctuation.Question or j2.punctuation == NALSyntax.Punctuation.Question:
Expand Down Expand Up @@ -907,7 +903,7 @@ def Comparison(j1: NALGrammar.Sentence, j2: NALGrammar.Sentence):

if j1.punctuation == NALSyntax.Punctuation.Judgment and j2.punctuation == NALSyntax.Punctuation.Judgment:
# Get Truth Value
(f1, c1), (f2, c2) = gettruthvalues_from2sentences(j1, j2)
(f1, c1), (f2, c2) = getevidentialvalues_from2sentences(j1, j2)

result_truth = F_Comparison(f1,c1,f2,c2)
result = NALGrammar.Judgment(result_statement, result_truth)
Expand Down Expand Up @@ -983,7 +979,7 @@ def IntensionalIntersectionOrDisjunction(j1, j2):

if j1.punctuation == NALSyntax.Punctuation.Judgment and j2.punctuation == NALSyntax.Punctuation.Judgment:
# Get Truth Value
(f1, c1), (f2, c2) = gettruthvalues_from2sentences(j1, j2)
(f1, c1), (f2, c2) = getevidentialvalues_from2sentences(j1, j2)

result_truth = F_Intersection(f1,c1,f2,c2)
result = NALGrammar.Judgment(result_statement, result_truth)
Expand All @@ -1003,7 +999,7 @@ def IntensionalIntersectionOrDisjunction(j1, j2):

if j1.punctuation == NALSyntax.Punctuation.Judgment and j2.punctuation == NALSyntax.Punctuation.Judgment:
# Get Truth Value
(f1, c1), (f2, c2) = gettruthvalues_from2sentences(j1, j2)
(f1, c1), (f2, c2) = getevidentialvalues_from2sentences(j1, j2)

result_truth = F_Union(f1,c1,f2,c2)
result = NALGrammar.Judgment(result_statement, result_truth)
Expand Down Expand Up @@ -1076,7 +1072,7 @@ def ExtensionalIntersectionOrConjunction(j1, j2):

if j1.punctuation == NALSyntax.Punctuation.Judgment and j2.punctuation == NALSyntax.Punctuation.Judgment:
# Get Truth Value
(f1, c1), (f2, c2) = gettruthvalues_from2sentences(j1, j2)
(f1, c1), (f2, c2) = getevidentialvalues_from2sentences(j1, j2)
result_truth = F_Union(f1,c1,f2,c2)
result = NALGrammar.Judgment(result_statement, result_truth)
elif j1.punctuation == NALSyntax.Punctuation.Question or j2.punctuation == NALSyntax.Punctuation.Question:
Expand All @@ -1094,7 +1090,7 @@ def ExtensionalIntersectionOrConjunction(j1, j2):

if j1.punctuation == NALSyntax.Punctuation.Judgment and j2.punctuation == NALSyntax.Punctuation.Judgment:
# Get Truth Value
(f1, c1), (f2, c2) = gettruthvalues_from2sentences(j1, j2)
(f1, c1), (f2, c2) = getevidentialvalues_from2sentences(j1, j2)
result_truth = F_Intersection(f1,c1,f2,c2)
result = NALGrammar.Judgment(result_statement, result_truth)
elif j1.punctuation == NALSyntax.Punctuation.Question or j2.punctuation == NALSyntax.Punctuation.Question:
Expand Down Expand Up @@ -1136,7 +1132,7 @@ def IntensionalDifference(j1, j2):

if j1.punctuation == NALSyntax.Punctuation.Judgment and j2.punctuation == NALSyntax.Punctuation.Judgment:
# Get Truth Value
(f1, c1), (f2, c2) = gettruthvalues_from2sentences(j1, j2)
(f1, c1), (f2, c2) = getevidentialvalues_from2sentences(j1, j2)
result_truth = F_Difference(f1,c1,f2,c2)
result = NALGrammar.Judgment(result_statement, result_truth)
elif j1.punctuation == NALSyntax.Punctuation.Question or j2.punctuation == NALSyntax.Punctuation.Question:
Expand Down Expand Up @@ -1190,7 +1186,7 @@ def ExtensionalDifference(j1, j2):

if j1.punctuation == NALSyntax.Punctuation.Judgment and j2.punctuation == NALSyntax.Punctuation.Judgment:
# Get Truth Value
(f1, c1), (f2, c2) = gettruthvalues_from2sentences(j1, j2)
(f1, c1), (f2, c2) = getevidentialvalues_from2sentences(j1, j2)
result_truth = F_Difference(f1,c1,f2,c2)
result = NALGrammar.Judgment(result_statement, result_truth)
elif j1.punctuation == NALSyntax.Punctuation.Question or j2.punctuation == NALSyntax.Punctuation.Question:
Expand Down Expand Up @@ -1227,7 +1223,7 @@ def Temporal_Induction(j1: NALGrammar.Judgment, j2: NALGrammar.Judgment):
:- or Sentence (S =/> P <f3, c3>)
:- or Sentence (P =/> S <f3, c3>)
"""
(f1, c1), (f2, c2) = gettruthvalues_from2sentences(j1, j2)
(f1, c1), (f2, c2) = getevidentialvalues_from2sentences(j1, j2)
j1_statement_term = j1.statement.term
j2_statement_term = j2.statement.term

Expand Down Expand Up @@ -1270,7 +1266,7 @@ def Temporal_Comparison(j1: NALGrammar.Judgment, j2: NALGrammar.Judgment):
:- or Sentence (S </> P <f3, c3>)
:- or Sentence (P </> S <f3, c3>)
"""
(f1, c1), (f2, c2) = gettruthvalues_from2sentences(j1, j2)
(f1, c1), (f2, c2) = getevidentialvalues_from2sentences(j1, j2)
j1_statement_term = j1.statement.term
j2_statement_term = j2.statement.term

Expand Down Expand Up @@ -1300,7 +1296,6 @@ def Temporal_Comparison(j1: NALGrammar.Judgment, j2: NALGrammar.Judgment):
======================================
"""


def get_truthvalue_from_evidence(wp, w):
"""
Input:
Expand Down Expand Up @@ -1340,7 +1335,7 @@ def get_confidence_from_evidence(w):
"""
return w / (w + Config.k)

def gettruthvalues_from2sentences(j1: NALGrammar.Sentence, j2: NALGrammar.Sentence):
def getevidentialvalues_from2sentences(j1: NALGrammar.Sentence, j2: NALGrammar.Sentence):
"""
Input:
j1: Statement <f1, c1>
Expand All @@ -1349,10 +1344,10 @@ def gettruthvalues_from2sentences(j1: NALGrammar.Sentence, j2: NALGrammar.Senten
Returns:
f1, c1, f2, c2
"""
return gettruthvalues_fromsentence(j1), gettruthvalues_fromsentence(j2)
return getevidentialvalues_fromsentence(j1), getevidentialvalues_fromsentence(j2)


def gettruthvalues_fromsentence(j: NALGrammar.Sentence):
def getevidentialvalues_fromsentence(j: NALGrammar.Sentence):
"""
Input:
j: Statement <f, c>
Expand All @@ -1371,5 +1366,5 @@ def getevidence_from2sentences(j1: NALGrammar.Sentence, j2: NALGrammar.Sentence)
Returns:
w1+, w1, w1-, w2+, w2, w2-
"""
(f1, c1), (f2, c2) = gettruthvalues_from2sentences(j1, j2)
(f1, c1), (f2, c2) = getevidentialvalues_from2sentences(j1, j2)
return get_evidence_fromfreqconf(f1, c1), get_evidence_fromfreqconf(f2, c2)
Loading

0 comments on commit 966f40a

Please sign in to comment.