Finishing natural language plugin.

This commit is contained in:
Adsooi 2024-10-17 02:08:24 +02:00
parent 8fab9d8e52
commit ef465b34e7
Signed by: Ad5001
GPG key ID: EF45F9C6AFE20160
10 changed files with 751 additions and 320 deletions

View file

@ -15,17 +15,47 @@
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
from idlelib.configdialog import is_int
from math import log10, floor, ceil
from typing import Self
from tests.plugins.natural.interfaces.assertion import Assertion
from tests.plugins.natural.interfaces.base import BaseAssertionInterface, EqualAssertionInterface, AssertionInterface
from tests.plugins.natural.interfaces.utils import repr_
from .assertion import Assertion
from .base import AssertionInterface
from .utils import repr_
class IntComparisonAssertionInterface(AssertionInterface):
def __init__(self, value):
super().__init__(value)
self._compare_to = None
class NumberComparisonAssertionInterface(AssertionInterface):
def __init__(self, value, parent: AssertionInterface = None):
super().__init__(value, parent)
self._compare_stack = []
def _generate_compare_to(self) -> int:
"""
The number generated by the comparison stack.
E.g. can parse one.hundred.million.and.thirty.three.thousand.and.twelve.hundred.and.seven
as ['one', 'hundred', 'million', 'thirty', 'three', 'thousand', 'twelve', 'hundred', 'seven']
which results 100,034,207
"""
minus = len(self._compare_stack) > 0 and self._compare_stack[0] == -1
if len(self._compare_stack) < (2 if minus else 1):
raise RuntimeError("No number to compare the value to provided.")
if minus:
self._compare_stack.pop(0)
# Compute the number
add_stack = [self._compare_stack.pop(0)]
for element in self._compare_stack:
last_power = floor(log10(abs(add_stack[-1])))
current_power = floor(log10(abs(element)))
if last_power < current_power: # E.g. one hundred
add_stack[-1] *= element
elif last_power == 1 and current_power == 0: # E.g thirty four
add_stack[-1] += element
elif last_power > current_power: # E.g a hundred and five
add_stack.append(element)
else:
raise RuntimeError(f"Cannot chain two numbers with the same power ({add_stack[-1]} => {element}.")
total = sum(add_stack)
return -total if minus else total
def _compare(self) -> Assertion:
raise RuntimeError(f"No comparison method defined in {type(self).__name__}.")
@ -34,23 +64,26 @@ class IntComparisonAssertionInterface(AssertionInterface):
return bool(self._compare())
def __call__(self, compare_to: int) -> Self:
if self._compare_to is None:
self._compare_to = int(compare_to)
else:
self._compare_to *= int(compare_to)
if type(compare_to) not in (float, int):
raise RuntimeError(f"Cannot compare number ({self._value}) to non number ({repr_(compare_to)}).")
self._compare_stack.append(compare_to)
return self
"""
Chain self properties
"""
@property
def and_(self) -> Self:
return self
@property
def time(self) -> Self:
def AND(self) -> Self:
return self
@property
def times(self) -> Self:
return self
@property
def never(self) -> Self:
return self(0)
"""
Number shorthands
"""
@property
def once(self) -> Self:
@ -64,6 +97,10 @@ class IntComparisonAssertionInterface(AssertionInterface):
def thrice(self) -> Self:
return self(3)
@property
def minus(self) -> Self:
return self(-1)
@property
def zero(self) -> Self:
return self(0)
@ -108,6 +145,42 @@ class IntComparisonAssertionInterface(AssertionInterface):
def ten(self) -> Self:
return self(10)
@property
def eleven(self) -> Self:
return self(11)
@property
def twelve(self) -> Self:
return self(12)
@property
def thirteen(self) -> Self:
return self(13)
@property
def fourteen(self) -> Self:
return self(14)
@property
def fifteen(self) -> Self:
return self(15)
@property
def sixteen(self) -> Self:
return self(16)
@property
def seventeen(self) -> Self:
return self(17)
@property
def eighteen(self) -> Self:
return self(18)
@property
def nineteen(self) -> Self:
return self(19)
@property
def twenty(self) -> Self:
return self(20)
@ -134,11 +207,11 @@ class IntComparisonAssertionInterface(AssertionInterface):
@property
def eighty(self) -> Self:
return self(70)
return self(80)
@property
def ninety(self) -> Self:
return self(70)
return self(90)
@property
def hundred(self) -> Self:
@ -157,73 +230,92 @@ class IntComparisonAssertionInterface(AssertionInterface):
return self(1_000_000_000)
class LessThanComparisonInterface(IntComparisonAssertionInterface):
class LessThanComparisonInterface(NumberComparisonAssertionInterface):
def _compare(self) -> Assertion:
compare = self._generate_compare_to()
return Assertion(
self._value < self._compare_to,
f"The value ({repr_(self._value)}) is not less than to {repr_(self._compare_to)}."
self._value < compare,
f"The value ({repr_(self._value)}) is not less than to {repr_(compare)}.",
self._not
)
class MoreThanComparisonInterface(IntComparisonAssertionInterface):
class MoreThanComparisonInterface(NumberComparisonAssertionInterface):
def _compare(self) -> Assertion:
compare = self._generate_compare_to()
return Assertion(
self._value > self._compare_to,
f"The value ({repr_(self._value)}) is not more than to {repr_(self._compare_to)}."
self._value > compare,
f"The value ({repr_(self._value)}) is not more than to {repr_(compare)}.",
self._not
)
class AtLeastComparisonInterface(IntComparisonAssertionInterface):
class AtLeastComparisonInterface(NumberComparisonAssertionInterface):
def _compare(self) -> Assertion:
compare = self._generate_compare_to()
return Assertion(
self._value >= self._compare_to,
f"The value ({repr_(self._value)}) is not at least to {repr_(self._compare_to)}."
self._value >= compare,
f"The value ({repr_(self._value)}) is not at least to {repr_(compare)}.",
self._not
)
class AtMostComparisonInterface(IntComparisonAssertionInterface):
class AtMostComparisonInterface(NumberComparisonAssertionInterface):
def _compare(self) -> Assertion:
compare = self._generate_compare_to()
return Assertion(
self._value <= self._compare_to,
f"The value ({repr_(self._value)}) is not at least to {repr_(self._compare_to)}."
self._value <= compare,
f"The value ({repr_(self._value)}) is not at least to {repr_(compare)}.",
self._not
)
class EqualComparisonInterface(IntComparisonAssertionInterface):
class EqualComparisonInterface(NumberComparisonAssertionInterface):
def _compare(self) -> Assertion:
compare = self._generate_compare_to()
return Assertion(
self._value == self._compare_to,
f"The value ({repr_(self._value)}) is not equal to {repr_(self._compare_to)}."
self._value == compare,
f"The value ({repr_(self._value)}) is not equal to {repr_(compare)}.",
self._not
)
@property
def to(self) -> Self:
return self
def of(self) -> Self:
return self
class IntInterface(AssertionInterface):
def less_than(self) -> LessThanComparisonInterface:
return LessThanComparisonInterface(self._value)
@property
def more_than(self) -> MoreThanComparisonInterface:
return MoreThanComparisonInterface(self._value)
@property
def at_least(self) -> AtLeastComparisonInterface:
return AtLeastComparisonInterface(self._value)
@property
def at_most(self) -> AtMostComparisonInterface:
return AtMostComparisonInterface(self._value)
class NumberInterface(AssertionInterface):
def __call__(self, value):
return EqualComparisonInterface(self._value, self)(value)
@property
def equals(self) -> EqualComparisonInterface:
return EqualComparisonInterface(self._value)
return EqualComparisonInterface(self._value, self)
@property
def is_equal(self) -> EqualComparisonInterface:
return EqualComparisonInterface(self._value)
def equal(self) -> EqualComparisonInterface:
return EqualComparisonInterface(self._value, self)
@property
def exactly(self) -> EqualComparisonInterface:
return EqualComparisonInterface(self._value)
return EqualComparisonInterface(self._value, self)
@property
def of(self) -> EqualComparisonInterface:
return EqualComparisonInterface(self._value, self)
@property
def less_than(self) -> LessThanComparisonInterface:
return LessThanComparisonInterface(self._value, self)
@property
def more_than(self) -> MoreThanComparisonInterface:
return MoreThanComparisonInterface(self._value, self)
@property
def at_least(self) -> AtLeastComparisonInterface:
return AtLeastComparisonInterface(self._value, self)
@property
def at_most(self) -> AtMostComparisonInterface:
return AtMostComparisonInterface(self._value, self)