3 回答

TA貢獻1847條經驗 獲得超7個贊
最好的工具是遞歸,而不是正則表達式。基本思想是從字符串的開頭開始尋找一個單詞,然后從字符串的其余部分開始尋找另一個單詞,依此類推,直到到達字符串的末尾。遞歸解決方案是很自然的,因為當字符串的給定其余部分不能分解為一組單詞時,需要進行回溯。下面的解決方案使用詞典來確定什么是單詞,并在找到它們時打印出解決方案(一些字符串可以分解為多個可能的單詞組,例如wickedweather可以解析為“對我們不利”)。如果您只想要一組單詞,則需要確定選擇最佳單詞的規(guī)則,
#!/usr/bin/perl
use strict;
my $WORD_FILE = '/usr/share/dict/words'; #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;
}
- 3 回答
- 0 關注
- 643 瀏覽
添加回答
舉報