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

标签 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>

现在我可以拆分 . 并删除数组的最后一个条目,但这不适用于以 ?! 结尾的句子有些句子以引号结尾,例如 something: "stuff."

function removeLastSentence(text) {
  sWithoutLastSentence = ...; // ??
  return sWithoutLastSentence;
}

如何做到这一点?什么是合适的算法?

编辑 - 长文本是指段落中的所有内容,句子是指实际句子(不是一行),所以在我的示例中,最后一句话是:He后来将其描述为:“有些疯狂。” 当那一个被移除时,下一个是 她不知道,“我认为我们应该越过栅栏!”,她很快说。/

最佳答案

定义您的规则: //1. 一个句子以大写字母开头 //2. 一个句子之前没有任何内容或 [.!?],但不是 [,:;] //3. 如果格式不正确,句子前面可以加引号,例如 ["'] //4. 如果引号后面的词是名字,在这种情况下句子可能是错误的

任何额外的规则?

定义您的目的: //1. 去掉最后一句

假设: 如果您从文本字符串中的最后一个字符开始并向后工作,那么您会将句子的开头标识为: 1.字符前的文本字符串是[.?!] OR 2. 字符前的文字串是["'],前面是一个大写字母 3. 每个 [.] 前面都有一个空格 4. 我们没有更正 html 标签 5. 这些假设并不稳健,需要定期调整

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

var characterGroups = $('#this-paragraph').html().split(' ').reverse();

如果你的字符串是:

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

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."';

那么你在 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,"]

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

每个 block 后面都有一个空格,所以当我们确定了句子的起始位置(通过数组索引)时,我们就会知道该空格的索引是什么,我们可以在空格占据该索引的位置拆分原始字符串从句末开始。

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

var found = false;
var index = null;

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

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++;
}

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

console.log(characterGroups[index]); // He at index=6

现在您可以遍历之前的字符串:

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.

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

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

再次运行得到: 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);
}

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

这有帮助吗?

谢谢, AE

关于Javascript (jQuery) 删除长文本的最后一句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7531664/

相关文章:

javascript - 如何组织显示 Div 和关闭

javascript - jquery 将类添加到 if, else 语句中的变量

R 将语料库分解为句子

java - Java如何将一个句子分成几部分?

正则表达式 - 将同一句子中的单词与否定环视匹配

javascript - Node.js 休息框架

JavaScript - 将数组转换为 map

javascript - navigator.serviceWorker 用于开发环境

javascript - 使用 jQuery 通过多维数组向 `<option>` 提供 `<datalist>` 元素

javascript - 当视口(viewport)小于 481px 时禁用平滑 slider