recognize labels things it finds.
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}'{
"pieces": 28,
"request_count": 1
}Step 2: label
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}'{"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"}Step 3: relate
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]'["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]
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.
On GitHub: github.com/botassembly/beatles-bench/tree/main/examples/recognize