130 lines
5.4 KiB
JavaScript
130 lines
5.4 KiB
JavaScript
|
const ishml = require('../ishml')
|
|||
|
const assert = require('chai').assert
|
|||
|
|
|||
|
before(function() {
|
|||
|
var lexicon = ishml.Lexicon()
|
|||
|
lexicon
|
|||
|
.register("взять")
|
|||
|
.as({ key: "take", part: "verb", prepositions: ["у"] })
|
|||
|
.register("отдать")
|
|||
|
.as({ key: "give", part: "verb", prepositions: [] })
|
|||
|
.register("кольцо")
|
|||
|
.as({ key: "ring", part: "noun", role: "thing" })
|
|||
|
.register("рубиновое")
|
|||
|
.as({ key: "ring", part: "adjective", role: "thing" })
|
|||
|
.register("мужчина", "мужчины", "мужчине")
|
|||
|
.as({ key: "james", part: "noun", role: "npc" })
|
|||
|
.register("женщина", "женщине")
|
|||
|
.as({ key: "violet", part: "noun", role: "npc" })
|
|||
|
.register("у", "от")
|
|||
|
.as({ key: "from", part: "preposition" })
|
|||
|
.register("и",",",";")
|
|||
|
.as({ part: "conjunction" })
|
|||
|
.register("его", "её", "ему")
|
|||
|
.as({ part: "pronoun" })
|
|||
|
|
|||
|
// ENGLISH
|
|||
|
|
|||
|
.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("wear", "put on").as({ key: "wear", 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" })
|
|||
|
|
|||
|
|
|||
|
//Create a set of nested rules which mirror the wanted syntax tree.
|
|||
|
var grammar = ishml.Rule()
|
|||
|
grammar.snip("verbs").snip("nounPhrase").snip("conjunction")
|
|||
|
grammar.nounPhrase.snip("article").snip("adjectives").snip("noun")
|
|||
|
|
|||
|
//Configure behavior of some of the rules with .configure().
|
|||
|
|
|||
|
grammar.verbs.configure({
|
|||
|
minimum: 0,
|
|||
|
maximum: Infinity,
|
|||
|
filter: (definition) => definition.part === "verb"
|
|||
|
})
|
|||
|
|
|||
|
grammar.nounPhrase.article.configure({
|
|||
|
minimum: 0,
|
|||
|
filter: (definition) => definition.part === "article"
|
|||
|
})
|
|||
|
|
|||
|
grammar.nounPhrase.adjectives.configure({
|
|||
|
minimum: 0,
|
|||
|
maximum: Infinity,
|
|||
|
filter: (definition) => definition.part === "adjective"
|
|||
|
})
|
|||
|
|
|||
|
//Create a parser
|
|||
|
this.parser = ishml.Parser({ lexicon, grammar })
|
|||
|
})
|
|||
|
|
|||
|
describe('Парсер', function() {
|
|||
|
it('должен понимать простые команды', function() {
|
|||
|
const input = "взять кольцо"
|
|||
|
const output = this.parser.analyze(input)
|
|||
|
const interpretation = output.interpretations[0].gist
|
|||
|
|
|||
|
assert.notEmpty(interpretation.verbs)
|
|||
|
assert.equal('взять', interpretation.verbs[0].lexeme)
|
|||
|
assert.equal('перчатки', interpretation.nounPhrase.noun.lexeme)
|
|||
|
})
|
|||
|
|
|||
|
it('should understand simple commands', function() {
|
|||
|
const input = "take ring"
|
|||
|
const output = this.parser.analyze(input)
|
|||
|
const interpretation = output.interpretations[0].gist
|
|||
|
|
|||
|
assert.notEmpty(interpretation.verbs)
|
|||
|
assert.equal('take', interpretation.verbs[0].lexeme)
|
|||
|
assert.equal('ring', interpretation.nounPhrase.noun.lexeme)
|
|||
|
})
|
|||
|
|
|||
|
it('должен понимать сложные команды', function() {
|
|||
|
const input = "взять рубиновое кольцо у женщины и отдать его мужчине"
|
|||
|
const output = this.parser.analyze(input)
|
|||
|
const interpretation = output.interpretations[0].gist
|
|||
|
console.log(interpretation)
|
|||
|
|
|||
|
assert.notEmpty(interpretation.verbs)
|
|||
|
assert.equal('взять', interpretation.verbs[0].lexeme)
|
|||
|
assert.notEmpty(interpretation.nounPhrase.adjectives)
|
|||
|
assert.equal('отдать', interpretation.verbs[0].lexeme)
|
|||
|
assert.equal('рубиновые', interpretation.nounPhrase.adjectives[0].lexeme)
|
|||
|
assert.equal('перчатки', interpretation.nounPhrase.noun.lexeme)
|
|||
|
})
|
|||
|
|
|||
|
it('should understand complex commands', function() {
|
|||
|
const input = "take the ruby slipper from woman and give it to him"
|
|||
|
const output = this.parser.analyze(input)
|
|||
|
const interpretation = output.interpretations[0].gist
|
|||
|
console.log(interpretation)
|
|||
|
|
|||
|
assert.notEmpty(interpretation.verbs)
|
|||
|
assert.equal('take', interpretation.verbs[0].lexeme)
|
|||
|
assert.equal('give', interpretation.verbs[1].lexeme)
|
|||
|
assert.notEmpty(interpretation.nounPhrase.adjectives)
|
|||
|
assert.equal('ruby', interpretation.nounPhrase.adjectives[0].lexeme)
|
|||
|
assert.equal('slipper', interpretation.nounPhrase.noun.lexeme)
|
|||
|
})
|
|||
|
});
|