Branch Logic Syntax Guide
Branch Rules Must Evaluate to True or False
When you create a Branch, you configure Wordsmith to write a certain word, phrase, or sentence if a conditional rule is true.
Those rules are written in the top box of the Branch window, which is labeled “If this is true.” Wordsmith will determine if that rule is true or false for that particular row of data. If a rule is true, then the associated text in the “Then write” box will show in the narrative.
You can use different functions in Branches based on the type of data you're using: Numbers, Text, Lists, Dates, and True/False.
Creating Branch Rules
Rules in Wordsmith can include comparisons and logical expressions.
Usable Across All Data | |
---|---|
= | Equal to |
!= | Not equal to |
and | To evaluate more than one condition that must both be True |
or | To evaluate more than one condition where either can be True |
Logical expressions include the words “and” and “or”. They allow you to link multiple comparisons together.
For example, you might want to write a certain phrase only if sales and profits are both up.
You can use more than one logical expression in a condition. Use parentheses to keep Wordsmith organized, just like the order of operations. For example:
The rule means something different if you change parentheses:
Using Math
For data categorized as a Number
For Numbers | |
---|---|
= | Equal to |
!= | Not equal to |
> | Greater than |
< | Less than |
>= | Greater than or equal to |
<= | Less than or equal to |
+ | Add |
- | Subtract |
* | Multiply |
/ | Divide |
% | Modulo |
percentChange | Calculate the percentage difference between two values. |
min | The minimum value for a data variable across all rows in the data uploaded to that Project. |
max | The maximum value for a data variable across all rows in the data uploaded to that Project. |
average | The average value for a data variable across all rows in the data uploaded to that Project. |
sum | The sum of all values for a data variable across all rows in the data uploaded to that Project. |
These comparisons can be used to compare one data point to another:
Or, to compare a data point to a number
Within Branch rules, you can do math using the standard +, -, *, / and % signs. For example:
You’ll only be able to do mathematical comparisons on data that represent real numbers. Wordsmith can’t compare text variables. A string of text like “cheese” is not greater than, equal to, or less than “socks” — at least as far as Wordsmith knows. As long as data represent real numbers, you can do math on them in your Branch rules.
Adding modulo, %, to a condition will find the remainder of division. You can use it as a variability tool to say something in particular rows of your data. For example, by using the unique ID automatically assigned to each row of your data, the condition below would return TRUE in every fifth row of your data. So every fifth piece of content could have a particular word, phrase, or sentence.
Use percentChange to calculate the percentage difference between two data variables. The order of the data variables in the function is important - the first data variable should be the original value, and the second data variable should be the value that has changed. The calculation works like this:
percentChange (original_value, changed_value) = ((original_value - changed_value) / original_value) * 100
The min, max, average, and sum functions all work the same way. They'll use all of the values for a particular data variable based on the data uploaded to that Project. If you're using the Wordsmith API, the data you send through the API will be compiled with the data uploaded to the Project. Every time you edit your data on the Data tab, the data will be reset and whatever you sent via the API will not be included in these calculations.
These are the only functions that compare data across rows.
Using Text Attributes
For Text | |
---|---|
= | Equal to |
!= | Not equal to |
"" | Null value in your data file |
includes | Will return True if specific text is in a row of your data file, even a partial match |
excludes | Will return True if specific text is not in a row of your data file, even a partial match |
You can also use text columns in your Branch rules, but they work slightly differently. You’re simply creating a rule that applies if a certain string of text appears in a column. Use the = operator and double quotes around the value for a scenario where you want an exact match.
Or you can search for a partial match of a text string.
Don't forget to use quotes around Text when using it in a Branch rule.
Using Date Functions
For Dates | |
---|---|
= | Will check for an exact date. Enter the date in quotes and use slashes, i.e. "4/1/16". |
!= | Will check that your data does not match an exact date. Enter the date in quotes and use slashes, i.e. "4/1/16". |
> | True for dates that come after the date in your data. |
>= | True for dates that come after to or on the same day as the date in your data. |
< | True for dates that come before the date in your data. |
<= | True for dates that come before or on the date in your data. |
getWeekday | Will return a numerical value for the day of the week. With Sunday = 1 and Saturday = 7. |
getDay | Will return the day part of a date as a number. |
getMonth | Will return the month part of a date as a number. |
getYear | Will return the year part of a date as a number. |
daysBetween | Will calculate the number of days between 2 dates. |
monthsBetween | Will return the months between 2 dates as a whole number. |
yearsBetween | Will return the years between 2 dates as a whole number. |
daysAhead | Allows you to add days to a date and return that new date. |
daysAgo | Allows you to subtract days from a date and return that new date. |
monthsAhead | Allows you to add months to a date and return that new date. |
monthsAgo | Allows you to subtract months from a date and return that new date. |
yearsAhead | Allows you to add years to a date and return that new date. |
yearsAgo | Allows you to subtract years from a date and return that new date. |
Get functions will parse out different parts of the date from your data. So for example, if the date = 4/1/16, then
getWeekday(date) = 7
getDay(date) = 1
getMonth(date) = 4
getYear(date) = 2016
You can use getWeekday to reference the days of the week.
You can use getMonth to denote seasons.
And you can compare the years of two different dates using getYear.
Between functions will allow you to compare time between two dates: the date from your data and a new date you specify in the function. Say the date in the data is 4/1/16, then
daysBetween(date,"5/31/16") = 30
monthsBetween(date,"5/31/16") = 1
yearsBetween(date,"5/31/16") = 0
You can also get the time between two data variables that are dates.
Ahead and Ago functions let you perform math on dates, like adding or subtracting time, and then return that new date. Ahead functions will add time to a date, and Ago functions will subtract time. Use numbers to add and subtract time in days, months, or years. So if the date in the data is 4/1/16, then
daysAhead(date, 2) = 4/3/16
daysAgo(date, 2) = 3/30/16
monthsAhead(date, 2) = 6/1/16
monthsAgo(date, 2) = 2/1/16
yearsAhead(date, 2) = 4/1/18
yearsAgo(date, 2) = 4/1/14
You can even combine the date functions in interesting ways.
Using Time
For Time | |
---|---|
= | Will be true if two times are the same. Make sure to put the value in quotes or insert another Time data variable. |
!= | Will be true if two times are not the same. |
> | Will be true if the time is later than the subsequent data. |
>= | Will be true if the time is the same or later than the subsequent data. |
< | Will be true if the time is earlier than the subsequent data. |
<= | Will be true if the time is the same or earlier than the subsequent data. |
You can compare Time data variables to each other or to a new value. When comparing to a new value, make sure to put the value in quotes. The new value can be in any Time format recognized by Wordsmith (even if it is not the same format as the original data).
You can also compare two Time data variables to each other.
Using Lists of Objects
A list is a series of words separated by commas in just one cell of your data file. For example, in the data below, the data in the "top neighborhoods" column is a List.
You can use all the same syntax for Lists that you can with Text, plus the includes, excludes, and count functions.
For Lists | |
---|---|
= | Equal to |
!= | Not equal to |
"" | Blank value in your data |
includes | Will return True if the exact text string is included in a list on that row of your data |
excludes | Will return True if the exact text string is not included in a list on that row of your data |
count | Count the number of objects in a list |
sort | Sort the objects in a list in ascending or descending order |
valueAt | Pull out one object in a list |
Lists work similarly to Text. To search for an exact match, use quotes around the text string exactly as it appears in your data file.
Using the exact match function for Lists is fairly uncommon. You'll likely want to search for one object in the list. And to do that, you can use the includes and excludes functions.
To count how many objects are in a list, use the "count" function. You can use "count" to treat lists with just one object differently than lists with more than one object. Now we can say The top neighborhood is... if there is only one object in that list. Or The top neighborhoods are... if there is more than one object.
You can re-order the objects in a List by using the sort function, either ascending or descending. To sort the List in ascending order, the syntax is sort(data_variable, "asc") and to put the List in descending order, sort(data_variable, "desc").
To pull out a specific object in a List use the valueAt function. The objects start at 0, so valueAt(data_variable, 0) would give you the first object in the List, valueAt(data_variable, 1) would give you the second, and so on. You can even combine the sort and valueAt functions to first sort the objects in a List and then pull out a specific one.
For more information on Lists, check out the Data Types page.
Using True/False Data
True/False data is data that only includes TRUE or FALSE values. In addition to "true" and "false", Wordsmith recognizes "yes" and "no", and their abbreviations.
All of these are TRUE:
- true
- t
- yes
- y
All of these are FALSE:
- false
- f
- no
- n
Accounting for Empty Cells
Even if your data set has some empty cells (or if you think it might but aren’t sure), you can still add a Branch Condition using two quotation marks side by side with nothing in the middle: “”.
This condition will return True if the data is blank for "expected precipitation".
expected_precipitation = “”
This condition will return True if there is data for "expected precipitation".
expected_precipitation != “”
Changing from Singular to Plural
If you need to change parts of your narrative to a singular or plural version based on something in your data, you can do that with a Branch. You'll just need to have a data variable that determines if that word or words needs to be singular or plural. Here's an example.
Updated over 5 years ago