ルールを定義します:
// 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