Skip to content

Commit

Permalink
feat: add support to others operators
Browse files Browse the repository at this point in the history
  • Loading branch information
luisfbl committed Sep 12, 2024
1 parent b0fc851 commit e25a821
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 9 deletions.
51 changes: 51 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,56 @@ The "Predimate" algorithm, created during the second semester at Ulbra by me, is
<h2>🤝 Contributing</h2>
<p>For detailed information on how to contribute, please refer to our <a href="https://github.com/baskerbyte/predimate/blob/dev/CONTRIBUTING.md">Contribution Guide</a>. It covers everything you need to know about making contributions to Predimate. Happy contributing! 🚀</p>

<h2>✍️ How to write sentences?</h2>
<table>
<tr>
<th>Operator</th>
<th>Name</th>
</tr>
<tr>
<td>^</td>
<td>Conjunction</td>
</tr>
<tr>
<td>∧</td>
<td>Conjunction</td>
</tr>
<tr>
<td>v</td>
<td>Disjunction</td>
</tr>
<tr>
<td>∨</td>
<td>Disjunction</td>
</tr>
<tr>
<td>-></td>
<td>Conditional</td>
</tr>
<tr>
<td>→</td>
<td>Conditional</td>
</tr>
<tr>
<td><-></td>
<td>Biconditional</td>
</tr>
<tr>
<td>↔</td>
<td>Biconditional</td>
</tr>
<tr>
<td>¬</td>
<td>Negation</td>
</tr>
<tr>
<td>~</td>
<td>Negation</td>
</tr>
</table>

- (p ∧ q) ∧ (¬p ∨ ¬q)
- (p^q)^(~pv~q)

<h2>📋 Examples</h2>
<img src="images/menu.png">
2 changes: 1 addition & 1 deletion src/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def get_input_list(prompt, repeat=True):
prepositions, expressions = [], []

while True:
item = input(prompt)
item = input(prompt).replace(" ", "")

if item == "0":
break
Expand Down
16 changes: 13 additions & 3 deletions src/data/encode.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def encode_expression(expr: str) -> Base:

# The behavior of negation is different from the other operators
# While operators have a left and right side to the expression, negation has only one parameter
if operator != '~':
if operator not in get_operators(Negation):
right = encode_expression(expr[i:])
left = result.pop()

Expand Down Expand Up @@ -58,7 +58,7 @@ def encode_expression(expr: str) -> Base:
j = find_parentheses_end(expr[i:]) + i
sub_result = encode_expression(expr[i:j])
i = j
elif expr[i] == '~':
elif expr[i] in get_operators(Negation):
i += 1
j = find_end(expr[i:]) + i + 1
sub_result = Negation(Predicate(expr[i], [*expr[i + 1:j]]))
Expand Down Expand Up @@ -122,9 +122,19 @@ def find_quantifier(expr: str) -> str | None:

def find_end(expr):
for i in range(len(expr)):
if not expr[i].isalpha() or expr[i] == 'v':
if not expr[i].isalpha() or expr[i] == 'v' or expr[i] == '∨':
return i - 1
elif i == len(expr) - 1:
return i

return None


def get_operators(op):
result = []

for key, value in operators.items():
if value == op:
result.append(key)

return result
15 changes: 10 additions & 5 deletions src/entity/operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,19 @@ def __str__(self):


class Conjunction(Operator):
symbol = '^'
symbol = ''


class Disjunction(Operator):
symbol = 'v'
symbol = ''


class Conditional(Operator):
symbol = '->'
symbol = ''


class BiConditional(Operator):
symbol = '<->'
symbol = ''


class Negation(Base):
Expand All @@ -40,7 +40,7 @@ def __str__(self):
expr_str = f'({self.expr})' if isinstance(self.expr, Operator) or isinstance(self.expr, Negation) \
else str(self.expr)

return f'~{expr_str}'
return f'¬{expr_str}'


class Preposition(Base):
Expand All @@ -53,8 +53,13 @@ def __str__(self):

operators = {
'^': Conjunction,
'∧': Conjunction,
'v': Disjunction,
'∨': Disjunction,
'->': Conditional,
'→': Conditional,
'<->': BiConditional,
'↔': BiConditional,
'¬': Negation,
'~': Negation
}

0 comments on commit e25a821

Please sign in to comment.