我有一个经过充分解析的多段文档列表(所有段落用 \ n \ n 分隔,句子用“。”分隔),我想分成句子,以及表示文件中段落编号的数字。例如,(两段)输入是:
First sentence of the 1st paragraph. Second sentence of the 1st paragraph. \n\n
First sentence of the 2nd paragraph. Second sentence of the 2nd paragraph. \n\n
理想情况下,输出应为:
1 First sentence of the 1st paragraph.
1 Second sentence of the 1st paragraph.
2 First sentence of the 2nd paragraph.
2 Second sentence of the 2nd paragraph.
我熟悉Perl中可以将文档拆分成句子的Lingua :: Sentences包。但是它与段落编号不兼容。因此,我想知道是否有另一种方法来实现上述目标(文档不包含缩写)。任何帮助是极大的赞赏。谢谢!
答案 0 :(得分:5)
如果您可以依赖句点.
作为分隔符,则可以执行以下操作:
perl -00 -nlwe 'print qq($. $_) for split /(?<=\.)/' yourfile.txt
说明:
-00
将输入记录分隔符设置为空字符串,即段落模式。-l
将输出记录分隔符设置为输入记录分隔符,在这种情况下转换为两个换行符。然后我们简单地用句子断言来分割期间并打印句子,然后是行号。
答案 1 :(得分:2)
正如您所提到的Lingua::Sentences
,我认为可以选择稍微操作此模块的原始输出以获得您需要的内容
use Lingua::Sentence;
my @paragraphs = split /\n{2,}/, $splitter->split($text);
foreach my $index (0..$#paragraphs) {
my $paragraph = join "\n\n", map { $index+1 . " $_" }
split /\n/, $paragraphs[$index];
print "$paragraph\n\n";
}