-1

In this URL How can I split multiple joined words?, i have found a source code which works perfectly written in perl, but my requirement is in PHP.

I have never worked on perl, not even once, But I have managed to translate the perl code to PHP.

But it does not give the correct result, can you please help me in finding out the problem.

#!/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 code written by me, but not working.

<?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 ;
}

?>
Community
  • 1
  • 1
Mani
  • 440
  • 4
  • 15

2 Answers2

1

The working copy of it, not sure is there any mistake in it. If someone can find some fault, please let me know.

<?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 ;
}

?>
Mani
  • 440
  • 4
  • 15
  • Though, this question is down voted. But i believe this will be useful to someone else. – Mani Feb 24 '16 at 07:00
0

Your foreach ($WORD_FILE as $str) needs to have an array in place of $WORD_FILE so it should be something like:

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

file_get_contents only returns a string. You can use explode to break that string into an array based on another string. In this case, I used the " " to represent a space.

oneeach
  • 87
  • 1
  • 8