?
Solved

Word Manipulation

Posted on 2010-09-16
6
Medium Priority
?
288 Views
Last Modified: 2013-12-26
Hi,

I'm looking for a way to generate all possible combinations of keywords.

The file has 500K+ records formatted in the following way (3 rows with tabs)

smart      convention      largest

The output I'm looking for should generate all possible combinations and sorround the output in ()

(smart convention largest)
(smart largest convention)
(largest convention smart)
(largest smart convention)
(convention smart largest)
(convention largest smart)
0
Comment
Question by:faithless1
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 7

Expert Comment

by:vikas_madhusudana
ID: 33698880
input the file to the shell scrpt

#!/bin/sh
#num=`wc -l $1`
for a in `seq 500000`
do
b=`head -n $a $1 | tail -n 1`
var1=`echo $b | cut -d " " -f 1`
var2=`echo $b | cut -d " " -f 2`
var3=`echo $b | cut -d " " -f 3`
echo "("$var1 $var2 $var3")"
echo "("$var1 $var3 $var2")"
echo "("$var2 $var1 $var3")"
echo "("$var2 $var3 $var1")"
echo "("$var3 $var2 $var1")"
echo "("$var3 $var1 $var2")"
done

Open in new window

0
 
LVL 31

Expert Comment

by:Marco Gasi
ID: 33699950
I would do it this way. First, Iwould create three array, each for column, wich grab column values. I never done this, but if you post a little sample file equal to yours I cant test.

Then You create a multidimansional array with three ayyay $smart, $convention and $largest

$totArr = array($smart, $convention, $largest);

Last you can use the snippet posted below to generate all combinations.

Hope this helps
//combination algorithm
function generateCodes($arr) {
    global $returnArray, $tempArray, $pos;
    if(count($arr)) {
        for($i=0; $i<count($arr[0]); $i++) {
            $tmp = $arr;
            $tempArray[$pos] = $arr[0][$i];
            $tarr = array_shift($tmp);
            $pos++;
            generateCodes($tmp);
        }
    } else {
        $returnArray[] = strtoupper(join("", $tempArray));
    }
    $pos--;
}


//How to use:
$returnArray = array();
$tempArray = array();
$pos = 0;
generateCodes($totArr);
foreach($returnArray as $value){
  echo $value."£<br />";
}

Open in new window

0
 
LVL 10

Expert Comment

by:jeromee
ID: 33702224
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 11

Accepted Solution

by:
VanHackman earned 2000 total points
ID: 33702287
Here is the solution with a test case file, in order that you see it works. =P
Just put both files in the same directory and try them. ;)

PD:
<?php
header("Content-Type:text/plain");

$WordSource = file("WordSource.txt");

foreach($WordSource as $line)
{
	$Words = explode("\t", str_replace("\n",'',trim($line)));
	scramble($Words[0],$Words[1],$Words[2]);
}

function scramble($Word1, $Word2, $Word3)
{
	echo "($Word1 $Word2 $Word3)\n";
	echo "($Word1 $Word3 $Word2)\n";
	echo "($Word2 $Word1 $Word3)\n";
	echo "($Word2 $Word3 $Word1)\n";
	echo "($Word3 $Word2 $Word1)\n";
	echo "($Word3 $Word1 $Word2)\n";
}

?>

Open in new window

WordSource.txt
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 33702700
This seems to work correctly.  Install it and run it on your server and please post back with any questions, ~Ray
<?php // RAY_temp_failthless1.php
error_reporting(E_ALL);
echo "<pre>" . PHP_EOL;



// TEST DATA FROM THE DESCRIPTION AT EE
$dat = <<<DAT
smart      convention      largest
cat dog pig
coffee tea     me you
DAT;



// INTERPRET THE TEST DATA INTO AN ARRAY OF ARRAYS
$new = array();

// MAN PAGE: http://us.php.net/manual/en/function.explode.php
$arr = explode(PHP_EOL, $dat);
foreach ($arr as $str)
{
    // EXAMPLE 5 MAN PAGE: http://us.php.net/manual/en/function.preg-replace.php
    $str = trim(preg_replace('/\s\s+/', ' ', $str));
    $new[] = explode(' ', $str);
}
// ACTIVATE THIS TO SEE THE NEW ARRAY OF ARRAYS
// var_dump($new);



// CREATE THE COMBINATIONS
foreach ($new as $sub)
{
    $sub_combos[] = array_combinations($sub);
}
// ACTIVATE THIS TO SEE THE COMBINATIONS
// var_dump($sub_combos);



// CREATE THE PARENTHESIZED STRINGS
$answer = '';
foreach ($sub_combos as $array_set)
{
    foreach ($array_set as $final_array)
    {
        $answer .= '(' . implode(' ', $final_array) . ')' . PHP_EOL;
    }
    $answer .= PHP_EOL; // READABILITY
}
// SHOW THE WORK PRODUCT
echo $answer;


// GENERATE THE COMBINATIONS OF CHARACTERS IN A STRING.
// OUTPUT IS AN ARRAY OF COUNT = strlen() FACTORIAL
// ADAPTED FROM http://cogo.wordpress.com/2008/01/08/string-permutation-in-php/
function string_combinations($str)
{
    if (strlen($str) < 2) return array($str);

    $combos = array();
    $endpop = substr($str, 1);

    // RECURSE
    foreach (string_combinations($endpop) as $permutation)
    {
        $length = strlen($permutation);
        for ($i = 0; $i <= $length; $i++)
        {
            $combos[] = substr($permutation, 0, $i) . $str[0] . substr($permutation, $i);
        }
    }
    return $combos;
}

// GENERATE THE COMBINATIONS OF ELEMENTS IN A ONE-DIMENSIONAL ARRAY
function array_combinations($arr)
{
    $return = array();

    // GET THE NUMERIC KEY RANGE OF THE INPUT ARRAY
    $str = implode('', range(0, count($arr)-1));

    // GET THE KEY COMBINATIONS
    $combos = string_combinations($str);

    // ITERATE OVER THE COMBINATIONS
    foreach ($combos as $combo)
    {
        $out = array();
        while (strlen($combo))
        {
           $key   = substr($combo,0,1);
           $combo = substr($combo,1);
           $out[] = $arr[$key];
        }
        $return[] = $out;
    }
    return $return;
}

Open in new window

0
 
LVL 8

Expert Comment

by:shanikawm
ID: 33713465
Following perl shell command will help
perl -ne 'chomp; 
print "(",$_,")\n("; 
split(/[ \s]+/); 
print join(" ",($_[0],$_[2],$_[1])),")\n(",
join(" ",($_[1],$_[0],$_[2])),")\n(",
join(" ",($_[1],$_[2],$_[0])),")\n(",
join(" ",($_[2],$_[0],$_[1])),")\n(",
join(" ",($_[2],$_[1],$_[0])),")\n";' file.txt 

Open in new window

0

Featured Post

Video: Liquid Web Managed WordPress Comparisons

If you run run a WordPress, you understand the potential headaches you may face when updating your plugins and themes. Do you choose to update on the fly and risk taking down your site; or do you set up a staging, keep it in sync with your live site and use that to test updates?

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question