def test_alt(): x = aida.Const('Alice') other_names = aida.Choices('Bob', 'Chris', seed=42) ctx = aida.Ctx() alt = aida.create_alt(x, other_names) assert aida.render(alt, ctx) == 'Alice' assert aida.render(alt, ctx) == 'Bob'
def test_in_ctx(): ctx = aida.Ctx() k = aida.Const('Alice') branch = aida.Branch(k.in_ctx(), 'yes', 'no') assert aida.render(branch, ctx=ctx) == 'no' k.render(ctx) assert aida.render(branch, ctx=ctx) == 'yes'
def test_injector(): a = aida.Var('a') b = aida.Var('b') node = a | b inj = aida.Injector([a, b], node) inj.assign([{'a': 'a1', 'b': 'b1'}, {'a': 'a2', 'b': 'b2'}]) assert aida.render(inj) == 'a1 b1' assert aida.render(inj) == 'a2 b2'
def test_sentence_ctx(): ref = aida.create_ref('Geralt of Rivia', 'Geralt') sentence = aida.Empty + 'Toss a coin to' | ref + '.' ctx = aida.Ctx() assert aida.render(sentence, ctx=ctx) == 'Toss a coin to Geralt of Rivia.' assert aida.render(sentence, ctx=ctx) == 'Toss a coin to Geralt.' assert aida.render(sentence, ctx=ctx) == 'Toss a coin to Geralt.' sentence2 = ref | 'is nice.' assert aida.render(sentence2, ctx=ctx) == 'Geralt is nice.'
def test_render_simple_choice(): x = aida.Var() x.assign('Alice') y = aida.Var() y.assign('Bob') choices = aida.Choices(x, y, seed=42) assert aida.render(choices) == 'Alice' x.assign('Chris') assert aida.render(choices) == 'Chris'
def test_concat(): x = aida.Var('name') x.assign('Alice') sent = aida.Const('good') ctx = aida.Ctx() node = x | 'is a' | sent | 'person.' assert aida.render(node, ctx) == 'Alice is a good person.' x.assign('Bob') assert aida.render(node, ctx) == 'Bob is a good person.'
def test_branch(): x = aida.Var('val') k = aida.Const(3) t = aida.Const(True) branch = aida.Branch((x > k) & (t == True), 'greater', 'not greater') x.assign(1) assert aida.render(branch) == 'not greater' x.assign(5) assert aida.render(branch) == 'greater' branch = aida.Branch((x > k) & (t == False), 'greater', 'not greater') assert aida.render(branch) == 'not greater'
def test_enumerate(): # english conf = aida.LangConfig(aida.Empty) assert aida.render(aida.create_enumeration(conf, 'Alice')) == 'Alice' assert aida.render(aida.create_enumeration( conf, 'Alice', 'Bob')) == 'Alice and Bob' assert aida.render(aida.create_enumeration( conf, 'Alice', 'Bob', 'Chris')) == 'Alice, Bob, and Chris' # portuguese conf.lang = aida.Lang.PORTUGUESE assert aida.render(aida.create_enumeration(conf, 'Alice')) == 'Alice' assert aida.render(aida.create_enumeration( conf, 'Alice', 'Bob')) == 'Alice e Bob' assert aida.render(aida.create_enumeration( conf, 'Alice', 'Bob', 'Chris')) == 'Alice, Bob e Chris'
def test_phrase(): phrase = (aida.Empty + 'this is a phrase').sentence() assert aida.render(phrase) == 'This is a phrase.'
from aida import render, Empty, Var # create a variable to hold a name name_var = Var('name') # create a simple phrase node = (Empty + 'hello,' | name_var).sentence() # assign a value to the variable name_var.assign('World') # render the node print(render(node))
def create_forecast(weather_label, condition, temp, time_of_day): node = create_match(weather_label, now=Choices( 'right now', 'now'), default=time_of_day + ',') verb = Branch(~condition.in_ctx(), create_match( weather_label, now='is', default='will be'), 'will') return (node | 'the weather' | verb | create_alt(condition, 'remain the same') | 'with a temperature of' | (temp | 'degrees')).sentence() ctx = Ctx() # define overall structure weather_label = Var('time') condition = Var('cond') temp = Var('temp') tod = Var('tod') forecast = create_forecast(weather_label, condition, temp, tod) inj = Injector([weather_label, condition, temp, tod], forecast) repeat = Repeat(inj) # assign data data = [{'time': 'now', 'cond': 'rainy', 'temp': 20, 'tod': None}, {'time': 'afternoon', 'cond': 'clear', 'temp': 15, 'tod': 'in the afternoon'}, {'time': 'evening', 'cond': 'clear', 'temp': 12, 'tod': 'in the night'}] inj.assign(data) repeat.assign(len(data)) # print result print(render(repeat))
Lang.PORTUGUESE, GNumber.SINGULAR).add_mapping('eu', GPerson.FIRST).add_mapping( 'ele', GPerson.THIRD).pop().push(GNumber.PLURAL).add_mapping( 'nós', GPerson.FIRST).add_mapping('eles', GPerson.THIRD)) verb = (VP('make').push(Lang.ENGLISH).add_mapping( 'make', GNumber.SINGULAR, GNumber.PLURAL, GPerson.FIRST).add_mapping( 'makes', GNumber.SINGULAR, GPerson.THIRD).clear().push( Lang.PORTUGUESE, GNumber.SINGULAR).add_mapping('faço', GPerson.FIRST).add_mapping( 'faz', GPerson.THIRD).pop().push(GNumber.PLURAL).add_mapping( 'fazemos', GPerson.FIRST).add_mapping('fazem', GPerson.THIRD)) cake = (NP('a cake').add_mapping('a cake', Lang.ENGLISH).add_mapping( 'um bolo', Lang.PORTUGUESE)) node = (subj | verb | cake).sentence() conf = LangConfig(node) for lang, person, number in product((Lang.ENGLISH, Lang.PORTUGUESE), (GNumber.SINGULAR, GNumber.PLURAL), (GPerson.FIRST, GPerson.THIRD)): conf.lang = lang conf.person = person conf.number = number print(f'lang={lang.value} person={person.value} number={number.value}:') print('\t', render(conf))