Function Repository Resource:

SentenceConstituents

Source Notebook

Parse a sentence into its constituents

Contributed by: Mark Greenberg

ResourceFunction["SentenceConstituents"]["sent"]

parses the sentence "sent" into its constituents.

Details

ResourceFunction["SentenceConstituents"] currently only works for English sentences.
SentenceConstituents returns an Association in the form <|"type1"{con1,con2,},}|>.
If "sent" is too complex, ResourceFunction["SentenceConstituents"] will return a Failure object.
ResourceFunction["SentenceConstituents"]["sent"] expects exactly one sentence. If "sent" contains multiple sentences, only the first one is parsed.
Sentence fragments are treated as complete sentences.
The returned Association is sorted alphabetically by its keys.
The lists of constituents preserve the order of the constituents appear in original sentence.

Examples

Basic Examples (1) 

Get the constituents of a sentence:

In[1]:=
ResourceFunction[
 "SentenceConstituents"]["Anna fed chicken meat to her dog for an entire week."]
Out[1]=

Scope (2) 

The sentence does not need to be in the indicative mood; here is an example of the interrogative mood:

In[2]:=
ResourceFunction[
 "SentenceConstituents"]["Who will be cleaning the courtyard this afternoon?"]
Out[2]=

Use a sentence in the imperative mood:

In[3]:=
ResourceFunction[
 "SentenceConstituents"]["Go to the store on the corner to buy some licorice, please."]
Out[3]=

Applications (1) 

Extract all the prepositional phrases from some text:

In[4]:=
text = TextSentences[ExampleData[{"Text", "AliceInWonderland"}]][[
   20 ;; 25]];
Merge[ResourceFunction["SentenceConstituents"] /@ text, Flatten][
  "PrepositionalPhrase"] // Column
Out[5]=

Properties and Relations (2) 

TextStructure produces the tree, graph, or string version of the entire sentence's nested structure:

In[6]:=
text = "Aldo knew when to quit.";
Table[TextStructure[text, a], {a, {"ConstituentTree", "ConstituentGraphs", "ConstituentStrings"}}] // Column
Out[7]=

SentenceConstituents distills out the constituents into lists, grouping together all the clauses, noun phrases, etc.:

In[8]:=
ResourceFunction["SentenceConstituents"][text]
Out[8]=

Possible Issues (2) 

Sentences of extreme complexity can overload SentenceConstituents. In those cases, the function returns a Failure object:

In[9]:=
ResourceFunction[
 "SentenceConstituents"]@"In the event that the Purchaser defaults in the payment of any instalment of purchase price, taxes, insurance, interest, or the annual charge described elsewhere herein, or shall default in the performance of any other obligations set forth in this Contract, the Seller may: at his option: (a) Declare immediately due and payable the entire unpaid balance of purchase price, with accrued interest, taxes, and annual charge, and demand full payment thereof, and enforce conveyance of the land by termination of the contract or according to the terms hereof, in which case the Purchaser shall also be liable to the Seller for reasonable attorney's fees for services rendered by any attorney on behalf of the Seller, or (b) sell said land and premises or any part thereof at public auction, in such manner, at such time and place, upon such terms and conditions, and upon such public notice as the Seller may deem best for the interest of all concerned, consisting of advertisement in a newspaper of general circulation in the county or city in which the security property is located at least once a week for Three (3) successive weeks or for such period as applicable law may require and, in case of default of any purchaser, to re-sell with such postponement of sale or resale and upon such public notice thereof as the Seller may determine, and upon compliance by the Purchaser with the terms of sale, and upon judicial approval as may be required by law, convey said land and premises in fee simple to and at the cost of the Purchaser, who shall not be liable to see to the application of the purchase money; and from the proceeds of the sale: First to pay all proper costs and charges, including but not limited to court costs, advertising expenses, auctioneer's allowance, the expenses, if any required to correct any irregularity in the title, premium for Seller's bond, auditor's fee, attorney's fee, and all other expenses of sale occurred in and about the protection and execution of this contract, and all moneys advanced for taxes, assessments, insurance, and with interest thereon as provided herein, and all taxes due upon said land and premises at time of sale, and to retain as compensation a commission of five percent (5%) on the amount of said sale or sales; SECOND, to pay the whole amount then remaining unpaid of the principal of said contract, and interest thereon to date of payment, whether the same shall be due or not, it being understood and agreed that upon such sale before maturity of the contract the balance thereof shall be immediately due and payable; THIRD, to pay liens of record against the security property according to their priority of lien and to the extent that funds remaining in the hands of the Seller are available; and LAST, to pay the remainder of said proceeds, if any, to the vendor, his heirs, personals representatives, successors or assigns upon the delivery and surrender to the vendee of possession of the land and premises, less costs and excess of obtaining possession."
Out[9]=

SentenceConstituents relies on AI to parse the sentence. It occasionally makes mistakes:

In[10]:=
ResourceFunction["SentenceConstituents"]["Call me Ishmael."]
Out[10]=

Publisher

Mark Greenberg

Requirements

Wolfram Language 13.0 (December 2021) or above

Version History

  • 1.0.0 – 17 January 2024

License Information