ThinkThen

Beatles Bench pages
Scripts
What Jev knows
Tune your bar
Beatles Bench · The ten functions

recognize labels things it finds.

The talk's slide: recognize labels things it finds.
Open the slide at full size.

recognize finds every name in a text and gives each one a kind from your list. It works in three steps.

First it finds the names. It splits the text into pieces and asks one question about each piece. Does the piece begin a name, sit inside one, end one, stand alone as a name, or sit outside every name? Then it labels each name with one of your kinds. Last it relates the names. Each rule names a relation, a subject kind, and an object kind. recognize asks one yes or no question for each pair a rule allows.

Step 1: find

--plan shows the find step's request: 28 pieces in one request.
sentence="Ringo Starr wrote Octopus's Garden"
sentence+=" on a boat off Sardinia, and the band"
sentence+=" recorded it at Abbey Road Studios"
sentence+=" for the album Abbey Road."
kinds=(person song album place)

printf '%s' "$sentence" |
thinkthen recognize "${kinds[@]}" \
  --plan |
head -1 |
jq '{pieces, request_count}'
Output
{
  "pieces": 28,
  "request_count": 1
}
exit 0

Step 2: label

The label step gives each name one of the four kinds.
sentence="Ringo Starr wrote Octopus's Garden"
sentence+=" on a boat off Sardinia, and the band"
sentence+=" recorded it at Abbey Road Studios"
sentence+=" for the album Abbey Road."
kinds=(person song album place)

printf '%s' "$sentence" |
thinkthen recognize "${kinds[@]}" \
  --replay recording |
jq -c '.entities[] | {text, kind}'
Output
{"text":"Ringo Starr","kind":"person"}
{"text":"Octopus's Garden","kind":"song"}
{"text":"Sardinia","kind":"place"}
{"text":"Abbey Road Studios","kind":"place"}
{"text":"Abbey Road","kind":"album"}
exit 0

Step 3: relate

The relate step keeps the edges at 0.5 or more.
sentence="Ringo Starr wrote Octopus's Garden"
sentence+=" on a boat off Sardinia, and the band"
sentence+=" recorded it at Abbey Road Studios"
sentence+=" for the album Abbey Road."
kinds=(person song album place)

printf '%s' "$sentence" |
thinkthen recognize "${kinds[@]}" \
  --relation wrote=person:song \
  --relation recorded_at=song:place \
  --relation on_album=song:album \
  --replay recording |
jq -c '.relations[] | [.source.text, .relation,
  .target.text, .probability]'
Output
["Ringo Starr","wrote","Octopus's Garden",0.99]
["Octopus's Garden","recorded_at","Abbey Road Studios",0.66]
["Octopus's Garden","on_album","Abbey Road",0.91]
exit 0

The lesson

All five names and their kinds are right. The sentence says Ringo Starr wrote Octopus's Garden, and that edge reads 0.99. The rules also let recognize ask whether Octopus's Garden was recorded at Sardinia. That answer falls under 0.5. No edge comes out for it.

Find the names, label them, then link them.