So,
For the while, I’ve taken out the second indirect form, as this is rare. So to concentrate on adding relativizers (eg “cat in the hat”) and the enigmatic and…
You’re right about repeaters. I used this for “and”, and it can also be used for a chain of relativizers, but recursive is slightly better here as later, each of those relative clauses need to be semantically resolved. In a chain, it’s a bit more confusing.
“and” is ok because it’s associative.
var lexicon=ISHML.Lexicon()
lexicon
lexicon
.register("the", "a", "an").as({ part: "article" })
.register("take", "steal", "grab")
.as({ key: "take", part: "verb", prepositions: ["to", "from"] })
.register("drop", "leave").as({ key: "drop", part: "verb", prepositions: [] })
.register("ring").as({ key: "ring", part: "noun", role: "thing" })
.register("slipper").as({ key: "slipper", part: "noun", role: "thing" })
.register("diamond").as({ key: "ring", part: "adjective", role: "thing" })
.register("diamond jim").as({ key: "jim", part: "noun", role: "npc" })
.register("jim").as({ key: "james", part: "noun", role: "npc" })
.register("ruby").as({ key: "ring", part: "adjective", role: "thing" })
.register("ruby").as({ key: "ruby", part: "noun", role: "thing" })
.register("ruby").as({ key: "slipper", part: "adjective", role: "thing" })
.register("glass").as({ key: "slipper", part: "adjective", role: "thing" })
.register("glass").as({ key: "tumbler", part: "noun", role: "thing" })
.register("looking glass").as({ key: "mirror", part: "noun", role: "thing" })
.register("good looking").as({ key: "tumbler", part: "adjective", role: "thing" })
.register("good").as({ key: "mirror", part: "adjective", role: "thing" })
.register("tumbler").as({ key: "tumbler", part: "noun", role: "thing" })
.register("ruby").as({ key: "miss_ruby", part: "noun", role: "npc" })
.register("sam").as({ key: "sam", part: "noun", role: "npc" })
.register("from").as({ key: "from", part: "preposition" })
.register("to").as({ key: "to", part: "preposition" })
.register("in").as({ key: "in", part: "preposition" })
.register("and").as({ key: "and", part: "conjunction" })
// a single noun with adjectives
var aNoun=ISHML.Rule()
aNoun.snip("article").snip("adjectives").snip("noun")
aNoun.article
.configure({minimum:0, filter:(definition)=>definition.part==="article" })
aNoun.adjectives
.configure(
{ minimum:0, maximum:Infinity,
filter:(definition)=>definition.part==="adjective"
})
aNoun.noun.configure({filter:(definition)=>definition.part==="noun" })
// a noun with optional relativizer
var arNoun=ISHML.Rule()
arNoun.snip("AN", aNoun.clone()).snip("relation")
arNoun.relation.snip("preposition").snip("RN",arNoun)
arNoun.relation.configure({minimum:0})
arNoun.relation.preposition
.configure({filter:(definition)=>definition.part==="preposition"})
// a list of nouns
var nouns=ISHML.Rule()
nouns.snip("ARN", arNoun.clone()).snip("andARN")
nouns.andARN.snip("and").snip("ARN", arNoun.clone())
nouns.andARN.configure({minimum:0,maximum:Infinity})
nouns.andARN.and
.configure({filter:(definition)=>definition.part==="conjunction"})
var command=ISHML.Rule()
command.snip("subject",arNoun.clone()).snip("verb").snip("object")
command.subject.configure({minimum:0})
command.verb.configure({filter:(definition)=>definition.part==="verb"})
command.object.snip("directObject",nouns.clone()).snip("indirectObject")
command.object.indirectObject.snip("preposition").snip("nouns",nouns.clone())
command.object.indirectObject.configure({minimum:0})
command.object.indirectObject.preposition
.configure({filter:(definition)=>definition.part==="preposition"})
var parser=ISHML.Parser({lexicon:lexicon,grammar:command})
// console.log(parser.analyze("take ruby from slipper"))
// console.log(parser.analyze("take ruby ring in glass slipper and diamond ring"))
//console.log(parser.analyze("take ruby from ring in slipper"))
// console.log(parser.analyze("take ruby and ring in slipper from jim"))
// missing ((ruby and ring) in slipper) from (jim)
Results:
-
“take ruby from slipper”
3 interpretations.
-
“take ruby ring in glass slipper and diamond ring”
3 interpretations
-
“take ruby from ring in slipper”
3 interpretations
-
“take ruby and ring in slipper from Jim”
3 interpretations
Unfortunately we’re missing a few. For example (4) we have already
take (ruby) and (ring in slipper) from (Jim)
take (ruby) and (ring in slipper from Jim)
take (ruby and ring) in (slipper from Jim)
but not;
take ((ruby and ring) in slipper) from (Jim)
This last case is a bit like the phrase “put the cup and saucer in the cupboard on the table.” meaning;
put the ((cup and saucer) that are in the cupboard) onto the table,
However, this is not a bug because the grammar defined above only relativizes single nouns. I tried to define half of nouns
before arNoun
, but it didn’t like it.
The problem with recursively defined grammars like this is that they become too recursive to handle.
failed attempt:
var nouns=ISHML.Rule()
// a noun with optional relativizer
var arNoun=ISHML.Rule()
arNoun.snip("AN", nouns).snip("relation")
arNoun.relation.snip("preposition").snip("RN",nouns)
arNoun.relation.configure({minimum:0})
arNoun.relation.preposition
.configure({filter:(definition)=>definition.part==="preposition"})
// a list of nouns
// var nouns=ISHML.Rule()
nouns.snip("ARN", arNoun.clone()).snip("andARN")
nouns.andARN.snip("and").snip("ARN", arNoun.clone())
nouns.andARN.configure({minimum:0,maximum:Infinity})
nouns.andARN.and
.configure({filter:(definition)=>definition.part==="conjunction"})