如何从文本中去除小写句子片段?
我正在尝试用正则表达式或简单的Perl一行代码,从标准文本文件中去掉小写的句子片段。
这些通常被称为对话或引述标签,比如“他说”、“她说”等等。
下面这个例子展示了手动删除前后的对比:
- 原始文本:
“啊,那完全是真的!”阿廖沙惊呼道。
“哦,别再装傻了!有个傻瓜进来了,你让我们感到羞愧!”窗边的女孩突然转向她的父亲,带着一种轻蔑和鄙视的神情。
“等一下,瓦尔瓦拉!”她的父亲喊道,语气强硬,但看着他们时却很赞许。“那就是她的性格,”他说,重新对阿廖沙说道。
“你去哪儿了?”他问他。
“我想,”他说,“我好像忘了什么……我的手帕,我想……好吧,即使我没忘什么,让我再待一会儿。”
他坐下了。父亲站在他身边。
“你也坐下,”他说。
- 手动删除所有小写句子片段后的文本:
“啊,那完全是真的!”
“哦,别再装傻了!有个傻瓜进来了,你让我们感到羞愧!”
“等一下,瓦尔瓦拉!” “那就是她的性格,”
“你去哪儿了?”
“我想,” “我好像忘了什么……我的手帕,我想……好吧,即使我没忘什么,让我再待一会儿。”
他坐下了。父亲站在他身边。
“你也坐下,”
我把直引号“改成了平衡引号,并尝试了: ” (...)+[.]
当然,这样可以去掉一些片段,但也删除了一些平衡引号中的文本和以大写字母开头的文本。[ ^A-Z]在上面的表达式中没有起作用。
我意识到可能不可能做到100%的准确,但任何有用的表达式、Perl或Python脚本都会非常感激。
谢谢,
亚伦
5 个回答
这个方法适用于问题中提到的所有情况:
sed -n '/"/!{p;b}; s/\(.*\)"[^"]*/\1" /;s/\(.*"\)\([^"]*\)\(".*"\)/\1 \3/;p' textfile
但是在以下这些情况下,它就不管用了:
He said, "It doesn't always work."
"Secondly," I said, "it fails for three quoted phrases..." He completed my thought, "with two unquoted ones."
I replied, "That's right." dejectedly.
如果你想用Perl来处理文本,那么Text::Balanced
这个模块可能正是你需要的。下面的代码可以提取你例子中的所有引号内容(虽然不太好看,但能完成任务)。
这个代码也适用于Dennis的测试案例。
下面代码的一个优点是,它会按段落把引号内容分组,这对后续分析可能有用,也可能没用。
脚本
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."
这里有一段Python代码,可以实现你想要的功能:
thetext="""triple quoted paste of your sample text"""
y=thetext.split('\n')
for line in y:
m=re.findall('(".*?")',line)
if m:
print ' '.join(m)
else:
print line