gpt4 book ai didi

javascript - 简单的语法检查程序-最优数据结构

转载 作者:行者123 更新时间:2023-12-03 08:14:51 25 4
gpt4 key购买 nike

我想创建一个简单的英语游戏来检查某人的句子。他们能够使用单词库中的一组固定单词来构建句子。

单词库可能是这样的:

[想吃,我嗨]

然后他们会按照正确的顺序造句:

嗨,我想吃东西。

我在 English SO 上问过这个问题由于它最初与语法问题有关,因此该问题已演变为更多的数据结构问题。您可以在该链接中阅读更多相关信息。我最初的想法是使用一组通用英语规则来检查句子语法,但它似乎很快就会变得过于复杂。建议我只使用硬编码检查进行匹配,如下所示。

在进一步定义这些检查之前,我想知道是否有更好的数据结构/方法可以为此目的检查语法。

  if (input === the_answer) {
msg = correct!
} else {
msg = 'Try again: ' + this.grammarRules(input, the_answer));
}

Language_System.prototype.grammarRules = function(input, answer) {

var grammar_hints = {
quest1 : {
task1 : [
'The subject, Bob, needs to be first',
'The phrase is Hello there'
]
}
};

var grammar_rules = {

quest1 : {
task1 : function (input, answer) {
var error = -1;
if (input[0] !== answer[0]) {
error = 0;
} else if (input.indexOf('hello') > input.indexOf('there')) {
error = 1;
}
return grammar_hints.quest1.task1[error];
}
}

};

return grammar_rules.Lee.quest1.task1(input, answer);

};

最佳答案

如果您考虑采用更具声明性的方法,会容易得多:- 定义标准quest结构- 使用通用输入格式定义标准task结构- 定义通用验证器并重用它们

您从 grammar_hints 对象开始就走上了正确的道路,但实际上我会将描述一个任务的所有属性放在同一个对象中。

建议:

var quests = [
{
name: 'Quest 1',
tasks: [
{
name: 'Task 1',
solution: 'hi I want to eat',
validators: [
validators.first('hi'),
validators.verbAfterNoun('want', 'I'),
]
}
],
},
];

您将能够在多个任务中重复使用大量验证器,因此您希望它们尽可能通用,下面是一个示例:

var validators = {
first: function (input, term) {
if (input[0] !== term) {
return 'The sentence needs to start with: ' + term;
}
},

verbAfterNoun: function (input, verb, noun) {
if (input.indexOf(verb) < input.indexOf(noun)) {
return 'The verb, ' + verb + ', needs to come after the noun ' + noun;
}
}
};

现在,因为您想要一种声明性格式(我实际上使用验证器的输入来初始化验证器,并将结果传递到 validators 数组中),所以我们需要一个采用泛型的验证器工厂验证器并返回一个仅可与输入一起重复使用的辅助方法。这将帮助我们继续下去,这样我们的测试框架就不需要知道要传递给每个验证器回调的输入数量

// This is a factory method that applies the given callback (with the given arguments)
function makeValidator (fn) {
return function inputFN () {
var args = [].slice.call(arguments);
return function validate (input) {
return fn.apply(null, [input].concat(args));
}
}
}

// Apply the makeValidator() method on all the validators
for (var key in validators) {
validators[key] = makeValidator(validators[key]);
}

最后,我们还需要一种标准方法来根据输入检查我们的任务:

// This method provides the generic validation framework for any task given any input
function validate (task, input) {
var wordList = input.split(' ');

if (input === task.solution) return {success: true, errors: []};

var errors = [];
task.validators.forEach(function (fn) {
var error = fn(wordList);
if (error) errors.push(error);
});
return {success: false, errors: errors};
}

还有一些例子:

var task = quests[0].tasks[0];
console.log(validate(task, 'hi I want to eat'));
console.log(validate(task, 'I want to eat hi'));
console.log(validate(task, 'hi want I to eat'));
console.log(validate(task, 'want I to eat hi'));

把它们放在一起:

// This is a factory method that applies the given callback (with the given arguments)
function makeValidator (fn) {
return function inputFN () {
var args = [].slice.call(arguments);
return function validate (input) {
return fn.apply(null, [input].concat(args));
}
}
}

var validators = {
first: function (input, term) {
if (input[0] !== term) {
return 'The sentence needs to start with: ' + term;
}
},

verbAfterNoun: function (input, verb, noun) {
if (input.indexOf(verb) < input.indexOf(noun)) {
return 'The verb, ' + verb + ', needs to come after the noun ' + noun;
}
}
};

// Apply the makeValidator() method on all the validators
for (var key in validators) {
validators[key] = makeValidator(validators[key]);
}

var quests = [
{
name: 'Quest 1',
tasks: [
{
name: 'Task 1',
solution: 'hi I want to eat',
validators: [
validators.first('hi'),
validators.verbAfterNoun('want', 'I'),
]
}
],
},
];

// This method provides the generic validation framework for any task given any input
function validate (task, input) {
var wordList = input.split(' ');

if (input === task.solution) return {success: true, errors: []};

var errors = [];
task.validators.forEach(function (fn) {
var error = fn(wordList);
if (error) errors.push(error);
});
return {success: false, errors: errors};
}

function printTask (input) {
var task = quests[0].tasks[0];
var result = validate(task, input);
document.body.innerHTML += '<div><b>checking:</b> ' + input + '<pre>' + JSON.stringify(result, null, 4) + '</pre><hr />';
}

// Lets look at some examples
printTask('I want to eat hi');
printTask('hi want I to eat');
printTask('want I to eat hi');
printTask('hi I want to eat');

关于javascript - 简单的语法检查程序-最优数据结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33989897/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com