New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conditional text generation #46

Open
fdb opened this Issue Mar 2, 2018 · 5 comments

Comments

Projects
None yet
4 participants
@fdb
Member

fdb commented Mar 2, 2018

I have a use case where I want to generate text based on incoming data. Depending on different data parameters I want to be able to select a piece of text.

E.g. there's a parameter called "rain_chance". Depending on the values you get different pieces of text:

weather_report(rain_chance):
-(rain_chance < 0.1) No rain expected today.
-(rain_chance < 0.1) It won't rain today.
-(rain_chance < 0.5) It probably won't rain today.
-(rain_chance < 0.8) We're pretty sure it will rain.
-(rain_chance > 0.8) Bring an umbrella!
-(_) No idea what the weather is going to be.

The syntax is preliminary: we should be able to distinguish between a conditional statement and a line of text that begins with a bracket.

  • There are two options for the first case, and Seed should select between them.
  • Note that there should also be a special operator for the "other" case.
@stebanos

This comment has been minimized.

Member

stebanos commented Mar 2, 2018

Hm but this conflicts with the current randomizing aspect of the - signififier. It would no longer mean "choose one of these options", but instead act like switch/case.

@fdb

This comment has been minimized.

Member

fdb commented Mar 3, 2018

I don't think it has to conflict. It just has a set of options that are also conditional. This means that if there are two options that satisfy the condition (like the rain_chance < 0.1 in the above example) the system can choose between the two; otherwise it is as if there was only one option to choose from.

@stebanos

This comment has been minimized.

Member

stebanos commented Mar 3, 2018

Ah I see. Well it's definitely something else that I had in mind for conditional logic, but yes this makes sense too!

@dhruvramdev

This comment has been minimized.

dhruvramdev commented Mar 13, 2018

Hi. If the syntax is somewhat finalized, then I can give it a shot .

@AlexanderRossa

This comment has been minimized.

AlexanderRossa commented Aug 5, 2018

This Issue has been resolved in seedtext - the NPM module based on Seed. The repository for the project is here.

The syntax, for now, has been slightly altered so that the conditions are more distinct to anything else, simplifying the process of finding them and dealing with them. Using the example from above, the current adjusted syntax would be:

weather_report(rain_chance):
- ([rain_chance < 0.1]) No rain expected today.
- ([rain_chance < 0.1]) It won't rain today.
- ([rain_chance < 0.5]) It probably won't rain today.
- ([rain_chance < 0.8]) We're pretty sure it will rain.
- ([rain_chance > 0.8]) Bring an umbrella!
- ([_]) No idea what the weather is going to be.

The wildcard operator _ has been implemented as well, but its usage is identical to not preceding the choice with any condition and as such it might be a bit redundant, unless we want to enforce that each choice in a block with at least one choice preceded with a condition must be preceded with a condition.

It might also be worth noting that the conditions are evaluated as classic JavaScript and if rain_chance passed in would be, for example, 0.05, any of the choices could be selected based on solely this condition. While this generally makes sense, in this example it could result in 'Bring an umbrella!' which does not seem to be correct. To specify choices valid for ranges of values, classic logical expressions chaining can be used:

- ([(rain_chance < 0.5) && (rain_chance > 0.25)]) It probably won't rain today.

The conditions are evaluated in the parsePhraseBook method and the condition variables are passed in as a conditionalVariables parameter which is a JS object with key:value pairs where key is the variable name and value is the value. The documentation explains this in more detail.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment