I am playing with Natural language programming in Mathematica, and can't figure out how to extract the entity classes (i.e. male, female) for the following example text:
"This was a pleasant summer afternoon. The hilly road crisscrossing the fields was a breezy ride for William Henry Hampton the Third, a boy of 8 years of age, and his cousin, Elizabeth-Ann Randall, a girl of 10 years of age. The Victorian English countryside was the perfect summer vacation setting for the cousins taking time off from a busy school in London. Their grandmother, Henriette Blake, who recently turned 57 years old, has been living in the countryside with her husband her entire life after her great-great grandparents left the city of Birmingham to try their hand in farming. Henriette's husband passed on due to old age many years ago, and her days were enlightened with her grandkids visits during their summer vacations.";
Below is what I tried, with actual result and expected result noted inline; the goals to achieve on are outlined below the code:
novel = "This was a pleasant summer afternoon. The hilly road \
crisscrossing the fields was a breezy ride for William Henry Hampton \
the Third, a boy of 8 years of age, and his cousin, Elizabeth-Ann \
Randall, a girl of 10 years of age. The Victorian English countryside \
was the perfect summer vacation setting for the cousins taking time \
off from a busy school in London. Their grandmother, Henriette Blake, \
who recently turned 57 years old, has been living in the countryside \
with her husband her entire life after her great-great grandparents \
left the city of Birmingham to try their hand in farming. Henriette's \
husband passed on due to old age many years ago, and her days were \
enlightened with her grandkids visits during their summer vacations.";
EntityRegister[EntityStore["male" -> <|
"Entities" -> <|"boy", "father", "grandfather", "son", "husband"|>
|>]];
EntityRegister[EntityStore["female" -> <|
"Entities" -> <|"girl", "mother", "grandmother", "daughter", "wife"|>
|>]];
FindTextualAnswer[novel, "What are the names of the males?"]
(* Result: Elizabeth-Ann Randall, Expected: William Henry Hampton the Third *)
(* HOWEVER, is the above be better suited for TextCases? This is tested below. *)
FindTextualAnswer[novel, "What is the name of the boy?"]
(* Result: Elizabeth-Ann Randall, Expected: William Henry Hampton the Third *)
FindTextualAnswer[novel, "What is the name of the father?"]
(* Result: Elizabeth-Ann Randall, Expected: none *)
FindTextualAnswer[novel, "What is the name of the grandfather?"]
(* Result: Henriette Blake, Expected: not available *)
FindTextualAnswer[novel, "What are the names of the females?"]
(* Result: Elizabeth-Ann Randall, Expected: Elizabeth-Ann Randall, Henriette Blake *)
(* HOWEVER, is the above be better suited for TextCases? This is tested below. *)
FindTextualAnswer[novel, "What is the name of the girl?"]
(* Result: Elizabeth-Ann Randall, Expected: Elizabeth-Ann Randall *)
FindTextualAnswer[novel, "What is the name of the mother?"]
(* Result: Elizabeth-Ann Randall, Expected: none *)
FindTextualAnswer[novel, "What is the name of the grandmother?"]
(* Result: Elizabeth-Ann Randall, Expected: Henriette Blake *)
TextCases[novel, "male"]
(* Error result *)
TextCases[novel, "female"]
(* Error result *)
It seems, with further deep-dive needed, that spaCy combined with prodigy has a working approach to solve for these questions, however before taking the leap, I hope Mathematica would have one available too.
[Update in response to m_goldberg, with appreciation] I see this as a named entity recognition (NER) problem. The goal is to receive a recommendation on approach (or a code) to train a model (e.g. a neural network) to accept input (a category) and, accounting for context (i.e. novel), provide output, such as:
Input: "male", Context: novel, Output: "William Henry Hampton the Third", "grandfather, unnamed"
Input: "boy", Context: novel, Output: "William Henry Hampton the Third"
Input: "father", Context: novel, Output: "No such character"
Input: "grandfather", Context: novel, Output: "Unnamed character"
Input: "female", Context: novel, Output: "Elizabeth-Ann Randall", "Henriette Blake"
Input: "girl", Context: novel, Output: "Elizabeth-Ann Randall"
Input: "mother", Context: novel, Output: "No such character"
Input: "grandmother", Context: novel, Output: "Henriette Blake"
In the meantime, this Mathematica tutorial regretfully does not suggest a solution: https://reference.wolfram.com/language/tutorial/NeuralNetworksSequenceLearning.html The closest approach is under the tutorial section "Simple RNN Trained on the bAbI QA Dataset", where the neural net accepts Context and a question about Context, and returns a classifier. From here, I am unable to envision how to tweak the NN architecture to accept Context and a classifier to return an answer from the Context based on the classifier.
There are also examples, regretfully not relevant, under individual neural networks in Wolfram Neural Net Repository such as here: https://resources.wolframcloud.com/NeuralNetRepository/resources/BERT-Trained-on-BookCorpus-and-Wikipedia-Data (Note: for other models see Browse by Task Type->(Feature Extraction, Language Modeling))