Javascript(jQuery)删除长文本的最后一句

ost*_*oon 6 javascript jquery sentence

我正在寻找一个足够智能的javascript函数来删除一长段文本的最后一句(实际上是一段).一些示例文本显示复杂性:

<p>Blabla, some more text here. Sometimes <span>basic</span> html code is used but that should not make the "selection" of the sentence any harder! I looked up the window and I saw a plane flying over. I asked the first thing that came to mind: "What is it doing up there?" She did not know, "I think we should move past the fence!", she quickly said. He later described it as: "Something insane."</p>
Run Code Online (Sandbox Code Playgroud)

现在我可以拆分.并删除数组的最后一个条目,但这对于以?或结尾的句子不起作用,!并且某些句子以引号结束something: "stuff."

function removeLastSentence(text) {
  sWithoutLastSentence = ...; // ??
  return sWithoutLastSentence;
}
Run Code Online (Sandbox Code Playgroud)

这该怎么做?什么是正确的算法?

编辑 - 通过长文本我的意思是段落中的所有内容和句子我的意思是一个实际的句子(不是一行),所以在我的例子中最后一句是:He later described it as: "Something insane."当删除那一个时,下一个是She did not know, "I think we should move past the fence!", she quickly said."

MyS*_*eam 3

定义规则: // 1. 句子以大写字母开头 // 2. 句子前面什么都没有或 [.!?],但不是 [,:;] // 3. 句子前面可以有引号如果格式不正确,例如 ["'] // 4. 如果引号后面的单词是名称,则在这种情况下句子可能不正确

还有其他规则吗?

定义你的目的: // 1. 删除最后一句

假设: 如果您从文本字符串中的最后一个字符开始向后操作,那么您会将句子的开头识别为: 1. 该字符之前的文本字符串是 [.?!] 或 2. 字符串字符之前的文本为 ["'],且前面有一个大写字母 3. 每个 [.] 前面都有一个空格 4. 我们不会针对 html 标签进行更正 5. 这些假设并不可靠,需要进行调整经常

可能的解决方案: 读入您的字符串并将其拆分为空格字符,以便为我们提供要反向查看的字符串块。

var characterGroups = $('#this-paragraph').html().split(' ').reverse();
Run Code Online (Sandbox Code Playgroud)

如果你的字符串是:

Blabla,这里还有一些文字。有时会使用基本的 html 代码,但这不应该使句子的“选择”变得更加困难!我抬头望窗,看到一架飞机飞过。我第一个想到的就是:“它在那儿做什么?” 她不知道,“我认为我们应该越过栅栏!”,她很快说道。他后来将其描述为:“有些疯狂的事情。”

var originalString = 'Blabla, some more text here. Sometimes <span>basic</span> html code is used but that should not make the "selection" of the sentence any harder! I looked up the window and I saw a plane flying over. I asked the first thing that came to mind: "What is it doing up there?" She did not know, "I think we should move past the fence!", she quickly said. He later described it as: "Something insane."';
Run Code Online (Sandbox Code Playgroud)

那么你的数组characterGroups将是:

    ["insane."", ""Something", "as:", "it", "described", "later", "He",
 "said.", "quickly", "she", "fence!",", "the", "past", "move", "should", "we",
 "think", ""I", "know,", "not", "did", "She", "there?"", "up", "doing", "it",
 "is", ""What", "mind:", "to", "came", "that", "thing", "first", "the", "asked",
 "I", "over.", "flying", "plane", "a", "saw", "I", "and", "window", "the", "up",
 "looked", "I", "harder!", "any", "sentence", "the", "of", ""selection"", "the",
 "make", "not", "should", "that", "but", "used", "is", "code", "html", "basic",
 "Sometimes", "here.", "text", "more", "some", "Blabla,"]
Run Code Online (Sandbox Code Playgroud)

注意: '' 标签和其他标签将使用 jQuery 中的 .text() 方法删除

每个块后面都有一个空格,因此当我们确定了句子的起始位置(通过数组索引)时,我们将知道该空格的索引,并且我们可以在空格从末尾占据该索引的位置拆分原始字符串的句子。

给我们自己一个变量来标记我们是否找到它,以及一个变量来保存我们确定为保存最后一个句子开头的数组元素的索引位置:

var found = false;
var index = null;
Run Code Online (Sandbox Code Playgroud)

循环遍历数组并查找以 [.!?] 结尾或以 " 结尾的任何元素,其中前一个元素以大写字母开头。

var position     = 1,//skip the first one since we know that's the end anyway
    elements     = characterGroups.length,
    element      = null,
    prevHadUpper = false,
    last         = null;

while(!found && position < elements) {
    element = characterGroups[position].split('');

    if(element.length > 0) {
       last = element[element.length-1];

       // test last character rule
       if(
          last=='.'                      // ends in '.'
          || last=='!'                   // ends in '!'
          || last=='?'                   // ends in '?'
          || (last=='"' && prevHadUpper) // ends in '"' and previous started [A-Z]
       ) {
          found = true;
          index = position-1;
          lookFor = last+' '+characterGroups[position-1];
       } else {
          if(element[0] == element[0].toUpperCase()) {
             prevHadUpper = true;
          } else {
             prevHadUpper = false;
          }
       }
    } else {
       prevHadUpper = false;
    }
    position++;
}
Run Code Online (Sandbox Code Playgroud)

如果您运行上面的脚本,它将正确地将“He”识别为最后一个句子的开头。

console.log(characterGroups[index]); // He at index=6
Run Code Online (Sandbox Code Playgroud)

现在您可以运行之前的字符串:

var trimPosition = originalString.lastIndexOf(lookFor)+1;
var updatedString = originalString.substr(0,trimPosition);
console.log(updatedString);

// Blabla, some more text here. Sometimes <span>basic</span> html code is used but that should not make the "selection" of the sentence any harder! I looked up the window and I saw a plane flying over. I asked the first thing that came to mind: "What is it doing up there?" She did not know, "I think we should move past the fence!", she quickly said.
Run Code Online (Sandbox Code Playgroud)

再次运行并得到: Blabla,这里还有一些文本。有时会使用基本的 html 代码,但这不应该使句子的“选择”变得更加困难!我抬头望窗,看到一架飞机飞过。我第一个想到的就是:“它在那儿做什么?”

再次运行并得到: Blabla,这里还有一些文本。有时会使用基本的 html 代码,但这不应该使句子的“选择”变得更加困难!我抬头望窗,看到一架飞机飞过。

再次运行并得到: Blabla,这里还有一些文本。有时会使用基本的 html 代码,但这不应该使句子的“选择”变得更加困难!

再次运行并得到: Blabla,这里还有一些文本。

再次运行并得到: Blabla,这里还有一些文本。

那么,我认为这符合您正在寻找的内容?

作为一个函数:

function trimSentence(string){
    var found = false;
    var index = null;

    var characterGroups = string.split(' ').reverse();

    var position     = 1,//skip the first one since we know that's the end anyway
        elements     = characterGroups.length,
        element      = null,
        prevHadUpper = false,
        last         = null,
        lookFor      = '';

    while(!found && position < elements) {
        element = characterGroups[position].split('');

        if(element.length > 0) {
           last = element[element.length-1];

           // test last character rule
           if(
              last=='.' ||                // ends in '.'
              last=='!' ||                // ends in '!'
              last=='?' ||                // ends in '?'
              (last=='"' && prevHadUpper) // ends in '"' and previous started [A-Z]
           ) {
              found = true;
              index = position-1;
              lookFor = last+' '+characterGroups[position-1];
           } else {
              if(element[0] == element[0].toUpperCase()) {
                 prevHadUpper = true;
              } else {
                 prevHadUpper = false;
              }
           }
        } else {
           prevHadUpper = false;
        }
        position++;
    }


    var trimPosition = string.lastIndexOf(lookFor)+1;
    return string.substr(0,trimPosition);
}
Run Code Online (Sandbox Code Playgroud)

为它制作一个插件是微不足道的,但要注意假设!:)

这有帮助吗?

谢谢,AE