If I have a document, any document, it can be a book, an article, etc, how could I extract people's names from there?
For example, if the input is an article, the output of the function would be the authors.
If I have a document, any document, it can be a book, an article, etc, how could I extract people's names from there?
For example, if the input is an article, the output of the function would be the authors.
You may use the functions in the Text Analysis guide to extract the "NounPhrase"
s containing "ProperNoun"
s. This gives a short list of proper nouns to evaluate for people names.
First collect some text that contains people's names in the form of a Wikipedia article using WikipediaData
.
articleText = WikipediaData["Garrett Morgan"];
The article's text can be separated into sentences using TextSentences
.
txtSentences = TextSentences[articleText];
Version 10.3 added the experimental function TextStructure
that breaks a text down into its grammatical structure using TextElement
s.
txtStruc = TextStructure[First@txtSentences, "ConstituentTree"]
Using the nested structure of the "ConstituentTree"
(evaluate FullForm@txtStruc
) the "NounPhrase"
s containing "ProperNoun"
s can be extracted by using Cases
.
properNounPrases =
StringRiffle /@
Cases[TextElement[w_String, _Association] :> w] /@
First /@
Cases[
TextStructure[txtSentences, "ConstituentTree"]
,
TextElement[
{TextElement[_String,
<|
"GrammaticalUnit" -> Entity["GrammaticalUnit", "ProperNoun" | "Punctuation"]
|>] ..},
<|"GrammaticalUnit" -> Entity["GrammaticalUnit", "NounPhrase"]|>
],
Infinity
];
This produces a short list of proper nouns to consider.
Multicolumn[DeleteDuplicates@properNounPrases,
Frame -> All,
FrameStyle -> LightGray,
ItemSize -> Full]
From properNounPrases
you could then continue either select by hand or compare to some database or train some net to select the names for you.
Hope this helps.