的Text::Balanced
模块是你仿佛是,如果以后你打算使用Perl。下面应该能够提取你的例子中所有引用的语音(不漂亮,但完成工作)。
它也适用于丹尼斯的测试案例。
下面的代码的优点在于,报价是由段分组,其可以是或可以不是用于以后的分析
有用脚本
use strict;
use warnings;
use Text::Balanced qw/extract_quotelike extract_multiple/;
my %quotedSpeech;
{
local $/ = '';
while (my $text = <DATA>) { # one paragraph at a time
while (my $speech = extract_multiple(
$text,
[sub{extract_quotelike($_[0])},],
undef,
1))
{ push @{$quotedSpeech{$.}}, $speech; }
}
}
# Print total number of paragraphs in DATA filehandle
print "Total paragraphs: ", (sort {$a <=> $b} keys %quotedSpeech)[-1];
# Print quotes grouped by paragraph:
foreach my $paraNumber (sort {$a <=> $b} keys %quotedSpeech) {
print "\n\nPara ",$paraNumber;
foreach my $speech (@{$quotedSpeech{$paraNumber}}) {
print "\t",$speech,"\n";
}
}
# How many quotes in paragraph 8?
print "Number of quotes in Paragraph 8: ", scalar @{$quotedSpeech{8}};
__DATA__
“啊,那是完全正确的!”阿辽沙惊呼道。 “
”哦,不要玩傻瓜! 有些白痴进来了,你把我们给 惭愧!“在窗边哭泣的女孩, 突然转向她的父亲与 轻蔑和轻蔑的空气。
“等一下,瓦尔瓦拉!”哭泣她的父亲 ,说得很透露,但 非常赞赏地看着他们。 “这是她的性格,”他说, 再次解决阿辽沙。
“你去哪儿了?”他问他。
“我认为,”他说,“我忘了 东西......我的手帕,我觉得 ....好吧,即使我还没有 忘记任何东西,让我留一个 小“。
他坐下了。父亲站在他身上。 “你也坐下,”他说。
他说:“它并不总是奏效。”
“其次,”我说,“ 三个引用短语失败......”他完成了 我的想法,“有两个没有引号的。
我回答说:“没错。”沮丧。
输出
Total paragraphs: 10
Para 1 "Ah, that's perfectly true!"
Para 2 "Oh, do leave off playing the fool! Some idiot comes in, and you put us
to shame!"
Para 3 "Wait a little, Varvara!"
"That's her character,"
Para 4 "Where have you been?"
Para 5 "I think,"
"I've forgotten something... my handkerchief, I think.... Well, even if
I've not forgotten anything, let me stay a little."
Para 7 "You sit down, too,"
Para 8 "It doesn't always work."
Para 9 "Secondly,"
"it fails for three quoted phrases..."
"with two unquoted ones."
Para 10 "That's right."
如果不是“父亲站在他身上”,你会做什么?这句话是“父亲站在吉姆身上。”? – Robusto 2010-03-13 20:53:13
我认为你需要给出一个更清晰的'小写句子片段'的定义。 特别是,为什么“惊叹阿辽沙”。一个小写的句子片段,但“父亲站在他身上。”不是? – Dancrumb 2010-03-13 21:06:14
我认为“小写的句子片段”,他的意思是“信号短语”http://nutsandbolts.washcoll.edu/quoting.html#signal – 2010-03-14 21:47:49