Skip to content

choose

choose picks the one choice from your list that best fits a piece of text, and prints it. When even the best choice falls short of the bar, it says not sure (unsure).

Terminal window
echo 'Please refund my order.' |
thinkthen choose \
--choice billing --choice shipping --choice other \
'What is the message about?'

Output

billing

billing: the message asks for money back.

Terminal window
echo 'Where is my parcel?' |
thinkthen choose \
--choice billing --choice shipping --choice other \
'What is the message about?'

Output

shipping

shipping: the message asks about a delivery.

choose picks the choice with the highest chance. If that chance is below the bar, it says not sure. The bar is 0.5 unless you set --threshold.

Terminal window
echo 'I was charged twice and my parcel is late again.' |
thinkthen choose --threshold 0.9 \
--choice billing --choice shipping --choice other \
'What is the message about?'

Output

unsure

Not sure: the message is about billing and shipping at once, so neither choice reaches 0.9. Send not sure to a person. Never send it to a default choice.

See why

--json prints the details. This jq keeps the chance of each choice.

Terminal window
echo 'Please refund my order.' |
thinkthen choose --json \
--choice billing --choice shipping --choice other \
'What is the message about?' |
jq -c '.detail.labels'

Output

{"billing":0.98,"other":0.02,"shipping":0.0}

billing has 0.98, well above the bar of 0.5, so choose picks it. JSON output lists every field.

Move the bar

Raise the bar to 0.99 and the same clear message is not sure, because billing has 0.98.

Terminal window
echo 'Please refund my order.' |
thinkthen choose --threshold 0.99 \
--choice billing --choice shipping --choice other \
'What is the message about?'

Output

unsure

Lower the bar to accept a closer call. Raise it to answer only when one choice clearly wins.

Use the answer in a script

--check turns the answer into the exit code, and the choice still prints.

Terminal window
message='Please refund my order.'
if topic=$(echo "$message" |
thinkthen choose --check \
--choice billing --choice shipping \
--choice other 'What is the message about?')
then
echo "send to the $topic queue"
else
status=$?
case "$status" in
1|3) echo 'send to a person' ;;
*) exit "$status" ;;
esac
fi

Output

send to the billing queue
Answer Exit code with --check
a choice 0
unsure 3

Without --check, choose exits 0 whenever it answers. Exit codes lists every code.

Choose for every row of a CSV file
messages.csv
id,text
a1,Please refund my order.
a2,Where is my parcel?
a3,I was charged twice for one invoice.
Terminal window
thinkthen choose 'What is the message about?' \
--choice billing --choice shipping --choice other \
--from csv --column text --into topic < messages.csv

Output

id,text,topic
a1,Please refund my order.,billing
a2,Where is my parcel?,shipping
a3,I was charged twice for one invoice.,billing

--column names the column to read, and --into names the new answer column. A row that is not sure gets an empty cell. File types covers TSV, Excel and other files.

Options

With --low and --high, choose answers only at or above the high bar. The low bar changes nothing, because choose never says no.

OptionWhat it does
<QUESTION>Question to ask
[FILE]...Input files; default: standard input
--name <NAME>Optional execution identity
--text <TEXT>Judge this text instead of reading input
--threshold <THRESHOLD>Required probability
--low <LOW>Low probability bar
--high <HIGH>High probability bar
--check [=<CHECK>]Return the verdict as an exit code
--choice <CHOICE>Choice; repeat in order
--describe <LABEL=TEXT>Description for a label; repeat
--column <COLUMN>Field column; repeat; replaces a saved pointer
--details [=<DETAILS>]Add status, probability, error, detail and source columns
--in-place [=<IN-PLACE>]Replace source with backup
--into <INTO>Answer field for JSON Lines; column for tables
--output <OUTPUT>Destination path
--sheet <SHEET>Worksheet

Shared options

If choose can't answer a text, it prints an error and exits with code 2, 4, 5 or 6. Troubleshooting lists each error and its fix.

In code: Python · TypeScript · Go · Java · Kotlin · C# · R · Ruby · Rust · Swift · Zig · C · C++ · DuckDB · SQLite · PostgreSQL

Next: filter keeps only the lines that answer yes.