将字符串拆分成对、三元组、四元组和 on (ngrams)?

Split string into pairs, triplets, quadruplets and on (ngrams)?

我想将自然文本拆分为词对、三元组、四元组等等!

到目前为止,我已经想出了如何分成两对。我假设我需要一个额外的循环来容纳单词 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);

添加预期输出...(ngram 数组)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

您可以使用像 lodash 这样的库来显着缩短您的代码:

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

然后您可以根据需要传入除 2 以外的值

假设您想要的结果不包括乱序组合,您可以尝试以下

// 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);

这称为 "n-grams",可以在现代 JavaScript 中使用 generators 像这样完成:

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));

这应该能满足您的需求:

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));