0

我正在尝试拆分多个连接词,并且我从 如何拆分多个连接词中获取了一个 perl 脚本?

脚本输出多个选项,但我只需要最后一个,通常是正确的,我应该在脚本中更改什么来实现这一点?

#!/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;
}

谢谢 !

4

2 回答 2

4

只需将printin替换find_words为对变量的赋值并在for循环结束后打印它。

于 2011-07-30T10:25:02.277 回答
1

bvr 的回答将解决问题的直接需求。

建议使用exists而不是defined检查字典中是否存在字符串。这将确保诸如此类的非单词'bemyg'永远不会成为字典哈希中的键。

于 2011-07-30T10:45:31.027 回答