2011-07-30 2 views
0

내가 여러 합류 단어를 분할하기 위해 노력하고있어 도움이 필요하고 난 스크립트는 여러 옵션을 출력하지만, 난 그냥 일반적으로, 마지막이 필요 How can I split multiple joined words?은 워드 감지 서브 루틴

에서 잡고 한 펄 스크립트를했습니다 올바른 하나, 이것을 달성하기 위해 스크립트에서 무엇을 변경해야합니까?

#!/usr/bin/perl 

use strict; 

my $WORD_FILE = 'dic_master'; #Change as needed 
my %words; # Hash of words in dictionary 

# Open dictionary, load words into hash 
open(WORDS, $WORD_FILE) or die "Failed to open dictionary: $!\n"; 
while (<WORDS>) { 
    chomp; 
    $words{lc($_)} = 1; 
} 
close(WORDS); 

# Read one line at a time from stdin, break into words 
while (<>) { 
    chomp; 
    my @words; 
    find_words(lc($_)); 
} 

sub find_words { 
    # Print every way $string can be parsed into whole words 
    my $string = shift; 
    my @words = @_; 
    my $length = length $string; 

    foreach my $i (1 .. $length) { 
    my $word = substr $string, 0, $i; 
    my $remainder = substr $string, $i, $length - $i; 
    # Some dictionaries contain each letter as a word 
    next if ($i == 1 && ($word ne "a" && $word ne "i")); 

    if (defined($words{$word})) { 
     push @words, $word; 
     if ($remainder eq "") { 
     print join(' ', @words), "\n"; 
     return; 
     } else { 
     find_words($remainder, @words); 
     } 
     pop @words; 
    } 
    } 

    return; 
} 

고마워요!

+0

질문을 쉽게 이해할 수 없습니다. 스크립트가 제작하는 내용과 원하는 내용을 설명해 주시겠습니까? – sergio

+0

스크립트는 예 : "bemyguest"인수를 받아서 사전에 단어베이스로 나눕니다. 내 수식이됩니다/ 내 가제가 될 것입니다/ 내 손님이됩니다/ 그냥 마지막으로 출력하고 싶습니다. –

답변

4

find_wordsprint을 변수로 할당하고 for 루프가 끝난 후에 인쇄하십시오.

1

bvr's answer은 문제의 직접적인 필요를 해결합니다.

defined 대신 exists을 사용하여 문자열이 사전에 있는지 확인하는 것이 좋습니다. 이렇게하면 'bemyg'과 같은 비 단어가 사전 해시에서 키가 될 수 없습니다.

관련 문제