def test_backreference(): v = {'X': 'apple'} ng = NatexNLG('$X is $X=banana') assert ng.generate(vars=v) == 'apple is banana' assert v['X'] == 'banana' ng = NatexNLG('$X=apple, is $X') assert ng.generate(vars=v) == 'apple is apple'
def test_backreference(): v = {"X": "apple"} ng = NatexNLG("$X is $X=banana") assert ng.generate(vars=v) == "apple is banana" assert v["X"] == "banana" ng = NatexNLG("$X=apple, is $X") assert ng.generate(vars=v) == "apple is apple"
def test_simple_macro(): natex = NatexNLG('i have a #SIMPLE', macros=macros) forms = set() for i in range(100): forms.add(natex.generate()) assert forms == { 'i have a foo', 'i have a bar', 'i have a bat', 'i have a baz' }
def test_simple_macro(): natex = NatexNLG("i have a #SIMPLE", macros=macros) forms = set() for i in range(100): forms.add(natex.generate()) assert forms == { "i have a foo", "i have a bar", "i have a bat", "i have a baz" }
class UpdateRule: def __init__(self, precondition, postcondition='', vars=None, macros=None): self.precondition = None self.precondition_score = 1.0 self.postcondition = None self.postcondition_score = None self.is_repeating = len(precondition) > 0 and precondition[0] == '*' if self.is_repeating: precondition = precondition[1:] if macros is None: macros = {} if vars is None: vars = {} self.vars = vars self.macros = macros self.set_precondition(precondition) if postcondition: self.set_postcondition(postcondition) def set_precondition(self, natex_string): natex, score = self._natex_string_score(natex_string) self.precondition = NatexNLU(natex, macros=self.macros) if score: self.precondition_score = score def set_postcondition(self, natex_string): natex, score = self._natex_string_score(natex_string) self.postcondition = NatexNLG(natex, macros=self.macros) self.postcondition_score = score def _natex_string_score(self, natex_string): i = natex_string.rfind(' (') if i != -1: for c in natex_string[i + len(' ('):-1]: if c not in set('0123456789.'): return natex_string, None return natex_string[:i], float(natex_string[i + len(' ('):-1]) return natex_string, None def satisfied(self, user_input, vars, debugging=False): return self.precondition.match(user_input, vars=vars, debugging=debugging) def apply(self, vars, debugging=False): if self.postcondition is not None: return self.postcondition.generate(vars=vars, debugging=debugging) else: return '' def set_vars(self, vars): self.vars = vars def __str__(self): return '{} ==> {}'.format(self.precondition, self.postcondition) def __repr__(self): return str(self)
def test_empty_string_nlg(): natex = NatexNLG('') assert natex.generate() == ''
def test_disjunction(): ng = NatexNLG('this is {a, the} test case') outputs = set() for i in range(100): outputs.add(ng.generate()) assert outputs == {'this is a test case', 'this is the test case'}
def test_completion(): v = {'X': 'apple'} natex = NatexNLG('i have $X in my $Y') assert natex.generate(vars=v) is None natex = NatexNLG('i have $X') assert not natex.is_complete()
def test_assignment(): v = {} ng = NatexNLG('i like $X={apple, banana}') ng.generate(vars=v, debugging=False) assert v['X'] == 'apple' or v['X'] == 'banana'
def test_reference(): v = {'A': 'apple'} ng = NatexNLG('i like $A') assert ng.generate(vars=v) == 'i like apple'
def test_rigid_sequence(): ng = NatexNLG('[!this, test, case]') assert ng.generate() == 'this test case'
def test_nlg_markup(): natex = NatexNLG('`She said, "hi there!" 10. <tag>`') assert natex.generate() == 'She said, "hi there!" 10. <tag>'
def test_completion(): v = {"X": "apple"} natex = NatexNLG("i have $X in my $Y") assert natex.generate(vars=v) is None natex = NatexNLG("i have $X") assert not natex.is_complete()
def test_assignment(): v = {} ng = NatexNLG("i like $X={apple, banana}") ng.generate(vars=v, debugging=False) assert v["X"] == "apple" or v["X"] == "banana"
def test_reference(): v = {"A": "apple"} ng = NatexNLG("i like $A") assert ng.generate(vars=v) == "i like apple"
def test_rigid_sequence(): ng = NatexNLG("[!this, test, case]") assert ng.generate() == "this test case"