给定一个索引位置,我如何拆分出该位置所在的句子?
Given an index position, how can I split out the sentence that that position lies in?
我正在使用 JavaScript,我的文字是:
Dana's places, we're having people coming to us people wanna buy condos. They want to move quickly and we're just losing out on a lot of great places. Really what would you say this?
如果我的索引位置是6
,我只想得到第一句:Dana's places, we're having people coming to us people wanna buy condos.
如果我的索引位置是80
,我只想得到第二句:They want to move quickly and we're just losing out on a lot of great places.
如何根据位置解析句子?
如果你分期。字符串对象有一个名为 split 的原型方法,returns 一个拆分字符串的数组。在下面的示例中,str
是一个保存字符串的变量。
const str = 'first sentence. Second sentence. third sentence';
const sentences = str.split('.');
sentences[0] // first sentence
sentences[1] // second sentence, etc
如果我没理解错的话,你应该可以
分期。
获取字符串的长度。
根据句子长度确定索引的位置。
考虑到您需要拆分“?, !”同样,您只需要遍历句子并将它们进一步扁平化。阿卡,再次分裂。
老实说,使用正则表达式和组可能更干净。
这是正则表达式版本
const paragraph = "Dana's places, we're having people coming to us people wanna buy condos. They want to move quickly and we're just losing out on a lot of great places. Really what would you say this?"
/**
* Finds sentence by character index
* @param index
* @param paragraph
*/
function findSentenceByCharacterIndex(index, paragraph) {
const regex = /([^.!?]*[.!?])/gm
const matches = paragraph.match(regex);
let cursor = 0;
let sentenceFound;
for (const sentence of matches) {
sentenceFound = sentence;
cursor += sentence.length;
if( cursor > index )
{
break;
}
}
return sentenceFound;
}
const found = findSentenceByCharacterIndex(5, paragraph);
与其尝试使用 Array.split
,不如对字符串进行一些传统字符的字符解析。因为我们知道要查找的索引是什么,所以我们可以简单地四处寻找句子的开头和结尾。
一个句子如何结束?通常使用 .
、!
或 ?
- 了解这一点后,我们可以测试这些字符并决定我们使用字符串的哪一部分应该切掉 return 回到程序。如果在我们选择的索引之前没有 sentence enders
(a.e.?!.) 我们假设字符串的开头是当前句子的开头( 0) - 我们在我们选择的索引之后做同样的事情,除了我们分配 str.length
如果在索引之后没有句子结束符。
let str = "Dana's places, we're having people coming to us people wanna buy condos. They want to move quickly and we're just losing out on a lot of great places. Really what would you say this?";
let getSentence = (ind, str) => {
let beg, end, flag, sentenceEnder = ["!", ".", "?"];
Array.from(str).forEach((c, c_index) => {
if(c_index < ind && sentenceEnder.includes(c)) {
beg = c_index + 1;
}
if (flag) return;
if (c_index >= ind && sentenceEnder.includes(c)) {
end = c_index;
flag = true;
}
});
end = end || str.length;
beg = beg || 0;
return str.slice(beg, end);
}
console.log(getSentence(10, str));
console.log(getSentence(80, str));
我正在使用 JavaScript,我的文字是:
Dana's places, we're having people coming to us people wanna buy condos. They want to move quickly and we're just losing out on a lot of great places. Really what would you say this?
如果我的索引位置是6
,我只想得到第一句:Dana's places, we're having people coming to us people wanna buy condos.
如果我的索引位置是80
,我只想得到第二句:They want to move quickly and we're just losing out on a lot of great places.
如何根据位置解析句子?
如果你分期。字符串对象有一个名为 split 的原型方法,returns 一个拆分字符串的数组。在下面的示例中,str
是一个保存字符串的变量。
const str = 'first sentence. Second sentence. third sentence';
const sentences = str.split('.');
sentences[0] // first sentence
sentences[1] // second sentence, etc
如果我没理解错的话,你应该可以
分期。 获取字符串的长度。 根据句子长度确定索引的位置。
考虑到您需要拆分“?, !”同样,您只需要遍历句子并将它们进一步扁平化。阿卡,再次分裂。
老实说,使用正则表达式和组可能更干净。
这是正则表达式版本
const paragraph = "Dana's places, we're having people coming to us people wanna buy condos. They want to move quickly and we're just losing out on a lot of great places. Really what would you say this?"
/**
* Finds sentence by character index
* @param index
* @param paragraph
*/
function findSentenceByCharacterIndex(index, paragraph) {
const regex = /([^.!?]*[.!?])/gm
const matches = paragraph.match(regex);
let cursor = 0;
let sentenceFound;
for (const sentence of matches) {
sentenceFound = sentence;
cursor += sentence.length;
if( cursor > index )
{
break;
}
}
return sentenceFound;
}
const found = findSentenceByCharacterIndex(5, paragraph);
与其尝试使用 Array.split
,不如对字符串进行一些传统字符的字符解析。因为我们知道要查找的索引是什么,所以我们可以简单地四处寻找句子的开头和结尾。
一个句子如何结束?通常使用 .
、!
或 ?
- 了解这一点后,我们可以测试这些字符并决定我们使用字符串的哪一部分应该切掉 return 回到程序。如果在我们选择的索引之前没有 sentence enders
(a.e.?!.) 我们假设字符串的开头是当前句子的开头( 0) - 我们在我们选择的索引之后做同样的事情,除了我们分配 str.length
如果在索引之后没有句子结束符。
let str = "Dana's places, we're having people coming to us people wanna buy condos. They want to move quickly and we're just losing out on a lot of great places. Really what would you say this?";
let getSentence = (ind, str) => {
let beg, end, flag, sentenceEnder = ["!", ".", "?"];
Array.from(str).forEach((c, c_index) => {
if(c_index < ind && sentenceEnder.includes(c)) {
beg = c_index + 1;
}
if (flag) return;
if (c_index >= ind && sentenceEnder.includes(c)) {
end = c_index;
flag = true;
}
});
end = end || str.length;
beg = beg || 0;
return str.slice(beg, end);
}
console.log(getSentence(10, str));
console.log(getSentence(80, str));