Ejemplo n.º 1
0
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'
    }
Ejemplo n.º 2
0
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"
    }
Ejemplo n.º 3
0
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)
Ejemplo n.º 4
0
 def set_transition_natex(self, source, target, speaker, natex):
     source, target = module_source_target(source, target)
     source = State(source)
     target = State(target)
     if isinstance(natex, str):
         if speaker == Speaker.USER:
             natex = NatexNLU(natex, macros=self._macros)
         else:
             natex = NatexNLG(natex, macros=self._macros)
     self._graph.arc_data(source, target, speaker)['natex'] = natex
Ejemplo n.º 5
0
 def update_state_settings(self, state, **settings):
     state = module_state(state)
     state = State(state)
     if 'settings' not in self._graph.data(state):
         self._graph.data(state)['settings'] = Settings()
     if 'global_nlu' in settings:
         self.add_global_nlu(state, settings['global_nlu'])
     if 'enter' in settings and isinstance(settings['enter'], str):
         settings['enter'] = NatexNLG(settings['enter'], macros=self._macros)
     self.state_settings(state).update(**settings)
Ejemplo n.º 6
0
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'
Ejemplo n.º 7
0
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"
Ejemplo n.º 8
0
 def add_system_transition(self, source: Union[Enum, str, tuple], target: Union[Enum, str, tuple],
                           natex_nlg: Union[str, NatexNLG, List[str]], **settings):
     source, target = module_source_target(source, target)
     source = State(source)
     target = State(target)
     if self.has_transition(source, target, Speaker.SYSTEM):
         raise ValueError('system transition {} -> {} already exists'.format(source, target))
     natex_nlg = NatexNLG(natex_nlg, macros=self._macros)
     if not self.has_state(source):
         self.add_state(source)
     if not self.has_state(target):
         self.add_state(target)
     self._graph.add_arc(source, target, Speaker.SYSTEM)
     self.set_transition_natex(source, target, Speaker.SYSTEM, natex_nlg)
     transition_settings = Settings(score=1.0)
     transition_settings.update(**settings)
     self.set_transition_settings(source, target, Speaker.SYSTEM, transition_settings)
     if self._all_multi_hop:
         self.update_state_settings(source, system_multi_hop=True)
     if target in self._prepends:
         prepend = self._prepends[target]
         natex = self.transition_natex(source, target, Speaker.SYSTEM)
         self.set_transition_natex(source, target, Speaker.SYSTEM, prepend + natex)
Ejemplo n.º 9
0
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'}
Ejemplo n.º 10
0
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()
Ejemplo n.º 11
0
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'
Ejemplo n.º 12
0
def test_reference():
    v = {'A': 'apple'}
    ng = NatexNLG('i like $A')
    assert ng.generate(vars=v) == 'i like apple'
Ejemplo n.º 13
0
def test_rigid_sequence():
    ng = NatexNLG('[!this, test, case]')
    assert ng.generate() == 'this test case'
Ejemplo n.º 14
0
 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
Ejemplo n.º 15
0
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()
Ejemplo n.º 16
0
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"
Ejemplo n.º 17
0
def test_reference():
    v = {"A": "apple"}
    ng = NatexNLG("i like $A")
    assert ng.generate(vars=v) == "i like apple"
Ejemplo n.º 18
0
def test_rigid_sequence():
    ng = NatexNLG("[!this, test, case]")
    assert ng.generate() == "this test case"
Ejemplo n.º 19
0
def test_empty_string_nlg():
    natex = NatexNLG('')
    assert natex.generate() == ''
Ejemplo n.º 20
0
def test_nlg_markup():
    natex = NatexNLG('`She said, "hi there!" 10. <tag>`')
    assert natex.generate() == 'She said, "hi there!" 10. <tag>'