小编mis*_*vie的帖子

搜索数字的字符串

我有一个javascript聊天机器人,一个人可以在输入框中输入任何他们喜欢的问题,并希望得到准确的答案.我可以做到这一点,但我知道我这一切都错了,因为我不知道这个数字在句子中出现的位置.如果一个人确切输入:

什么是5的平方根这很好用.

如果他输入这样的东西,它就不会.

什么是5的平方根

5的平方根是什么

你知道5的平方根是什么吗?

等等

我需要能够确定句子中出现的数字,然后从那里进行计算.请注意,下面的行是更大的工作聊天机器人的一部分.在下面的行中,我只是试图能够回答任何平方根问题,无论数字出现在句子中的哪个位置.我也知道有一个开放式输入框有许多陷阱,一个人可以输入任何拼写错误等.这只是为了娱乐而不是一个严肃的科学项目.:)

if(
    (word[0]=="what's") &&
    (word[1]=="the") &&
    (word[2]=="square") &&
    (word[3]=="root") &&
    (word [4]=="of") &&
    (input.search(/\d{1,10}/)!=-1) &&
    (num_of_words==6)
){        
    var root= word[5];
    if(root<0){ 
        document.result.result.value = "The square root of a negative number is not possible.";
    }else{
         word[5] = Math.sqrt(root);
         word[5] = Math.round(word[5]*100)/100 
         document.result.result.value = "The square root of "+ root +" is "+ word[5] +"."; 
    }
    return true;
}
Run Code Online (Sandbox Code Playgroud)

为了清楚起见,机器人是使用"If statemments"编写的.如果在这种情况下输入不包括单词"what"和"square root"和"some number",那么该行不会触发,并且机器人会通过一般的"我不知道类型响应".所以我希望任何答案都适合我正在使用的格式.善待,我是新来的.我喜欢制作机器人,但我不是一个程序员.谢谢.

javascript math nlp square-root string-parsing

5
推荐指数
1
解决办法
734
查看次数

标签 统计

javascript ×1

math ×1

nlp ×1

square-root ×1

string-parsing ×1