• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 228
  • Last Modified:

find occurences of words in text string for text spinning

Hi all.

What I would like to do is be able to find occurences in a text file and replace them.

I have a text file synonyms.txt
each line looks like this

a week|per week
brilliant|amazing|outstanding|excellent|fantastic|great|remarkable|dazzling
places to go|places to check out|places to visit|places you can see

next i have a text string say like

$text = "What a week. It was amazing. The places to go were great."

What I would like it to do, is open the file synonyms.txt
find all 3 word occurences and then check them within the $text and replace the text.(such as "places to go")
Then all 2 word occurence and check them(such as "per week" and "a week")
Then all 1 word (brilliant,amazing etc)

then to output a random variety of it.

such as "What per week. It was excellent. The places to visit were excellent."

Any suggestions.

thank you for any help.
0
paulp75
Asked:
paulp75
  • 4
  • 4
1 Solution
 
Lukasz ChmielewskiCommented:
Try this:

<?php

    $lines = array();

    $f = file("synonyms.txt");
    foreach($f as $value){
        $lines[] = explode("|",$value);
    }
    
    $x1 = rand(0,count($lines[0])-1);
    $x2 = rand(0,count($lines[1])-1);
    $x3 = rand(0,count($lines[2])-1);
    echo"What ".$lines[0][$x1].". It was ".$lines[1][$x2].". The ".$lines[2][$x3]." were ".$lines[1][$x2].".";
?>

Open in new window

0
 
paulp75Author Commented:
sorry i didnt mention that it can only replace the text with words from the same line.
there might be 500 lines in the text file.
0
 
Lukasz ChmielewskiCommented:
So is this a one line:

a week|per week
brilliant|amazing|outstanding|excellent|fantastic|great|remarkable|dazzling
places to go|places to check out|places to visit|places you can see

or three ?
Do you have a "3-line portions" to get text from ?
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.

 
paulp75Author Commented:
that would be 3 lines

they are all to be similar phrases, that can be replaced in a text string.
0
 
Lukasz ChmielewskiCommented:
So ok - to simplify - you have a file with lines as this:

a|aa|aaa
b|bb|bbb
cc|c|ccc
dd|dddd|ddddd


and a text:
This is a. This was bb. This will be ccc.

so now,
1. you have to find a line with "a" and pick some random value from that line - like "aaa"
2. you have bb - find a line with bb and replace with random - like "bbb"
3. ccc - fint the line with ccc and pick for example: c

So that your text would become
This is aaa. This was bbb. This will be c.

Right ?
My question is:
if so, every line in your text file would have a different words, so there would not be two lines with the same word ?
brilliant|amazing|outstanding|excellent
brilliant|amazing|outstanding|excellent|fantastic|great|remarkable|dazzling

And second one: can you pick ramdomly the same value from that line or is it have to be different ?
0
 
Ray PaseurCommented:
This seems susceptible of being programmed in PHP, but it's not really a question - it's a need for application development and for that you might want to hire a professional developer.  I think that this would better be handled by a token replacement system instead of starting with a text.  So maybe your input would look like:

$text = "What A. It was B. The C were D.";

Then your data base would have a collection of rows to match each of A, B, C and D.  You can select each of these replacements with ORDER BY RAND and make simple character substitutions.  Other features such as weighting the replacements so that some happen more than others are easy extensions if you do it this way.

HTH, ~Ray
0
 
paulp75Author Commented:
Roads Roads, yes thats right. it can be random, and will need to go from 3 words phrases down to one word.

Hope that makes sense.

thanks for the responses.
0
 
Lukasz ChmielewskiCommented:
If I understood everything correct, this would do:

<?php

    $text = "What a week. It was amazing. The places to go were great.";
    echo"The text is:<br />".$text."<br /><br />";
    
    $pattern = "/What\s(.*)\. It was\s(.*)\. The\s(.*)were\s(.*)\./s";
    
    preg_match($pattern,$text,$matches);
    $word1 = trim($matches[1]);
    $word2 = trim($matches[2]);
    $word3 = trim($matches[3]);
    $word4 = trim($matches[4]);
    
    echo"word1: $word1<br />word2: $word2<br />word3: $word3<br />word4: $word4<br /><br />";

    $lines = array();

    $f = file("synonyms.txt");
    foreach($f as $value){
        $lines[] = explode("|",$value);
    }
    
    
    // we're searching for words in the whole array
    
    for($i=0;$i<count($lines);$i++){
        //print_r($lines[$i]);
        if(in_array($word1,$lines[$i])){
            // we found the word in the array - pick the random word from the same array
            $x1 = rand(0,count($lines[$i])-1);
        }
        if(in_array($word2,$lines[$i])){
            // we found the word in the array - pick the random word from the same array
            $x2 = rand(0,count($lines[$i])-1);
        }        
        if(in_array($word3,$lines[$i])){
            // we found the word in the array - pick the random word from the same array
            $x3 = rand(0,count($lines[$i])-1);
        } 
        if(in_array($word4,$lines[$i])){
            // we found the word in the array - pick the random word from the same array
            $x4 = rand(0,count($lines[$i])-1);
        } 
    }

    echo"What ".$lines[0][$x1].". It was ".$lines[1][$x2].". The ".$lines[2][$x3]." were ".$lines[1][$x2].".";
?>

Open in new window

0
 
paulp75Author Commented:
I managed to work this one out myself, but i really appreciated the help. It helped to get me on the right track anyway.

I've only got a couple of small problems with the script i now have but will start new question, as it wasnt mentioned in original question. thanks
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now