基于规则不起作用在地图内连接数组元素

时间:2018-04-13 07:40:05

标签: javascript

我试图用一个字符串分解成一个数组来修复一个问题,问题是当我不想要它们时,像Mr.等标题是分裂点

我有一个想法,使用map()重新处理数组,并尝试联接备份任何截断的元素(特别是那些结束于"先生","太太。&#34我的代码不起作用,我不确定它是否真的可以在map()中向前看并将两个数组索引连接在一起,或者如果我从根本上从错误的角度解决问题。

这是我的代码:



var origstr = "During July of this year I completed a two week work placement with Bruce and Carry Ltd, a national insurance company with an annual turnover of £24 million. I worked as an Accounting Assistant with Mr. Bench, where I was given responsibility for financial reporting and for assisting the senior credit controller by sending invoices to debtors.";

// TEXT INPUT SPLIT AND PROCESS
// Step 1: split full text input into sentence chunks
var arr1 = origstr.match(/([^\.!\?]+[\.!\?]+)|([^\.!\?]+$)/g); // split

// Step 2: check for terms like Mr. or Ltd. if so, merge back into the correct sentence
// Step 3: remove leading whitespace from array elements, return array
var arr2 = arr1.map(function (el, index) {
  if (endsWithAny(["Mr.", "Mrs.", "Ltd.", "Dept.", "I.T."], el)){
      el = [el[index],el[index+1]].join();
  }
  return el.trim();
});

// extends endswith() to work with arrays
// https://stackoverflow.com/questions/45069514/check-if-string-ends-with-any-of-multiple-characters
function endsWithAny(suffixes, string) {
    return suffixes.some(function (suffix) {
        return string.endsWith(suffix);
    });
}




1 个答案:

答案 0 :(得分:0)

要分割成句子,你可以迭代分裂的元素,并决定一个句子是否已经结束了元素或你的分裂数组,如下面的代码片段。这不包括以"?"结尾的句子。或者"!",你也必须应用它。



var text = "During July of this year I completed a two week work placement with Bruce and Carry Ltd., a national insurance company with an annual turnover of £24 million. \
I worked as an Accounting Assistant with Mr. Bench, where I was given responsIbIlIty for fInancIal reportIng and for assIstIng the senIor credIt controller by sendIng InvoIces to debtors.";

function endsWithAny(suffixes, string) {
  return suffixes.some(function(suffix) {
    return string.endsWith(suffix);
  });
}

function splitSentences(input) {
  var splitted = input.split(".");
  var sentences = [];
  var cur = "";

  splitted.forEach((part, idx) => {
    if (idx === splitted.length - 1 || !endsWithAny(["Mr", "Mr", "Ltd", "Dept", "I.T"], part)) {
      cur += part;
      if (cur) {
        sentences.push(cur.trim())
      };
      cur = "";
    } else {
      cur += part+".";
    }
  });
  return sentences;
}


console.info(splitSentences(text));