将字符串分成对,三重态,四倍体以及(ngrams)



我想将自然文本分为单词对,三重态,四弦和triplets等!

我已经弄清楚了如何分成对远。我认为我需要一个额外的循环来容纳单词count

这是对的代码

var test = "I love you so much, but Joe said "he doesn't"!";
var words = test.split(" ");
var two_words = [];
for (var i = 0; i < words.length - 1; i++) {
  two_words.push(words[i] + ' ' + words[i + 1]);
}
console.log(two_words);
// Here is what I am trying
var words = test.split(" ");
var split_words = [];
var split_length = 5;
for (var l = 2; l <= split_length; l++) {
  for (var i = 0; i < words.length - (l - 1); i++) {
    var split_word;
    for (c = 0; c <= l; c++) {
      split_word += split_words[i + c];
    }
    split_words.push(split_word);
  }
}
console.log(split_words);

添加预期输出...(ngrams的一个数组(SG像这样

// 2grams
"I love"
"love you"
"you so"
"so much,"
"much, but"
"but Joe"
"Joe said"
"said "he"
""he doesn't"!"
//3grams
"I love you"
"love you so"
"you so much"
"so much, but"
//and on and on

这称为" n-grams",可以在现代JavaScript中使用这样的生成器进行:

function* ngrams(a, n) { 
    let buf = [];
    for (let x of a) {
        buf.push(x);
        if (buf.length === n) {
            yield buf;
            buf.shift();
        }
    }
}
var test = "The quick brown fox jumps over the lazy dog";
for (let g of ngrams(test.split(' '), 3))
    console.log(g.join(' '))

另一个更简洁,可能更快的选项:

let ngrams = (a, n) => a.slice(0, 1 - n).map((_, i) => a.slice(i, i + n));

假设您所需的结果不包括混合订单组合,您可以尝试遵循

// Code goes here
var test = "I love you so much, but Joe said "he doesn't"!";
var arr = test.split(" ");
var words = arr.length; // total length of words
var result = [];
function process(arr, length) { // process array for number of words
  var temp = [];
  // use equal if want to include the complete string as well in the array
  if (arr.length >= length) { 
    // the check excludes any left over words which do not meet the length criteria 
    for (var i = 0; (i + length) <= arr.length; i++) { 
      temp.push(arr.slice(i, length + i).join(" "));
    }
    result.push(temp);
    process(arr, length + 1); // recursive calling
  }
}
process(arr, 2);
console.log(result);

这应该做您要寻找的事情:

function chunkIt(str,chunk) {
  var words = str.split(" ");
  var arr = [];
  for (var i = (chunk - 1); i < words.length; i++) {
    var start = i - (chunk - 1);
    arr.push(words.slice(start, start + chunk));
  }
  return arr.map(v => v.join(" "));
}  
var test = "I love you so much, but Joe said "he doesn't"!";
console.log(chunkIt(test,2));
console.log(chunkIt(test,3));
console.log(chunkIt(test,4));

您可以使用诸如lodash之类的库大大缩短代码:

var word = 'foobarbaz';
var chunks = _.chunk(word, 2).map((chunk) => chunk.join(''));
console.log(chunks); //[ 'fo', 'ob', 'ar', 'ba', 'z' ]

然后,您可以传递2个以外的值以适合您的需求

最新更新