Tuesday, December 27, 2022

Discover Natural Language Processing with the help of ChatGPT

The latest generation of machine learning is of magical education value:

  • They can "show you that something is possible".
  • They are quick: New concepts can be explored in seconds.
  • They are very powerful.
Best to show by example. This post-Xmas 2022 week I am refreshening my natural language processing skills. Therefore I start my learning sessions with the following request:

Can you tell me the little red riding hood story while including the phrase structure name and the most important phrase structure properties of each phrase structure of the story.

https://chat.openai.com/chat responds with: 

Sure! Here is the story of Little Red Riding Hood, with the phrase structure name and the most important phrase structure properties included for each phrase structure:
Once upon a time, there was a little girl (noun phrase) who lived in a village (noun phrase) near the forest (noun phrase). She had a red hooded cloak (noun phrase), which she always wore (verb phrase, finite, past tense) on her daily walks (noun phrase) to visit her grandmother (noun phrase) who lived on the other side of the forest (prepositional phrase, place).
One day, her mother (noun phrase) said (verb phrase, finite, past tense) to her (verb phrase, finite, past tense, indirect object), "Take (verb phrase, imperative mood) this basket (noun phrase) of goodies (noun phrase) to your grandmother (verb phrase, finite, present tense, indirect object). She is not feeling well (adjective phrase) today (adverb phrase, time)."
The little girl (noun phrase) set off (verb phrase, finite, past tense) through the forest (prepositional phrase, place), and on the way (adverb phrase, time), she encountered (verb phrase, finite, past tense) a wolf (noun phrase). "Where (adverb phrase) are you going (verb phrase, present tense), little girl (verb phrase, present tense, direct object)?" the wolf (noun phrase) asked (verb phrase, finite, past tense)...

I cannot remember what are the typical phrase properties. So I ask, and get the following phrase structure properties:

  • Head: the central word or group of words in a phrase that gives the phrase its main meaning
  • Modifier: a word or group of words that provides additional information about the head of a phrase
  • Function: the grammatical role that a phrase plays in a sentence
  • Complementation: the addition of a complement (a word or group of words that is necessary to complete the meaning of a verb) to a verb phrase
  • Dependency: the relationship between one phrase and another, in which one phrase relies on or depends on the other for its meaning
  • Voice: the form of a verb that indicates whether the subject of the verb is performing the action or is the recipient of the action
  • Tense: the form of a verb that indicates the time when the action described by the verb takes place
  • Mood: the form of a verb that indicates the attitude or mode of the speaker or writer toward the action described by the verb
  • Finiteness: the property of a verb phrase that indicates whether it is complete or incomplete
  • Agreement: the correspondence in number, person, or gender between the subject and verb of a sentence
  • Person: the form of a verb that indicates the perspective of the speaker or writer (first person, second person, or third person)
  • Number: the form of a verb or pronoun that indicates whether it refers to one or more than one person or thing
  • Gender: the form of a pronoun or noun that indicates whether it refers to a male or female
  • Case: the form of a noun or pronoun that indicates its grammatical function in a sentence (subject, object, possessive, etc.)
  • Aspect: the form of a verb that indicates the extent or completeness of the action described by the verb
  • Formality: the level of politeness or formality in the language used
  • Emphasis: the focus or emphasis given to a particular word or phrase in a sentence
  • Syntactic role: the function of a phrase in the sentence
  • Negation: the presence or absence of a word or phrase that indicates the opposite or negation of the action of the verb
  • Emotion: the expression of feelings or emotions in the language used
  • Quantity: the amount or quantity of something mentioned in the phrase
  • Quality: the characteristic or quality of something mentioned in the phrase
  • Condition: the state or condition of something mentioned in the phrase
  • Reason: the cause or reason for something mentioned in the phrase
  • Manner: the way in which something is done or happens mentioned in the phrase
  • Place: the location or place where something is or happens mentioned in the phrase
  • Time: the point in time when something is or happens mentioned in the phrase
  • Comparison: the comparison of one thing to another mentioned in the phrase
  • Possession: the ownership or possession of something mentioned in the phrase
  • Purpose: the intended or desired result or purpose of something mentioned in the phrase
I then do the same for the phrase structures. Here is a descriptions and their major properties of the first major fifteen:
  1. Noun phrase: a phrase that functions as a noun in a sentence and consists of a head noun and any modifiers. Major properties: head, modifier, function, agreement (person, number, gender, case).
  2. Adjective phrase: a phrase that functions as an adjective in a sentence and consists of an adjective as the head and any modifiers. Major properties: head, modifier, function
  3. Adverb phrase: a phrase that functions as an adverb in a sentence and consists of an adverb as the head and any modifiers. Major properties: head, modifier, function.
  4. Verb phrase: a phrase that consists of a verb and any modifiers or complements and functions as the predicate of a sentence. Major properties: head, modifier, complementation, function, tense, mood, finiteness, agreement (person, number).
  5. Prepositional phrase: a phrase that consists of a preposition, its object (a noun or pronoun), and any modifiers of the object, and functions as an adjective or adverb in a sentence. Major properties: head, object, modifier, function.
  6. Infinitive phrase: a phrase that consists of an infinitive (the base form of a verb preceded by "to") and any modifiers or complements and functions as a noun, adjective, or adverb in a sentence. Major properties: head, modifier, complementation, function.
  7. Gerund phrase: a phrase that consists of a gerund (a verb form ending in "-ing" that functions as a noun) and any modifiers or complements and functions as a subject, direct object, subject complement, or object of a preposition in a sentence. Major properties: head, modifier, complementation, function.
  8. Participle phrase: a phrase that consists of a participle (a verb form ending in "-ing," "-ed," or "-en" that functions as an adjective) and any modifiers and functions as an adjective in a sentence. Major properties: head, modifier, function.
  9. Appositive phrase: a noun or noun phrase that renames or explains another noun or noun phrase, and is set off by commas or parentheses. Major properties: head, modifier, function.
  10. Absolute phrase: a noun phrase, participle phrase, or adjective phrase that modifies the whole sentence, and is not directly connected to the rest of the sentence. Major properties: head, modifier, function.
  11. Participial phrase: a phrase that consists of a participle and any modifiers and functions as an adjective in a sentence. Major properties: head, modifier, function.
  12. Prepositional object phrase: a phrase that consists of a noun or pronoun and any modifiers and functions as the object of a preposition. Major properties: head, modifier, function.
  13. Subject complement phrase: a phrase that follows a linking verb and complements the subject of the sentence by renaming or describing it. Major properties: head, modifier, function.
  14. Object complement phrase: a phrase that follows a direct object and complements it by renaming or describing it. Major properties: head, modifier, function.
  15. Adverbial phrase: a phrase that functions as an adverb in a sentence and consists of an adverb as the head and any modifiers. Major properties: head, modifier, function.
For inspiration, I ask for an ascii text representation. (I bet that using ASCII is not very modern!). Which produces:

Once upon a time | noun phrase | there was | verb phrase / \ finite past / \ a little girl / | \ | \ a an the little | | noun noun | | who who / | \ | \ lived in a village | | | noun noun noun | | | near the near | | noun noun | forest | noun | she | noun | had | verb | a | noun | red | adjective | hooded | adjective | cloak | noun

                                             ... 

Then I ask for a Prolog representation:

% Characters
character(little_red_riding_hood).
character(wolf).
character(grandmother).

% Relationships
relationship(little_red_riding_hood, grandmother, granddaughter).

% Locations
location(forest).
location(path).
location(grandmother's_house).

% Actions
action(little_red_riding_hood, start_journey, forest).
action(little_red_riding_hood, go_through, path).
action(little_red_riding_hood, arrive_at, grandmother's_house).
action(wolf, start_journey, forest).
action(wolf, go_through, path).
action(wolf, arrive_at, grandmother's_house).
action(wolf, disguise_as, grandmother).
action(wolf, eat, grandmother).

% Items
item(basket).
item(cake).
item(wine).

% Facts
has(little_red_riding_hood, basket).
contains(basket, cake).
contains(basket, wine).
A which point I noted that this is the 'fabric' of the little red riding hood story, but much is missing. So I ask the machine learning to produce an alternative Prolog version that captures an object centric view and the emotional tension of the story. Which it produce promptly!
% Objects
object(little_red_riding_hood).
object(wolf).
object(grandmother).
object(basket).
object(cake).
object(wine).
object(forest).
object(path).
object(grandmother's_house).

% Object properties
property(little_red_riding_hood, human).
property(wolf, animal).
property(grandmother, human).
property(basket, container).
property(cake, food).
property(wine, drink).
property(forest, nature).
property(path, nature).
property(grandmother's_house, building).

% Object relationships
relationship(little_red_riding_hood, has, basket).
contains(basket, cake).
contains(basket, wine).

% Emotional tension
emotion(little_red_riding_hood, excited, start_journey).
emotion(little_red_riding_hood, worried, arrive_at_grandmother's_house).
emotion(wolf, hungry, start_journey).
emotion(wolf, satisfied, eat_grandmother).
Hmmm... that is pretty good. The last thing we need is a bit more temporal and spatial details. Which ChatGPT produces as:
% Temporal dependencies
before(start_journey, go_through).
before(go_through, arrive_at).

% Spatial dependencies
adjacent(forest, path).
adjacent(path, grandmother's_house).
Finally, we can ask for a few query examples, which ChatGPT produces as:
Who is Little Red Riding Hood's grandmother?
?- relationship(little_red_riding_hood, Grandmother, granddaughter).

What is in Little Red Riding Hood's basket?
?- has(little_red_riding_hood, Basket), contains(Basket, Item).

What is the emotional state of Little Red Riding Hood when she arrives at the Grandmother's house?
?- emotion(little_red_riding_hood, Emotion, arrive_at_grandmother's_house).

What actions must Little Red Riding Hood take before arriving at the Grandmother's house?
?- before(Action, arrive_at).

What locations are adjacent to the forest?
?- adjacent(forest, Location).
PS: Prolog because it is compact and easy to express in production with a monadic FP style.
PS: In the first version of this post, ChatGPT produced incompatible snippets, but as I tried to clean up the post's style, the new snippets all fit together, and the post was updated.

James Litsios, 2022.

No comments: