有一些字符串内容,我必须拆分。首先,我需要将字符串内容分成几行。将字符串拆分为行和句子,但忽略缩写
这是我该怎么办:
str.split('\n').forEach((item) => {
if (item) {
// TODO: split also each line into sentences
let data = {
type : 'item',
content: [{
content : item,
timestamp: Math.floor(Date.now()/1000)
}]
};
// Save `data` to DB
}
});
但现在我还需要每一行分成句子。我对此的困难是正确分割它。因此我会使用.
(点和空格)来分割线条。 但也有缩略语的数组,不应分割线:
cont abbr = ['vs.', 'min.', 'max.']; // Just an example; there are 70 abbrevations in that array
...而且有几个规则:
- 任何数量和网点或单个字母和点也应该被忽略,因为分割字符串:
1.
,2.
,30.
,A.
,b.
- 大写和小写应该被忽略:
Max. Lorem ipsum
不应被分裂。Lorem max. ipsum
。
例
const str = 'Just some examples:\nThis example has min. 2 lines. Max. 10 lines. There are some words: 1. Foo and 2. bar.';
的该结果应该是四个数据对象:
{ type: 'item', content: [{ content: 'Just some examples:', timestamp: 123 }] }
{ type: 'item', content: [{ content: 'This example has min. 2 lines.', timestamp: 123 }] }
{ type: 'item', content: [{ content: 'Max. 10 lines.', timestamp: 123 }] }
{ type: 'item', content: [{ content: 'There are some words: 1. Foo and 2. bar.', timestamp: 123 }] }
你可能,可能的话,可以用一个正则表达式来做到这一点(我做不到,但并不意味着这是不可能的),但它会写一个野兽并保持。我建议使用一个非常宽松的正则表达式来扫描字符串中的潜在匹配,然后在上下文中对照像您所描述的一组规则对它们进行评估。它仍然很复杂,但至少应该更易于阅读和排除故障。另外,如果你正在分裂自然语言文本,不要忽视''你好,我是Sue,“她说。 “这是一个字符串?”她问。 “这是。”'和'我喜欢'字符串'这样的单位。' – Palpatim