在PHP代码中拆分多个连接的单词 - 无法正常工作

时间:2016-02-23 10:57:29

标签: php algorithm perl function text-parsing

在这个URL How can I split multiple joined words?中,我找到了一个完全用perl编写的源代码,但我的要求是在PHP中。

我从未使用过perl,甚至没有使用过一次,但我已经设法将perl代码翻译成PHP。

但它没有给出正确的结果,请你帮我找出问题所在。

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

由我编写的PHP代码,但无效。

<?php

$WORD_FILE = file_get_contents ("word.txt") ;

$words = Array () ;

foreach ($WORD_FILE as $str)
{
    $words [$str] = 1 ;
}

while(true) 
{
    $input = trim(fgets(STDIN, 1024));
    find_words ($input) ;
}

function find_words ($str)
{
    $string = $str ;
    $length = strlen ($str) ;

    for ($i = 1 ; $i <= $length; $i++)
    {
        $word = substr ($string, 0, $i) ;
        $remainder = substr ($string, $i, $length - $i) ;

        $i++ ;

        if ($i == 1 && ($word != "a" && $word != "i")) ;

        if ($words($word))
        {
            array_push($words, $word) ;
            if ($remainder == "")
            {
                print_r ($words) ;
                return ;
            }
            else
            {
                find_words ($remainder, @words) ;
            }
            array_pop ($words) ;
        }
    }

    return ;
}

?>

2 个答案:

答案 0 :(得分:0)

您的foreach ($WORD_FILE as $str)需要有一个数组代替$WORD_FILE所以它应该是这样的:

$WORD_FILE = file_get_contents("word.txt");
$words = explode(" ", $WORD_FILE);
foreach ($words as $str) {
     *commands go here*
}

file_get_contents 只返回一个字符串。您可以使用 explode 将该字符串分解为基于另一个字符串的数组。在这种情况下,我使用" "来表示空格。

答案 1 :(得分:0)

它的工作副本,不确定是否有任何错误。 如果有人能找到一些错误,请告诉我。

<?php

$WORD_FILE = file_get_contents ("word.txt") ;

$temp_arr = explode("\n", $WORD_FILE);

foreach ($temp_arr as $str)
{
    $words [$str] = 1 ;
}

$processed = Array () ;

find_words ($argv[1]) ; 
print_r ($processed) ;

function find_words ($str)
{
    global $words ;
    global $processed ;

    $string = $str ;
    $length = strlen ($str) ;

    for ($i = 1 ; $i <= $length + 1; $i++)
    {
        $word = substr ($string, 0, $i) ;
        $remainder = substr ($string, $i, $length - $i) ;

        if ($i == 1 && ($word != "a" && $word != "i")) ;

        if (array_key_exists ($word, $words))
        {
            array_push($processed, $word) ;
            if ($remainder == "")
            {
                return ;
            }
            else
            {
                find_words ($remainder, $words) ;
            }
            echo "popping the word " . array_pop ($words) . "\n";
        }
    }

    return ;
}

?>