Package: aida-lib
Aida LibAida is a language agnostic library for text generation.UsageA simple hello world script would look like this:fromaidaimportrender,Empty,Var# create a variable to hold a namename_var=Var('name')# create a simple phrasenode=(Empty+'hello,'|name_var).to_phrase()# assign a value to the variablename_var.assign('World')# render the nodeprint(render(node))# 'Hello, World.'InstallDownload and install with pip:pipinstallaidalibCore ConceptsWhen using Aida, first you compose a tree of operations on your text that include conditions via branches and other control flow. Later, you fill the tree with data and render the text.A building block is the variable class:Var. Use it to represent a value that you want to control later. A variable can hold numbers (e.g.float,int) or strings.You can create branches and complex logic withBranch. In the example below, ifxis greater than 1, it will rendermany, otherwisesingle.x=Var('x')Branch(x>1,'many','single')ContextThe context, represented by the classCtx, is useful to create rules that depends on what has been written before. Each object or literal that is passed to Aida is remembered by the context.name=Const('Bob')alt_name=Const('He')bob=Branch(~name.in_ctx(),name,alt_name)ctx=Ctx()render(bob|'is a cool guy.'|bob|'doesn\'t mind.',ctx)# Bob is a cool guy. He doesn't mind.Creating a reference expression is a common use-case, so we have a helper function calledcreate_ref.bob=create_ref('Bob','He')OperatorsYou can compose operations on your text with some handy operators.Concatenation (+and|)'the'|'quick'|'brown'|'fox'# 'the quick brown fox''the'+'quick'+'brown'+'fox'# 'thequickbrownfox'Check context (in_ctx)Check if the current node is in the context.Const('something').in_ctx()Create a sentence (sentence)Formats a line into a sentence, capitalizing the first word and adding a period.Const('hello world').sentence()# 'Hello world.'Logical and numeric operatorsoperatorexamplenegation!xgreater thanx > ygreater or equal thanx >= yless thanx < yless or equal thanx <= yequalx == ynot equalx != yor`xandx & yplusx + yRandom choiceRandomly draws one node from a list of possibilities.Choice('Alice','Bob','Chris')# either 'Alice', 'Bob', or 'Chris'InjectorTheInjectorclass assigns values to variables from a list each time it is rendered. Very useful to automatically fill values based on data.animal=Var('animal')sound=Var('sound')node=animal|'makes'|soundnode=Injector([animal,sound],node)# assign multiple valuesnode.assign([{'animal':'cat','sound':'meaw'},{'animal':'dog','sound':'roof'},])render(node)# 'cat makes meaw'render(node)# 'dog makes roof'For-loops withRepeatUseRepeatto render a node multiple times. At the simplest level, you have this:render(Repeat('buffalo').assign(8))# 'buffalo buffalo buffalo buffalo buffalo buffalo buffalo buffalo'Repeatis very useful when used withInjector, like this:animal=Var('animal')sound=Var('sound')node=animal|'makes'|soundnode=Injector([animal,sound],node)repeat=Repeat(node)# assign multiple valuesdata=[{'animal':'cat','sound':'meaw'},{'animal':'dog','sound':'roof'},]node.assign(data)repeat.assign(len(data))# renders text based on datarender(node)# cat makes meaw dog makes roofLanguage ConceptsThere are some experimental features that allows you to create text that adapts to common language features, like grammaticalnumberandperson.Enumerate itemsUseLangConfigto setup language features and then callcreate_enumeration().fromaidaimportcreate_enumeration,LangConfig,Lang,renderrender(create_enumeration(LangConfig(lang=Lang.ENGLISH),'Alice','Bob','Chris'))# 'Alice, Bob, and Chris'render(create_enumeration(LangConfig(lang=Lang.PORTUGUESE),'Alice','Bob','Chris'))# 'Alice, Bob e Chris'Sentence StructureYou can compose sentences using special structures:NP(noun phrase) andVP(verb phrase) along withLangConfig.fromaidaimportNP,VP,LangConfigsubj=NP('the dog')verb=VP('barked')s=(subj|verb).sentence()render(LangConfig(s))# The dog barked.What really makes this different from just usingConstis that we can create rules that change the output ofNPandVPbased on various language features. The system will try to use the rule that matches most features from the givenLangConfig.fromaidaimportNP,VP,LangConfig,GNumber,GPersonsubj=(NP('I').add_mapping('I',GNumber.SINGULAR,GPerson.FIRST).add_mapping('he',GNumber.SINGULAR,GPerson.THIRD)).add_mapping('we',GNumber.PLURAL,GPerson.FIRST))verb=(VP('drive').add_mapping('drive',GPerson.FIRST).add_mapping('drives',GPerson.THIRD))s=(subj|verb|'a nice car').sentence()render(LangConfig(s,number=GNumber.SINGULAR,person=GPerson.FIRST))# I drive a nice car.render(LangConfig(s,number=GNumber.SINGULAR,person=GPerson.THIRD))# He drives a nice car.render(LangConfig(s,number=GNumber.PLURAL,person=GPerson.FIRST))# We drive a nice car.ExamplesCheck more complex uses at theexamples folder.LicenseLicensed under theMIT License.