【问题标题】:convert paragraph into sentence using Perl使用 Perl 将段落转换为句子
【发布时间】:2013-03-22 03:36:03
【问题描述】:

我正在做 Perl 编程。我需要阅读一个段落并将每个句子作为一行打印出来。

有人知道怎么做吗?

下面是我的代码:

#! /C:/Perl64/bin/perl.exe

use utf8;

if (! open(INPUT, '< text1.txt')){
die "cannot open input file: $!";
}

if (! open(OUTPUT, '> output.txt')){
die "cannot open input file: $!";
}

select OUTPUT;

while (<INPUT>){
print "$_";
}

close INPUT;
close OUTPUT;
select STDOUT;

【问题讨论】:

  • 你应该use strictuse warnings
  • “将段落转换为句子”是什么意思?您的意思是“将段落拆分为单独的句子”,然后将每个句子单独打印在一行上。 “A. P. McDowney 先生好像很忙!”一共有多少句?
  • @squiguy:第一个select 将默认输出流设置为指定文件(OUTPUT),因此不合格的print 输出到该文件;第二个重置默认值,但由于脚本即将退出是多余的。
  • @JonathanLeffler 好的,这是写入文件的一个小捷径。谢谢。
  • 是的,把段落分成句子

标签: perl text-segmentation


【解决方案1】:

我将让 Perl 来处理文件名,而不是处理文件名。

这在多个层面上都非常粗糙,整个工作无疑是艰巨的。

句子.pl

#!/usr/bin/env perl
use strict;
use warnings;
use Lingua::EN::Sentence qw(get_sentences);

sub normalize
{
    my($str) = @_;
    $str =~ s/\n/ /gm;
    $str =~ s/\s\s+/ /gm;
    return $str;
}

{
    local $/ = "\n\n";
    while (<>)
    {
        chomp;
        print "Para: [[$_]]\n";
        my @sentences = split m/(?<=[.!?])\s+/m, $_;
        foreach my $sentence (@sentences)
        {
            $sentence = normalize $sentence;
            print "Ad Hoc Sentence: $sentence\n";
        }
        my $sref = get_sentences($_);
        foreach my $sentence (@$sref)
        {
            $sentence = normalize $sentence;
            print "Lingua Sentence: $sentence\n";
        }
    }
}

split 正则表达式查找前面有句号(句点)、感叹号或问号的一个或多个空格,并匹配多行。后视(?&lt;=[.!?]) 表示标点符号与句子保持一致。 normalize 函数只是将换行符扁平化为空格并将多个空格呈现为单个空格。 (请注意,这将无法正确识别括号内的句子。)这将被视为前一句的一部分,因为. 后面没有空格。

示例输入

This is a paragraph with more than one sentence in it.  How many will be
determined later.  Mr. A. P. McDowney has been rather busy.  This
incomplete sentence will still be counted as one

This is the second paragraph.  With three sentences in it, it is a lot
less exciting than the first paragraph, but the middle sentence extends
over multiple lines and   there   is     some         wonky spacing too.
But 'tis time to finish.

样本输出

Para: [[This is a paragraph with more than one sentence in it.  How many will be
determined later.  Mr. A. P. McDowney has been rather busy.  This
incomplete sentence will still be counted as one]]
Ad Hoc Sentence: This is a paragraph with more than one sentence in it.
Ad Hoc Sentence: How many will be determined later.
Ad Hoc Sentence: Mr.
Ad Hoc Sentence: A.
Ad Hoc Sentence: P.
Ad Hoc Sentence: McDowney has been rather busy.
Ad Hoc Sentence: This incomplete sentence will still be counted as one
Lingua Sentence: This is a paragraph with more than one sentence in it.
Lingua Sentence: How many will be determined later.
Lingua Sentence: Mr. A. P. McDowney has been rather busy.
Lingua Sentence: This incomplete sentence will still be counted as one
Para: [[This is the second paragraph.  With three sentences in it, it is a lot
less exciting than the first paragraph, but the middle sentence extends
over multiple lines and   there   is     some         wonky spacing too.
But 'tis time to finish.
]]
Ad Hoc Sentence: This is the second paragraph.
Ad Hoc Sentence: With three sentences in it, it is a lot less exciting than the first paragraph, but the middle sentence extends over multiple lines and there is some wonky spacing too.
Ad Hoc Sentence: But 'tis time to finish.
Lingua Sentence: This is the second paragraph.
Lingua Sentence: With three sentences in it, it is a lot less exciting than the first paragraph, but the middle sentence extends over multiple lines and there is some wonky spacing too.
Lingua Sentence: But 'tis time to finish.

注意Lingua::EN::Sentence 是如何处理“Mr. A. P. McDowney 比头脑简单的正则表达式更好。

【讨论】:

    【解决方案2】:

    如果将段落作为字符串给出,则可以在标记句子结尾的字符上split()

    例如:

    my @sentences = split /[.?!]/, $paragraph;
    

    【讨论】:

    • 哦!那太不对了!如何判断标记是否在句末?例如,它无法正确处理此评论。
    • @ikegami OP 没有定义他所说的句子的意思。出于许多目的,可以将句子视为以指定标点符号结尾的单词序列。鉴于他当前的代码,我相信这就是 OP 的目标。
    • 您认为“诺博士说是”的世界是怎样的。可以考虑两个句子吗?如果您要制定自己的定义,则应说明它们。
    • 他的代码并没有显示出任何识别句子的尝试,所以你怎么能板着脸声称你知道他想要什么基于他甚至还没有想到的代码!我没有 -1,但这种说法让我想要。
    • 哦,另一个问题:您实际上删除了标点符号。
    【解决方案3】:

    识别句子非常困难且因语言而异。你需要帮助。也许Lingua::EN::Sentence 是要走的路?

    【讨论】:

      猜你喜欢
      • 2014-06-01
      • 2012-01-02
      • 1970-01-01
      • 2013-05-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-10
      相关资源
      最近更新 更多