I am not overly familiar with the markov model, but I feel like I could lend a hand here- especially considering that there are no answers here so far.
(我对markov模型不太熟悉,但是我觉得我可以在这里伸出援手-特别是考虑到目前为止还没有答案。)
First, the code you provided:
(首先,您提供的代码:)
randomValue = Math.Random().toFixed(2)
has a couple of issues.
(有几个问题。)
The "R" in random should be lowercase, and toFixed(2) returns a string, not a number.(随机的“ R”应为小写,并且toFixed(2)返回字符串,而不是数字。)
The correct version of that line is:(该行的正确版本是:)
var randomValue = Number(Math.random().toFixed(2));
That being said, to pick the next word based purely on the highest probability, you wouldn't need to use that line of code anyway.
(话虽这么说,要完全根据最高的概率选择下一个单词,则无论如何都不需要使用该行代码。)
You'd do something line this:(您可以在此行:)
var nextWordProbabilities = [{word:"hello", prob: 0.5}, {word: "world", prob: 0.25}];
nextWordProbabilities.sort(function(a, b){
if(a.prob < b.prob)return 1;
if(a.prob > b.prob)return -1;
return 0;
});
var nextWord = nextWordProbabilities[0].word;
If you then wanted to throw in a little randomness so you didn't always end up with exactly the highest probability word, but rather possibly a word that was just close enough to the highest possibility, you could go on to then add this following that previous code block:
(如果您随后想稍微随意一点,以使您不一定总是得到恰好是最高概率的单词,而是可能单词刚好接近最高可能性,那么您可以继续在该单词后面添加上一个代码块:)
var TENDENCY_TOWARDS_MOST_PROBABLE_WORDS = .5;
for(var i = 0; i < nextWordProbabilities.length; i++){
if(Math.random() > TENDENCY_TOWARDS_MOST_PROBABLE_WORDS){
nextWord = nextWordProbabilities[i].word;
}
}
I'm also not sure how you're determining when to end a sentence.
(我也不确定您如何确定何时结束句子。)
If you're not just doing a set number of words in a row, it might be a good idea to just end the sentence when the most probable word isn't a super probable, like so:(如果您不只是连续处理一定数量的单词,那么最好在最有可能的单词不是超级概率时结束句子,这是一个好主意,例如:)
if(nextWordProbabilities[0].prob < .2){
//end the sentence
}
Hope this is helpful.
(希望这会有所帮助。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…