加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

正则表达式 – 使用Perl自动化形态标记

发布时间:2020-12-14 02:29:11 所属栏目:百科 来源:网络整理
导读:让我们假设我有一个带有形态标签的文本,以及一个没有标签的类似文本.两种文本以行间方式合并,一行低于另一行.因此(为清楚起见,增加了额外的回车): The(Art) day(N) started(V) well(Adv),windy(Adj) and(C) humid(Adj), here(Adv) in(P) London(PN), The da
让我们假设我有一个带有形态标签的文本,以及一个没有标签的类似文本.两种文本以行间方式合并,一行低于另一行.因此(为清楚起见,增加了额外的回车):

The(Art) day(N) started(V) well(Adv),windy(Adj) and(C) humid(Adj),
here(Adv) in(P) London(PN),

The day was windy and quite humid here in London,

but(P) we(Pr) did(AuxV) not(Adv) mind(V),because(P) we(Pr) had(AuxV)
planned(V) to(P) stay(V) indoors(Adv)

but no problem at all,mate! We had planned to stay at home anyway!

第二行(即未标记的文本)始终以空格和制表符开头.

此外,可以安全地忽略标点符号和区分大小写.此外,可能是第一行中的某些单词未被标记的情况.

所以,从这种伪代码开始,鉴于我对Perl的了解有限,我决定构建一系列正则表达式来提取第1行的标签(总是在括号中)并将它们插入第2行,前提是言语是一样的.

我当前的代码如下所示:

use strict;
use warnings;

while ( <DATA> )
{
s/(^w+)((w+))?(.+r)(st)(1)/$1$2$3$4$5$2/g; #Tag 1st word on line 2 (if it's the same one as the 1st on line 1).
s/(^w+)((w+))?s(w+)((w+))?(.+r)(st)(12)s(3)/$1$2 $3$4$5$6$7 $8$4/g; #Tag 2nd word on line 2 (if it's the same one as the 2nd on line 1).
# And so on...

print;
}


__DATA__
The(Art) day(N) started(V) well(Adv),here(Adv) in(P) London(PN),The day was windy and quite humid here in London,but(P) we(Pr) did(AuxV) not(Adv) mind(V),because(P) we(Pr) had(AuxV) planned(V) to(P) stay(V) indoors(Adv) 
   but no problem at all,mate! We had planned to stay at home anyway!

显然,我想要的输出看起来如下:

The(Art) day(N) started(V) well(Adv),
The(Art) day(N) was windy and quite humid(Adj) here(Adv) in(P)
London(PN),

but(P) we(Pr) did(AV) not(Adv) mind(V),because(P) we(Pr) had(AuxV)
planned(V) to(P) stay(V) indoors(Adv)

but(P) no problem at all,mate! We(Pr) had(AuxV) planned(V) to(P)
stay(V) at home anyway!

我的问题是双重的:

a)上面的脚本(目前我试图替换第一个和第二个单词)不起作用,虽然我认为正则表达式是正常的(我已经在BBEdit中测试它们作为搜索/替换).

b)我完全不确定这是解决手头任务的正确方法(即添加一系列越来越长且更复杂的正则表达式).

有人可以告诉我应该做些什么才能让它发挥作用,或者,让我看一个更好的方法来优化任务?我全都耳朵!

非常感谢你.

像这样的东西?
#!/usr/bin/perl

use strict;
use warnings;

my %tag;

while (<DATA>)
{
    if (m/((Adj|Art|AuxV|C|N|PN|V))/) # it's an example
    {
        # Loop over tagged words; memorize tag for each
        while (m/(w+)((w+))/g)
        {
            # If there were already some tags,add to existing
            $tag{$1} = (defined $tag{$1} ? "$tag{$1}|" : "") . $2;
        }
        print;
        next;
    }
    # else
    # Loop over all words; tag the ones we have a tag for
    s/(w+)/defined $tag{$1} ? "$1($tag{$1})" : $1 /eg;
    print;

    # Flush tags for next iteration
    %tag = ();
}

注意在未标记的行之前支持多个示例行;以及对单词的多个标签的支持.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读