6

長いテキストチャンク(実際には1段落)の最後の文を削除するのに十分スマートな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 later described it as: "Something insane."次のようになります。She did not know, "I think we should move past the fence!", she quickly said."

4

3 に答える 3

3

ルールを定義します: // 1. 文は大文字で始まります // 2. 文の前には何もないか [.!?] がありますが、[,:;] はありません // 3. 文の前に引用符を付けることができます["'] のように適切にフォーマットされていない場合 // 4. この場合、引用符に続く単語が名前の場合、文が正しくない可能性があります

追加のルールはありますか?

目的を定義してください: // 1. 最後の文を削除してください

仮定: テキストの文字列の最後の文字から始めて逆方向に作業した場合、文の先頭を次のように識別します: 1. 文字の前のテキストの文字列は [.?!] または 2. 文字列文字の前のテキストは ["'] であり、先頭に大文字が続きます 3. すべての [.] の前にスペースがあります 4. HTML タグについては修正していません定期的

考えられる解決策: 文字列を読み取り、スペース文字で分割して、逆に確認する文字列のチャンクを提供します。

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

あなたの文字列が次の場合:

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

次に、配列は次のように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() メソッドを使用して削除されます。

各ブロックの後にはスペースが続くため、(配列インデックスによって) 文の開始位置を特定すると、スペースのインデックスがわかり、スペースがそのインデックスを占有する場所で元の文字列を最後から分割できます。文の。

見つかったかどうかを示す変数と、最後の文の開始を保持していると特定した配列要素のインデックス位置を保持する変数を自分自身に与えます。

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

上記のスクリプトを実行すると、'He' が最後の文の始まりとして正しく識別されます。

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.

もう一度実行すると、次のようになります。基本的な html コードが使用されることもありますが、それによって文の「選択」が難しくなることはありません。窓を見上げると、上空を飛行機が飛んでいるのが見えた。私は最初に頭に浮かんだことを尋ねました:「あそこで何をしているの?」

もう一度実行すると、次のようになります。基本的な html コードが使用されることもありますが、それによって文の「選択」が難しくなることはありません。窓を見上げると、上空を飛行機が飛んでいるのが見えた。

もう一度実行すると、次のようになります。基本的な html コードが使用されることもありますが、それによって文の「選択」が難しくなることはありません。

もう一度実行すると、次のようになります。

もう一度実行すると、次のようになります。

それで、これはあなたが探しているものと一致すると思いますか?

関数として:

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

于 2011-10-01T08:31:29.413 に答える
2

これで済むはずです。

/*
Assumptions:
- Sentence separators are a combination of terminators (.!?) + doublequote (optional) + spaces + capital letter. 
- I haven't preserved tags if it gets down to removing the last sentence. 
*/
function removeLastSentence(text) {

    lastSeparator = Math.max(
        text.lastIndexOf("."), 
        text.lastIndexOf("!"), 
        text.lastIndexOf("?")
    );

    revtext = text.split('').reverse().join('');
    sep = revtext.search(/[A-Z]\s+(\")?[\.\!\?]/); 
    lastTag = text.length-revtext.search(/\/\</) - 2;

    lastPtr = (lastTag > lastSeparator) ? lastTag : text.length;

    if (sep > -1) {
        text1 = revtext.substring(sep+1, revtext.length).trim().split('').reverse().join('');
        text2 = text.substring(lastPtr, text.length).replace(/['"]/g,'').trim();

        sWithoutLastSentence = text1 + text2;
    } else {
        sWithoutLastSentence = '';
    }
    return sWithoutLastSentence;
}

/*
TESTS: 

var text = '<p>Blabla, some more text here. Sometimes <span>basic</span> html code is used but that should not make the "selection" of the text 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>';

alert(text + '\n\n' + removeLastSentence(text));
alert(text + '\n\n' + removeLastSentence(removeLastSentence(text)));
alert(text + '\n\n' + removeLastSentence(removeLastSentence(removeLastSentence(text))));
alert(text + '\n\n' + removeLastSentence(removeLastSentence(removeLastSentence(removeLastSentence(text)))));
alert(text + '\n\n' + removeLastSentence(removeLastSentence(removeLastSentence(removeLastSentence(removeLastSentence(text))))));
alert(text + '\n\n' + removeLastSentence(removeLastSentence(removeLastSentence(removeLastSentence(removeLastSentence(removeLastSentence(text)))))));
alert(text + '\n\n' + removeLastSentence('<p>Blabla, some more text here. Sometimes <span>basic</span> html code is used but that should not make the "selection" of the text any harder! I looked up the '));
*/
于 2011-10-03T06:48:21.120 に答える
0

これは良いものです。一時変数を作成して、すべての '!' を変換してみませんか。と '?' 「。」に変換し、その一時変数を分割し、最後の文を削除し、その一時配列を文字列にマージして、その長さを取得しますか? 次に、元の段落をその長さまでサブストリング化します

于 2011-09-23T16:02:27.453 に答える