2011-05-10 4 views
0

이것은 현재 아래에있는 문장으로 모든 명사를 인쇄합니다.동일한 단어가 포함 된 여러 문장을 어떻게 나열합니까? 제목은 해당 문장에 포함 된 단어입니다.

#!/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; 
my %seens =(); 
my %seenw =(); 

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, and therefore will end with _NN: 
      if (defined($words[$j]) and $words[$j] =~ /_NN/) { 
       #PRINT word (without _NN) and sentence (without any _ENDING): 
       next if $seenw{$words[$j]}++; ## How to include plural etc 
       push @words, $words[$j]; 
       print "**", split(/_\S+/, $words[$j]), "**", "\n"; 
       ## next if $seens{ $sentences[$i] }++; 
       ## push @sentences, $sentences[$i]; 
       print split(/_\S+/, $sentences[$i]), "\n" 
       ## HOW PRINT bold or specifically word bold? 
       #FILTER if word has been output, add sentence under that heading 
      } 
     } ## put print sentences here to print each sentence after all the nouns inside 
    } 
} 
close $tag_corpus || die "Can't close $tag_corpus: $!"; 
+2

가 함께 작동하도록 예제 데이터를 제공합니다.. 그것을 사용하여 "공통 단어", "제목"등으로 생각하는 것을 지적하십시오. – daxim

+1

질문에 명확한 설명이 필요합니다. "제목 인 단어 아래에 열거되어 있습니다"? 제목을 명확히하고 짧은 글꼴 설명을 추가하십시오. 보통 글꼴 크기를 사용하는 것이 좋습니다. 모든 것을 헤드 라인에 넣을 필요는 없습니다. – Lumi

답변

1

원래 :

좋은 시작이다
#!/usr/bin/perl 
use strict; 
use warnings FATAL => "all"; 

... 루프에서 정규식이 사용하려고하고 있기 때문에

my $search_key = "expend"; ## CHANGE "..." to <> 

, 그것은 컴파일하는 것이 좋습니다 지금 정규 표현식 : my $verb_regex = qr/\bexpend_VB\b/i 나는 단어 경계를 에 놓았습니다. 왜냐하면 당신이 필요로하는 것처럼 보였기 때문입니다. '

open(my $tag_corpus, '<', "ch13tagged.txt") or die $!; 

my @sentences = <$tag_corpus>; # This breaks up each line into list 
my @words; 
my %seens =(); 
my %seenw =(); 

for (my $i = 0; $i <= @sentences; $i++) { 

적은 오버 헤드와 같은 많은 작업을 수행합니다 선이 레코드 분리를 포함

if (defined($sentences[$i]) and $sentences[$i] =~ /($search_key)_VB.*/i) { 

경우 - 그것은 것입니다 : 당신 위로

while (<$tag_corpus>) { 
    ... 

chomp을 제외하고는 항상 파일 끝까지 정의 된 행을 얻으므로 이됩니다. 테스트를 할 필요가 없습니다.

또한 검색어 뒤에 .*이 필요하지 않으며 여기에서 $search_key 을 캡처해도 아무런 영향이 없습니다.

 @words = split /\s/, $sentences[$i]; ## \s is a whitespace 

당신은 공백에 대한 하나의 공간을 분할하고 싶지 않아요. /\s+/을 사용해야하지만 의 경우에도 더 좋은 결과를 얻을 수 있습니다. @words = split ' ', $sentences[$i];

하지만 그럴 필요는 없습니다.

 for (my $j = 0; $j <= @words; $j++) { 
      #FILTER if word is noun, and therefore will end with _NN: 
      if (defined($words[$j]) and $words[$j] =~ /_NN/) { 
       #PRINT word (without _NN) and sentence (without any _ENDING): 

그러나 그것은에 보내고 경우 - 당신이있어 전부 : _NN에서 끝 단어 . 또한 전체 목록은 split에서 정의됩니다. 테스트 할 필요가 없습니다.

   next if $seenw{$words[$j]}++; ## How to include plural etc 

각 문장 후 %seenw를 재설정 할 않는 한, 당신은 각 _NN 단어 파일 당을 처리 할 수 ​​있습니다.

   push @words, $words[$j]; 

나는이 push 단어의 목록 명사 등을 추가하여 어떤 목적으로 서비스를 제공 할 수 표시되지 않습니다. 물론 _NN 단어가있는 경우 무한 루프에서 을 저장하기 전에 유일성 검사를 받았지만 문장에있는 모든 단어가 개 있고 모든 "명사"가 뒤에 올 것입니다. 그뿐만 아니라, 당신은 단순히 이라는 명사임을 테스트 할 것이고 아무 것도하지 않을 것입니다.당신을 언급하지 않기 위하여 clobber 다음 문장으로 목록.

   print "**", split(/_\S+/, $words[$j]), "**", "\n"; 

       ## next if $seens{ $sentences[$i] }++; 
당신은, 나는 그것이 주석 인 경우에이 작업을 수행 할 것이라고 생각하고 있지 않다

   ## push @sentences, $sentences[$i]; 

다시 단어 루프에서이 작업을 수행하지 않으려는

단어 루프 외부 . 2 줄 전부터 모든 것이 loop 이후에 이 될 것 같습니다.

   print split(/_\S+/, $sentences[$i]), "\n" 
       ## HOW PRINT bold or specifically word bold? 
       #FILTER if word has been output, add sentence under that heading 
      } 
     } ## put print sentences here to print each sentence after all the nouns inside 
    } 
} 
close $tag_corpus || die "Can't close $tag_corpus: $!"; 

아니요. 그것은 가까운 곳에서 나쁜 수익을 처리하지 않습니다. || 또는 "바인딩"도 너무 입니다. $tag_corpus 또는 출력물을 닫습니다. 다행히도 (또는 불행하게도) 우리는 이것을 멀리 가지고 있다면, $tag_corpus은 참 값이어야하기 때문에 다이는 결코 호출되지 않습니다.

당신이 뭘 하려는지의 청소 업 버전의 일종이다 - 나는 남아의 감각을 만들 수있는 부품

my @sentences; 
# We're processing a single line at a time. 
while (<$tag_corpus>) { 
    # Test if we want to work with the line 
    next unless m/$verb_regex/; 
    # If we do, then test that we haven't dealt with it before 
    # Although I suspect that this may not be needed as much if we're not 
    # pushing to a queue that we're reading from. 
    next if $seens{ $_ }++; 

    # split -> split ' ', $_ 
    # pass through only those words that match _NN at the end and 
    # are unique so far. We test on a substitution, because the result 
    # still uniquely identifies a noun 
    foreach my $noun (grep { s/_NN$// && !$seenw{ $_ }++ } split) { 
     print "**$noun**\n"; 
    } 
    # This will omit any adjacent punctuation you have after the word--if 
    # that's a problem. 
    print split(/_\S+/), "\n"; 
    # Here we save the sentence. 
    push @sentences, $_; 
} 
close $tag_corpus or die "Can't close ch13tagged.txt: $!"; 
+0

심층적 인 해결책에 감사드립니다. 나는 문장을 출력하는 것처럼 보이지 않는다. $ sentences [$ i]로 남겨 두었다. – Jon

관련 문제