这给出了整行:
#!/usr/bin/perl
$file = 'output.txt';
open(txt, $file);
while($line = <txt>) {
print "$line" if $line =~ /_NN/;
}
close(txt);
答案 0 :(得分:2)
#!/usr/bin/perl
use strict;
use warnings FATAL => "all";
binmode(STDOUT, ":utf8") || die;
my $file = "output.txt";
open(TEXT, "< :utf8", $file) || die "Can't open $file: $!";
while(<TEXT>) {
print "$1\n" while /(\w+)_NN\b/g;
}
close(TEXT) || die "Can't close $file: $!";
答案 1 :(得分:1)
print "$1" if $line =~ /(\S+)_NN/;
答案 2 :(得分:1)
您的答案脚本读起来有点尴尬,并且有一些潜在的错误。我会像这样重写主逻辑循环:
foreach my $line (grep { /expend_VB/ } @sentences) {
my @nouns = grep { /_NN/ } split /\s+/, $line;
foreach my $word (@nouns) {
$word =~ s/_NN//;
print "$word\n";
}
print "$line\n" if scalar(@nouns);
}
你需要将我的声明放在循环中 - 否则它会持续的时间超过你想要的时间,并且可能会在以后引起问题。
foreach 是迭代列表的更常见的perl习语。
答案 3 :(得分:-1)
#!/usr/bin/perl
use strict;
use warnings FATAL => "all";
my $search_key = "expend"; ## CHANGE "..." to <>
open(my $tag_corpus, '<', "ch13tagged.txt") or die $!;
my @sentences = <$tag_corpus>; # This breaks up each line into list
my @words;
for (my $i=0; $i <= @sentences; $i++) {
if ( defined( $sentences[$i] ) and $sentences[$i] =~ /($search_key)_VB.*/i) {
@words = split /\s/,$sentences[$i]; ## \s is a whitespace
for (my $j=0; $j <= @words; $j++) {
#FILTER if word is noun:
if ( defined( $words[$j] ) and $words[$j] =~ /_NN/) {
#PRINT word and sentence:
print "**",split(/_\S+/,$words[$j]),"**", "\n";
print split(/_\S+/,$sentences[$i]), "\n"
}
} ## put print sentences here to print each sentence after all the nouns inside
}
}
close $tag_corpus || die "Can't close $tag_corpus: $!";