Natural Language Parser

by gtoal
  scripts
  sprites
See inside
Instructions

Enter a sentence using one choice from each of the three lines. Only grammatically valid combinations will be accepted. (Don't enter any punctuation for now.) For example: i am happy
Defining a grammar rule with a name starting in $ causes a broadcast of that name if the statement is successfully parsed. This makes adding simple natural language commands to your own programs quite easy. (See the 'happy hat' sprite for an example)

See https://scratch.mit.edu/discuss/topic/173919/?page=1#post-1675278 for details of how to use this in your own programs.

Notes and Credits

IF YOU WANT TO USE THIS START FROM https://scratch.mit.edu/projects/92272269/ INSTEAD. NEWER PARSER.

This is a table-driven parser for natural language experiments, such as a chatbot, or a safe chat mechanism, or for language learning, or automatic language translation, or a dungeon adventure ... or something entirely different that I haven't even thought of.

So far I've built a table for a simple grammar and a parser that returns success or failure and issues broadcasts to perform semantic actions. There's also a crude analysis record/parse tree but it's not very useful yet.

Backtracking is implemented which permits more complex grammars to be used. (I'll document this in the forums soon)

The parser is quite simple and uses top-down recursive descent. Currently it is token based but it could be modified to be character based with a few small changes.

The parser is similar in structure to that of Tony Brooker's original Compiler-Compiler.

The demo grammar used here is:

SENTENCE <SUBJECT+VERB> <ADJ>

SUBJECT+VERB <FIRST-PERSON> | <SECOND-PERSON> | <THIRD-PERSON>

FIRST-PERSON <FP-SINGULAR> | <FP-PLURAL>

SECOND-PERSON YOU <SP-VERB>

THIRD-PERSON <TP-SINGULAR> | <TP-PLURAL>

FP-SINGULAR I <FP-SINGULAR-VERB>

FP-SINGULAR-VERB <FP-SING-PRESENT> | <FP-SING-PAST>

FP-PLURAL WE <FP-PLURAL-VERB>

FP-PLURAL-VERB <FP-PLURAL-PRESENT> | <FP-PLURAL-PAST>

FP-SING-PRESENT AM | FEEL

FP-SING-PAST WAS | FELT

FP-PLURAL-PRESENT ARE | FEEL

FP-PLURAL-PAST WERE | FELT

SP-VERB <SP-PRESENT> | <SP-PAST>

SP-PRESENT ARE | FEEL

SP-PAST WERE | FELT

TP-SINGULAR HE <TP-SINGULAR-VERB> | SHE <TP-SINGULAR-VERB>

TP-PLURAL THEY <TP-PLURAL-VERB>

TP-SINGULAR-VERB <TP-SING-PRESENT> | <TP-SING-PAST>

TP-PLURAL-VERB <TP-PLURAL-PRESENT> | <TP-PLURAL-PAST>

TP-SING-PRESENT IS | FEELS

TP-SING-PAST WAS | FELT

TP-PLURAL-PRESENT ARE | FEEL

TP-PLURAL-PAST WERE | FELT

ADJ HUNGRY | THIRSTY | HAPPY <$HAPPY> | SAD <$SAD> | PLEASED | TIRED | EXCITED

For example "she is happy" will parse, but "she are happy" will not.

The $HAPPY and $SAD broadcasts cause Scratch the Cat to put on or take off his Happy Hat whenever they are triggered.

Shared: 18 Dec 2015 Modified: 4 Jun 2017
Favorite this project 13
Love this project 20
Total views 243
View the remix tree  1
  
More projects by gtoal